[英]How to live blur bitmap with renderscript?
我需要使用可以让用户控制模糊半径的SeekBar
来模糊图像。 我在下面使用此方法,但是由于SeekBar
值更改时每个函数调用都会创建新的位图,因此似乎浪费了内存和时间。 用RenderScript
实现实时模糊的最佳方法是什么?
public static Bitmap blur(Context ctx, Bitmap image, float blurRadius) {
int width = Math.round(image.getWidth() * BITMAP_SCALE);
int height = Math.round(image.getHeight() * BITMAP_SCALE);
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
Bitmap outputBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
RenderScript rs = RenderScript.create(ctx);
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
theIntrinsic.setRadius(blurRadius);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
rs.destroy();
if(inputBitmap!=outputBitmap)
inputBitmap.recycle();
return outputBitmap;
}
这些调用可能非常昂贵,实际上应该在应用程序的外部完成。 然后,可以在需要时重用RenderScript上下文/对象和ScriptIntrinsicBlur。 函数完成后,您也不应销毁它们(因为您将重用它们)。 为了节省更多,您可以将实际的输入/输出位图传递给例程(或它们的分配),并使它们保持稳定。 这个片段中确实有很多动态的创建/销毁操作,而且我可以想象其中的某些事情不会经常更改(因此不需要从头开始重新创建)。
...
RenderScript rs = RenderScript.create(ctx);
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.