4

I'm looking to get a YUV format from the camera preview, change it to an rgb byte array, apply a Renderscript filter to it (for example a Grayscale one) and revert it back to a YUV byte array.

I have the first part covered, but I can't find an efficient way to convert back the rgb byte array to YUV.

outputBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// Create an allocation that corresponds to the outputBitmap
allocationOut = Allocation.createFromBitmap(renderScript, outputBitmap);

// allocationIn matches the allocationOut
allocationIn = Allocation.createTyped(renderScript, allocationOut.getType(),
android.renderscript.Allocation.USAGE_SCRIPT);

greyScaleMatrix = ScriptIntrinsicColorMatrix.create(renderScript, allocationOut.getElement());
greyScaleMatrix.setGreyscale();

Type.Builder typeYUV = new Type.Builder(renderScript,
        Element.createPixel(renderScript, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV));
typeYUV.setYuvFormat(ImageFormat.NV21);

// allocation for the YUV input from the camera
allocationYUV = Allocation.createTyped(renderScript, typeYUV.setX(width).setY(height).create(),
    android.renderscript.Allocation.USAGE_SCRIPT);
allocationYUV.copyFrom(dataIn);

// Create the instance for the YuvToRGB
scriptIntrinsicYuvToRGB = ScriptIntrinsicYuvToRGB.create(renderScript, Element.U8_4(renderScript));

scriptIntrinsicYuvToRGB.setInput(allocationYUV);
scriptIntrinsicYuvToRGB.forEach(allocationIn);

// Apply Grayscale
greyScaleMatrix.forEach(allocationIn, allocationOut);

allocationOut.copyTo(outputBitmap);

Right now it outputs to a Bitmap, but ideally there would be a ScriptIntrinsicRGBToYuv renderscript object / script that would to the opposite of ScriptIntrinsicYuvToRGB.

Thanks for your help!

Tiago
  • 71
  • 1
  • 5

0 Answers0