简体   繁体   English

如何使用renderscript动态模糊位图?

[英]How to live blur bitmap with renderscript?

I need to blur image with a SeekBar that lets user to control radius of the blur. 我需要使用可以让用户控制模糊半径的SeekBar来模糊图像。 I use this method below, however it seems wasting memory and time because of creating new bitmaps every function call when SeekBar value changed. 我在下面使用此方法,但是由于SeekBar值更改时每个函数调用都会创建新的位图,因此似乎浪费了内存和时间。 What is the best approach for live blur implementation with RenderScript ? 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;
}

These calls can be quite expensive and really should be done in an outer part of your application. 这些调用可能非常昂贵,实际上应该在应用程序的外部完成。 You can then reuse the RenderScript context/object and ScriptIntrinsicBlur when you need them. 然后,可以在需要时重用RenderScript上下文/对象和ScriptIntrinsicBlur。 You also should not destroy them when the function finishes (since you will be reusing them). 函数完成后,您也不应销毁它们(因为您将重用它们)。 For greater savings, you can pass the actual input/output bitmaps to your routine (or their Allocations) and keep those steady too. 为了节省更多,您可以将实际的输入/输出位图传递给例程(或它们的分配),并使它们保持稳定。 There really is a lot of dynamic creation/destruction in this snippet, and I can imagine that some of these things don't change frequently (and thus don't need to keep being recreated from scratch). 这个片段中确实有很多动态的创建/销毁操作,而且我可以想象其中的某些事情不会经常更改(因此不需要从头开始重新创建)。

...
RenderScript rs = RenderScript.create(ctx);
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs,  Element.U8_4(rs));
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM