[英]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.