2

Android has buildin convert function for YUV to RGB, below code works fine for NV21 YUV input but if use NV12 input, it will crash.

public Bitmap YUV_toRGB(byte[] yuvByteArray,int W,int H) {
    RenderScript rs = RenderScript.create(this);
    ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));

    Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(yuvByteArray.length);
    Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);

    Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(W).setY(H);
    Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);

    in.copyFrom(yuvByteArray);

    yuvToRgbIntrinsic.setInput(in);
    yuvToRgbIntrinsic.forEach(out);
    Bitmap bmp = Bitmap.createBitmap(W, H, Bitmap.Config.ARGB_8888);
    out.copyTo(bmp);

    yuvToRgbIntrinsic.destroy();
    rs.destroy();
    return bmp;
}

How can I change the code to convert NV12 to RGB? No document say what's the input format supported and how to config it.

beetlej
  • 1,841
  • 4
  • 13
  • 27
  • Sounds like has a api .setYuvFormat(ImageFormat.YUV_420_888) but it will crash which says only NV21 and YV12 supported. so I guess you need to write a script for NV12 to RGB, the buildin function doesn't work for this case. – lucky1928 Apr 26 '17 at 02:14
  • Possibly related: http://stackoverflow.com/questions/28620790/converting-specialized-nv12-video-frames-to-rgb – Morrison Chang Apr 26 '17 at 04:12
  • You can try this [link](https://stackoverflow.com/questions/47498843/incorrect-image-converting-yuv-420-888-into-bitmaps-under-android-camera2/47601824#47601824) it help me convert YUV to RGB – Chung Yau Jun 20 '19 at 09:24

1 Answers1

1

The Android documentation on ScriptIntrinsicYuvToRGB clearly states:

The input allocation is supplied in NV21 format as a U8 element type. The output is RGBA, the alpha channel will be set to 255.

If you need a different YUV format, you'll have to write your own RS kernel to do the conversion.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33