2017-04-26 490 views
1

Android有內置的YUV轉換到RGB的功能,下面的代碼對於NV21的YUV輸入工作正常,但如果使用NV12輸入,它會崩潰。安卓 - YUV NV12到RenderScript的RGB轉換

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的代碼?沒有文檔說什麼支持的輸入格式以及如何配置它。

+0

聽起來像有一個.setYuvFormat(ImageFormat.YUV_420_888),但它會崩潰,說只有NV21和YV12支持。所以我想你需要爲NV12編寫一個腳本到RGB,buildin函數不適用於這種情況。 – lucky1928

+0

可能相關:http://stackoverflow.com/questions/28620790/converting-specialized-nv12-video-frames-to-rgb –

回答

0

ScriptIntrinsicYuvToRGB的Android文檔明確指出:

輸入分配是在NV21格式作爲U8元素類型提供。輸出爲RGBA,alpha通道將設置爲255.

如果您需要不同的YUV格式,則必須編寫自己的RS內核來完成轉換。