我希望从相机预览中获取YUV格式,将其更改为rgb字节数组,对其应用Renderscript滤镜(例如灰度级)并将其还原为YUV字节数组。
我已经介绍了第一部分,但我找不到将rgb字节数组转换回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);
现在它输出到Bitmap,但理想情况下会有一个与ScriptIntrinsicYuvToRGB相反的ScriptIntrinsicRGBToYuv renderscript对象/脚本。
感谢您的帮助!