2017-07-19 52 views
0

我使用ImageFormat的相机NV21预览,当我获得NV21数据时,我尝试使用以下方法获取可显示给ImageView的字节。为什么使用100质量的位图压缩变得更小

字体:

public static byte[] n21ToBitmap(byte[] data, Camera camera) { 
    try { 
    Camera.Parameters parameters = camera.getParameters(); 
    Camera.Size size = parameters.getPreviewSize(); 
    YuvImage image = 
     new YuvImage(data, parameters.getPreviewFormat(), size.width, size.height, null); 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    image.compressToJpeg(new Rect(0, 0, size.width, size.height), 100, stream); 
    Bitmap originBitmap = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size()); 
    stream.close(); 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(270); 
    Bitmap rotateBitmap = 
     Bitmap.createBitmap(originBitmap, 0, 0, originBitmap.getWidth(), originBitmap.getHeight(), 
      matrix, true); 
    Bitmap temp = rotateBitmap.copy(rotateBitmap.getConfig(), true); 
    Log.e("TAG", "n21ToBitmap(ImageUtils.java:" 
     + Thread.currentThread().getStackTrace()[2].getLineNumber() 
     + ")" 
     + "temp:" 
     + temp.getByteCount()); 
    stream = new ByteArrayOutputStream(); 
    temp.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
    byte[] bytes = stream.toByteArray(); 
    Log.e("TAG", "n21ToBitmap(ImageUtils.java:" 
     + Thread.currentThread().getStackTrace()[2].getLineNumber() 
     + ")" 
     + "bytes:" 
     + bytes.length); 
    return bytes; 
} catch (Exception ex) { 
    Log.e(TAG, "Error:" + ex.getMessage()); 
} 
    return null; 
} 

和日志:

n21ToBitmap(ImageUtils.java:103)temp: 2073600 
n21ToBitmap(ImageUtils.java:112)bytes:311627 

那么,为什么字节长度成为smallar?

+1

这是JPG格式的压缩器:) – Vyacheslav

+0

但是为什么当你使用系统相机拍照时,你会得到一个大的图像?这也是JPG图片。 – ChengTao

回答

0

查看字节长度是数组的大小,因此每个数组索引都有一些字节值。

byte [] bytes; bytest.length =数组大小;

现在你的数组大小= 20 = bytest.length; 并且每个索引都有一些字节值。 假定每个索引包含1024个Byets。

因此总大小= 20 * 1024,这是你的情况阵列长度的实际尺寸

是311627和每个索引在每一一些字节值 所以总大小= 311627 *值的索引