如何使用ScriptIntrinsicYuvToRGB(将byte [] yuv转换为byte [] rgba)

wil*_*dev 9 android renderscript

我有byte[] yuvByteArray(540x360图像从Camera.PreviewCallback.onPreviewFrame方法中捕获并转储到assets/yuv.bin文件中).我想转换byte[] yuvbyte[] rgba数组,使用以下代码(基于LivePreview android示例).

但是我收到outBytes了在forEach之后填充零的rgba数组并将out分配复制到outBytes.我的代码出了什么问题?


package hellorender;        
import android.app.Activity; 
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.Element;
import android.support.v8.renderscript.RenderScript;
import android.support.v8.renderscript.ScriptIntrinsicYuvToRGB;
import android.support.v8.renderscript.Type;
import android.widget.ImageView;
import hellorender.R;

import java.io.IOException;
import java.io.InputStream;

public class HelloRenderActivity extends Activity {

    public static final int W = 540;
    public static final int H = 360;
    private RenderScript rs;
    private ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        AssetManager assets = getAssets();
        byte[] yuvByteArray = new byte[291600];
        byte[] outBytes = new byte[W * H * 4];

        InputStream is = null;
        try {
            is = assets.open("yuv.bin");
            System.out.println("read: " + is.read(yuvByteArray));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        ImageView iv = (ImageView) findViewById(R.id.image);
        rs = RenderScript.create(this);
        yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(rs));


        Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs))
                .setX(W).setY(H)
                .setYuvFormat(android.graphics.ImageFormat.NV21);
        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);

        out.copyTo(outBytes);

        Bitmap bmpout = Bitmap.createBitmap(W, H, Bitmap.Config.ARGB_8888);
        out.copyTo(bmpout);

        iv.setImageBitmap(bmpout);
    }

}
Run Code Online (Sandbox Code Playgroud)

wil*_*dev 11

yuv.bin文件绝对是NV21格式,因为它抓住了在这里http://developer.android.com/reference/android/hardware/Camera.PreviewCallback.html#onPreviewFrame

setYuvFormat方法来自API级别18,我删除了它

所以这段代码工作正常:

rs = RenderScript.create(this);
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);
Run Code Online (Sandbox Code Playgroud)

  • 与在纯 Java 中完成的 rutine 相比,这真是太快了!! (3认同)