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?没有文档说明支持什么输入格式,以及如何配置输入格式。
发布于 2017-04-26 10:41:51
ScriptIntrinsicYuvToRGB上的Android文档明确指出:
输入分配以NV21格式作为U8元素类型提供。输出为RGBA,alpha通道将设置为255。
如果您需要一个不同的YUV格式,您将不得不编写您自己的RS内核来进行转换。
https://stackoverflow.com/questions/43623817
复制相似问题