6

I have blurred a bitmap with Androids Rederscript

private Bitmap createBitmap_ScriptIntrinsicBlur(Bitmap src, float r) {

    //Radius range (0 < r <= 25)
    if(r <= 0){
        r = 0.1f;
    }else if(r > 25){
        r = 25.0f;
    }
    Bitmap bitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(),Bitmap.Config.ARGB_8888);

    RenderScript renderScript = RenderScript.create(getActivity());

    Allocation blurInput = Allocation.createFromBitmap(renderScript, src);
    Allocation blurOutput = Allocation.createFromBitmap(renderScript, bitmap);

    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(renderScript,Element.U8_4(renderScript));
    blur.setInput(blurInput);
    blur.setRadius(r);
    blur.forEach(blurOutput);

    blurOutput.copyTo(bitmap);
    renderScript.destroy();
    return bitmap;
}

But the image isn't blurred enough. Are there any possibilities to blur the image with a radius over 25. And when I call the blur function multiple times the image stays as blurred as a single function call.So that doesn't work either.
Thank you for your help.

RoterBaron
  • 103
  • 2
  • 10
  • If you call it a second time on your modified bitmap, what does it do ? – king Feb 21 '16 at 19:17
  • It's a bit strange, when I do it like this: Bitmap bitmap = ....my Bitmap; imageView.setImageBitmap(createRenderScriptBlur(createRenderScriptBlur(bitmap,25),25)); is operates like one call. But when I do it like this: Bitmap a =...myBitmap Bitmap b = createRenderScriptBlur(a,25); Bitmap c = createRenderScriptBlur(b,25); imageView.setImageBitmap(c); it works. – RoterBaron Feb 22 '16 at 14:28
  • Yeah I wanted to say more like Bitmap a = blur(mybitmap, 25); a = blur(a, 25); – king Feb 22 '16 at 14:51

2 Answers2

16
  1. Scale the image down by a factor of 4-8.
  2. Run blur with 25 px radius.
  3. Scale back up to the original size.
Miloslaw Smyk
  • 1,305
  • 10
  • 15
10

I know you said that running it multiple times doesn't work, but here's what I'm doing.

Bitmap inputBitmap = Bitmap.createScaledBitmap(artistImage, artistImage.getWidth(), artistImage.getHeight(), false);
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

RenderScript rs = RenderScript.create(context);

for (int i = 0; i < 10; i++){
    final Allocation input = Allocation.createFromBitmap(rs, outputBitmap); //use this constructor for best performance, because it uses USAGE_SHARED mode which reuses memory
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    script.setRadius(25F);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(outputBitmap);
}

You may have been running a blur on your normal source image multiple times, which obviously wouldn't accomplish anything. This works for me, and can blur it an incredible amount depending on what i is.

LukeWaggoner
  • 8,869
  • 1
  • 29
  • 28
  • This approach works fine. Just move `ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create` and `script.setRadius(25f);` outside the scope, if performance matters. – Volodymyr Kulyk Nov 16 '16 at 09:47