摄像机2 API YUV420转换为rgb绿了

编程入门 行业动态 更新时间:2024-10-22 11:00:40
本文介绍了摄像机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(); yuvImagepressToJpeg(new Rect(0, 0,1920, 1080), 95, outBitmap); byte[] imageBytes = outBitmap.toByteArray(); final Bitmap imageBitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); mImageView.setImageBitmap(imageBitmap); ... imageBitmappress(Bitmap.CompressFormat.JPEG, 95, out);

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

我错过了什么?

推荐答案

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

There are two main problems on your conversion attempt:

  • 我们不能假设U和V平面是孤立的,它们可能包含交织的数据(例如: U-PLANE = {U1,V1,U2,V2,...} )。事实上,它甚至可能是一个NV21样式交织已经。这里的关键是看飞机的排步幅和像素间距,并检查了我们可以假设有关 YUV_420_888格式。
  • 您已经评价说大部分的U的V平面的数据都为零的事实表明,你正在经历的 Android版上的图像的生成错误的YUV_420_888 。这意味着,即使你得到正确的转换,图像看上去依然绿色,如果你被错误,这是唯一的固定在了Android 5.1.1及以上的影响,所以它是值得检查正在使用之外的版本固定code。
  • 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.
  • 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绿了

    本文发布于:2023-11-30 20:40:01,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1651411.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:转换为   摄像机   API   rgb

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!