Android 内置了 YUV 到 RGB 的转换功能,下面的代码适用于 NV21 YUV 输入但如果使用 NV12 输入,它会崩溃。
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;
}
如何更改代码以将 NV12 转换为 RGB?没有文档说明支持的输入格式是什么以及如何配置它。
最佳答案
ScriptIntrinsicYuvToRGB
上的 Android 文档明确指出:
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.
如果您需要不同的 YUV 格式,则必须编写自己的 RS 内核来进行转换。
关于android - 使用 RenderScript 将 YUV NV12 转换为 RGB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43623817/