Android YuvImage类格式不正确?
有据可查的是,Android 的相机预览数据以 NV21 (YUV 420) 返回。 2.2 添加了一个YuvImage类,用于解码数据。我遇到的问题是 YuvImage 类数据出现损坏或不正确。我使用了名为 HelloCompute 的 Renderscript 示例应用程序,它将位图转换为单色位图。我使用了两种方法将预览数据解码为位图并将其作为输入传递给 Renderscript:
方法 1 - Android YuvImage 类:
YuvImage preview = new YuvImage(data, ImageFormat.NV21, width, height, null);
ByteArrayOutputStream mJpegOutput = new ByteArrayOutputStream(data.length);
preview.compressToJpeg(new Rect(0, 0, width, height), 100, mJpegOutput);
mBitmapIn = BitmapFactory.decodeByteArray( mJpegOutput.toByteArray(), 0, mJpegOutput.size());
// 将 mBitmapIn 传递给 RS
方法 2 - 发布的解码器方法: David Pearlman 在此发布的
// work around for Yuv format </p>
mBitmapIn = Bitmap.createBitmap(
ImageUtil.decodeYUV420SP(data, width, height),
width,
height,
Bitmap.Config.ARGB_8888);
// pass mBitmapIn to RS
正如 由 Renderscript 处理并显示的方法 1 的颗粒感很强,而且不是单色的,而方法 2 则产生预期的输出,即预览帧的单色图像。我做错了什么或者 YuvImage 类不可用吗?我正在运行 3.1 的 Xoom 上对此进行测试。
此外,在传递给 RS 之前,我在屏幕上显示了两种方法生成的位图。方法 1 的位图在光照方面有明显差异(我怀疑这是由于 JPEG 压缩造成的),而方法 2 的位图与预览帧相同。
It's well documented that Android's camera preview data is returned back in NV21 (YUV 420). 2.2 added a YuvImage class for decoding the data. The problem I've encountered is that the YuvImage class data appears corrupt or incorrect. I used the Renderscript Sample app called HelloCompute which transforms a Bitmap into a mono-chrome Bitmap. I used two methods for decoding the Preview data into a Bitmap and passing it as input to the Renderscript:
Method 1 - Android YuvImage Class:
YuvImage preview = new YuvImage(data, ImageFormat.NV21, width, height, null);
ByteArrayOutputStream mJpegOutput = new ByteArrayOutputStream(data.length);
preview.compressToJpeg(new Rect(0, 0, width, height), 100, mJpegOutput);
mBitmapIn = BitmapFactory.decodeByteArray( mJpegOutput.toByteArray(), 0, mJpegOutput.size());
// pass mBitmapIn to RS
Method 2 - Posted Decoder Method:
As posted over here by David Pearlman
// work around for Yuv format </p>
mBitmapIn = Bitmap.createBitmap(
ImageUtil.decodeYUV420SP(data, width, height),
width,
height,
Bitmap.Config.ARGB_8888);
// pass mBitmapIn to RS
When the image is processed by the Renderscript and displayed Method 1 is very grainy and not mono-chrome, while Method 2 produces the expected output, a mono-chrome image of the preview frame. Am I doing something wrong or is the YuvImage class not usable? I'm testing this on a Xoom running 3.1.
Furthermore, I displayed the bitmaps produced by both methods on screen prior to passing to the RS. The bitmap from Method 1 has noticeable differences in lighting (I suspected this was due to the JPeg compression), while Method 2's bitmap is identical to the Preview Frame.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有理由仅仅使用 Jpeg 编码/解码将 YUV 图像转换为灰度位图(我相信您毕竟需要灰度,而不是单色黑白位图)。您可以找到许多代码示例来生成您需要的结果。您可以使用这个:将预览帧转换为位图。
There is no justification for using Jpeg encode/decode just to convert a YUV image to a grayscale bitmap (I believe you want grayscale, not monochrome b/w bitmap after all). You can find many code samples that produce the result you need. You may use this one: Converting preview frame to bitmap.