【发布时间】:2017-04-26 02:06:36
【问题描述】:
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?没有文档说明支持的输入格式是什么以及如何配置。
【问题讨论】:
-
听起来有一个 api .setYuvFormat(ImageFormat.YUV_420_888) 但它会崩溃,说只支持 NV21 和 YV12。所以我想你需要为 NV12 编写一个脚本到 RGB,内置函数不适用于这种情况。
-
你可以试试这个link它帮我把YUV转RGB
标签: android renderscript