Cjen1
Cjen1

Reputation: 1746

How to use an image as the Allocation for scripts

I'm trying to get rgb values from an image capture, I was trying to use the YuvToRGB intrinsic to get the rgb values (I have to capture in yuv for fastest capture time).

So far I have:

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);

What I can't work out is how to input the image from the imageReader into the allocateIn.

Upvotes: 0

Views: 725

Answers (2)

Settembrini
Settembrini

Reputation: 1396

I just used ScriptIntrinsicYuvToRGB in order to convert a preview picture of the old Camera (which is also in YUV) into an RGBA-Bitmap with the following code that works fine. data is the byte[] array you get from the capture. You need to handover the data to the in-Allocation with copyFrom and to handover the Output to a Bitmap with copyTo. previewWidth and previewHeight are the dimensions of the picture.

    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);

Upvotes: 1

Stephen Hines
Stephen Hines

Reputation: 2622

You can use http://developer.android.com/reference/android/renderscript/Allocation.html#createFromBitmap(android.renderscript.RenderScript, android.graphics.Bitmap) directly to create your input Allocation. Take a look at some sample code as well for using Allocation.copyFrom/Allocation.copyTo to actually do the proper copying.

Upvotes: 0

Related Questions