次阅读 作者:智能小宝 来源:互联网 2015-09-29 10:14 我要评论(0)


final YuvImage image = new YuvImage(mData, ImageFormat.NV21, w, h, null);

ByteArrayOutputStream os = new ByteArrayOutputStream(mData.length);

if(!image.compressToJpeg(new Rect(0, 0, w, h), 100, os)){

return null;


byte[] tmp = os.toByteArray();

Bitmap bmp = BitmapFactory.decodeByteArray(tmp, 0,tmp.length);

因为这个mData是byte[ ]格式,转换流程是:byte[ ]---YuvImage----ByteArrayOutputStream---byte[ ]-----Bitmap。乍一看这个转换还真是多余了。看看看goolge的api:


public abstract void onPreviewFrame (byte[] data, Camera camera)

Added in API level 1

Called as preview frames are displayed. This callback is invoked on the event thread open(int) was called from.

If using the YV12 format, refer to the equations in setPreviewFormat(int) for the arrangement of the pixel data in the preview callback buffers.


datathe contents of the preview frame in the format defined by ImageFormat, which can be queried with getPreviewFormat(). If setPreviewFormat(int) is never called, the default will be the YCbCr_420_SP (NV21) format.

camerathe Camera service object.

大致意思是:可以用getPreviewFormat()查询支持的预览帧格式。如果setPreviewFormat(INT) 从未被调用,默认将使用YCbCr_420_SP的格式(NV21)。



public void setPreviewFormat (int pixel_format)

Added in API level 1

Sets the image format for preview pictures.

If this is never called, the default format will be NV21, which uses the NV21 encoding format.

Use getSupportedPreviewFormats() to get a list of the available preview formats.

It is strongly recommended that either NV21 or YV12 is used, since they are supported by all camera devices.

For YV12, the image buffer that is received is not necessarily tightly packed, as there may be padding at the end of each row of pixel data, as described in YV12. For camera callback data, it can be assumed that the stride of the Y and UV data is the smallest possible that meets the alignment requirements. That is, if the preview size is width x height, then the following equations describe the buffer index for the beginning of row y for the Y plane and row c for the U and V planes:

yStride= (int) ceil(width / 16.0) * 16;

uvStride= (int) ceil( (yStride / 2) / 16.0) * 16;

ySize= yStride * height;

uvSize= uvStride * height / 2;

yRowIndex = yStride * y;

uRowIndex = ySize + uvSize + uvStride * c;

vRowIndex = ySize + uvStride * c;

size= ySize + uvSize * 2;



getSupportedPreviewFormats ()

Added in API level 5

Gets the supported preview formats. NV21 is always supported. YV12 is always supported since API level 12.


  • 韩春雨称已能重复实验结果 近期将有消息公布

    韩春雨称已能重复实验结果 近期将有消息公布

  • 英国研发“杀生”机器人 通过生命体获取能量

    英国研发“杀生”机器人 通过生命体获取能量

  • 未来两年人工智能要怎么走?看这篇就够了


  • 无人驾驶汽车如何改变城市生活?听听他们怎么说




霍金 视觉中国 图 英国著名物理学家霍金(Stephen Hawking)再次就人工智能(AI)发声,他认为:对于人类来说,强大AI的出现可能是最美妙的...

文|郑娟娟 今年,人工智能(AI) 60岁了。在AI60岁的时候,笔者想要介绍一下AI100,一个刚刚2岁的研究项目,但它的预设寿命是100年,甚至更长...

AlphaGo与李世石的人机大战,为大众迅速普及了人工智能的概念。 但对谷歌而言,除了下围棋,现在的人工智能进展到哪一步了?未来,人工智能...