Android摄像头开发:实时摄像头视频预览帧的编码问题

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

[java]

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:

[html]

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.

Parameters

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)。

setPreviewFormat里,它又说:

[html]

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;www.2cto.com

强烈建议使用NV21格式和YV21格式,而默认情况下是NV21格式,也就是YUV420SP的。因此不经过转换,直接用BitmapFactory解析是不能成功的。事实也是如此。直接解析mData将会得到如下的错误:

另外下面也提到NV21是通用的。

getSupportedPreviewFormats ()

Added in API level 5

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

本站文章信息来源于网络以及网友投稿,本站只负责对文章进行整理、排版、编辑,是出于传递更多信息之目的,并不意味着赞同其观点或证实其内容的真实性。如果您有什么意见或建议,请联系QQ28-1688-302!

人工智能实验室
相关文章相关文章
  • 韩春雨称已能重复实验结果 近期将有消息公布

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

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

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

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

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

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

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

网友点评网友点评
阅读推荐阅读推荐

据国外媒体报道,在过去两年内,聊天机器人(chatbot)、人工智能以及机器学习的研发和采用取得了巨大进展。许多初创公司正利用人工智能和...

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

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

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