摄像机2 API YUV420转换为rgb绿了 [英] camera2 api convert yuv420 to rgb green out

查看:4335
本文介绍了摄像机2 API YUV420转换为rgb绿了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换图像从YUV_420_888到RGB和我有一些麻烦输出图像。在我的ImageReader(利用摄像头2 API来获得此图像preVIEW)获得的图像格式YUV_420_888。

i trying convert image from YUV_420_888 to rgb and i have some trouble with output image. In ImageReader i get image in format YUV_420_888 (using camera 2 api for get this image preview).

imageReader = ImageReader.newInstance(1920,1080,ImageFormat.YUV_420_888,10);

在为YuvImage类写作Android SDK中,这YuvImage只使用NV21,YUY2。

In android sdk for YuvImage class writing, that YuvImage using only NV21, YUY2.

我们可以看到N21和YUV420之间的区别并不大,我尝试数据转换为N21

as we can see difference between N21 and yuv420 not big and i try convert data to N21

YUV420:

N21:

onImageAvailable 我得到每个单独的飞机,把他们在正确的位置(如在图片)

in onImageAvailable i get separately each Planes and put them in correct place (as on image)

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

ByteBuffer bufferY = image.getPlanes()[0].getBuffer();
byte[] data0 = new byte[bufferY.remaining()];
bufferY.get(data0);

ByteBuffer bufferU = image.getPlanes()[1].getBuffer();
byte[] data1 = new byte[bufferU.remaining()];
bufferU.get(data1);

ByteBuffer bufferV = image.getPlanes()[2].getBuffer();
byte[] data2 = new byte[bufferV.remaining()];
bufferV.get(data2);
...
outputStream.write(data0);
for (int i=0;i<bufferV.remaining();i++) {
    outputStream.write(data1[i]);
    outputStream.write(data2[i]);
}

在创建YuvImage,转换为位图,查看和保存

after create YuvImage, convert to Bitmap, view and save

final YuvImage yuvImage = new YuvImage(outputStream.toByteArray(), ImageFormat.NV21, 1920,1080, null);
ByteArrayOutputStream outBitmap = new ByteArrayOutputStream();

yuvImage.compressToJpeg(new Rect(0, 0,1920, 1080), 95, outBitmap);

byte[] imageBytes = outBitmap.toByteArray();

final Bitmap imageBitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
mImageView.setImageBitmap(imageBitmap);
...
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 95, out);

但我保存的图像是绿色和粉红色:

我错过了什么?

推荐答案

有您的转换尝试两个主要问题:

There are two main problems on your conversion attempt:


  1. 我们不能假设U和V平面是孤立的,它们可能包含交织的数据(例如: U-PLANE = {U1,V1,U2,V2,...} )。事实上,它甚至可能是一个NV21样式交织已经。这里的关键是看飞机的排步幅像素间距,并检查了我们可以假设有关 YUV_420_888格式

  2. 您已经评价说大部分的U的V平面的数据都为零的事实表明,你正在经历的 Android版上的图像的生成错误的YUV_420_888 。这意味着,即使你得到正确的转换,图像看上去依然绿色,如果你被错误,这是唯一的固定在了Android 5.1.1及以上的影响,所以它是值得检查正在使用之外的版本固定code。

  1. We can not assume that the U and V planes are isolated, they might contain interleaved data (e.g. U-PLANE = {U1, V1, U2, V2, ...} ). In fact, it might even be a NV21 style interleaving already. The key here is looking at the plane's row stride and pixel stride and also check what we can assume about the YUV_420_888 format.
  2. The fact that you've commented that most of the U an V planes data are zeros indicates that you are experiencing an Android bug on the generation of images in YUV_420_888. This means that even if you get the conversion right, the image would still look green if you are affected by the bug, which was only fixed at the Android 5.1.1 and up, so it is worth to check which version you are using besides fixing the code.

这篇关于摄像机2 API YUV420转换为rgb绿了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆