Android convert ImageReader Image to YCbCr_420_SP (NV21) byte array using render script?

走远了吗. 提交于 2019-11-30 06:36:17
@TargetApi(19) public static byte[] yuvImageToByteArray(Image image) {      assert(image.getFormat() == ImageFormat.YUV_420_888);      int width = image.getWidth();     int height = image.getHeight();      Image.Plane[] planes = image.getPlanes();     byte[] result = new byte[width * height * 3 / 2];      int stride = planes[0].getRowStride();     assert (1 == planes[0].getPixelStride());     if (stride == width) {         planes[0].getBuffer().get(result, 0, width*height);     }     else {         for (int row = 0; row < height; row++) {             planes[0].getBuffer().position(row*stride);             planes[0].getBuffer().get(result, row*width, width);         }     }      stride = planes[1].getRowStride();     assert (stride == planes[2].getRowStride());     int pixelStride = planes[1].getPixelStride();     assert (pixelStride == planes[2].getPixelStride());     byte[] rowBytesCb = new byte[stride];     byte[] rowBytesCr = new byte[stride];      for (int row = 0; row < height/2; row++) {         int rowOffset = width*height + width/2 * row;         planes[1].getBuffer().position(row*stride);         planes[1].getBuffer().get(rowBytesCb);         planes[2].getBuffer().position(row*stride);         planes[2].getBuffer().get(rowBytesCr);          for (int col = 0; col < width/2; col++) {             result[rowOffset + col*2] = rowBytesCr[col*pixelStride];             result[rowOffset + col*2 + 1] = rowBytesCb[col*pixelStride];         }     }     return result; } 

I have published another function with similar requirements. That new implementation tries to take advantage of the fact that quite often, YUV_420_888 is only NV21 in disguise.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!