如何使用图像作为脚本的分配

How to use an image as the Allocation for scripts

我正在尝试从图像捕获中获取 rgb 值,我正在尝试使用 YuvToRGB 内在函数来获取 rgb 值(我必须在 yuv 中捕获以获得最快的捕获时间)。

到目前为止我有:

RenderScript rs = RenderScript.Create(this);
Android.Renderscripts.Type.Builder tb = new Android.Renderscripts.Type.Builder(rs, Element.RGBA_8888);
tb.SetX(imageSheaf[i].Width);
tb.SetY(imageSheaf[i].Height);

Android.Renderscripts.Type t = tb.Create();
Allocation AllocateOut = Allocation.CreateTyped(rs, t, AllocationUsage.Script | AllocationUsage.IoOutput);


tb = new Android.Renderscripts.Type.Builder(rs, Element.CreatePixel(rs, Element.DataType.Unsigned8, Element.DataKind.PixelYuv));
tb.SetX(imageSheaf[i].Width);
tb.SetY(imageSheaf[i].Height);
tb.SetYuvFormat((int)Android.Graphics.ImageFormatType.Yuv420888);
Allocation allocateIn = Allocation.CreateTyped(rs, tb.Create(), AllocationUsage.Script);

ScriptIntrinsicYuvToRGB script = ScriptIntrinsicYuvToRGB.Create(rs, Element.RGBA_8888);

script.SetInput(allocateIn);

我无法解决的是如何将图像从 imageReader 输入到 allocateIn

您可以直接使用 http://developer.android.com/reference/android/renderscript/Allocation.html#createFromBitmap(android.renderscript.RenderScript、android.graphics.Bitmap) 来创建您的输入分配。查看一些示例代码以及使用 Allocation.copyFrom/Allocation.copyTo 实际执行正确的复制。

我刚刚使用 ScriptIntrinsicYuvToRGB 将旧相机(也是 YUV 格式)的预览图片转换为 RGBA-Bitmap,代码如下,效果很好。 data 是您从捕获中获得的 byte[] 数组。您需要使用 copyFrom 将数据移交给 in-Allocation,并使用 copyTo 将 Output 移交给 Bitmap。 previewWidth 和 previewHeight 是图片的尺寸。

    Bitmap bmpfoto1 = Bitmap.createBitmap(previewWidth, previewHeight,   Bitmap.Config.ARGB_8888);
    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(data.length);
    Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
    in.copyFrom(data);

    Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(previewWidth).setY(previewHeight);
    Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);

    yuvToRgbIntrinsic.setInput(in);
    yuvToRgbIntrinsic.forEach(out);

    out.copyTo(bmpfoto1);