Skip to content

Instantly share code, notes, and snippets.

@Mariuxtheone
Created May 24, 2014 14:02
Show Gist options
  • Star 70 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save Mariuxtheone/903c35b4927c0df18cf8 to your computer and use it in GitHub Desktop.
Save Mariuxtheone/903c35b4927c0df18cf8 to your computer and use it in GitHub Desktop.
public Bitmap blurBitmap(Bitmap bitmap){
//Let's create an empty bitmap with the same size of the bitmap we want to blur
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
//Instantiate a new Renderscript
RenderScript rs = RenderScript.create(getApplicationContext());
//Create an Intrinsic Blur Script using the Renderscript
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
//Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
//Set the radius of the blur
blurScript.setRadius(25.f);
//Perform the Renderscript
blurScript.setInput(allIn);
blurScript.forEach(allOut);
//Copy the final bitmap created by the out Allocation to the outBitmap
allOut.copyTo(outBitmap);
//recycle the original bitmap
bitmap.recycle();
//After finishing everything, we destroy the Renderscript.
rs.destroy();
return outBitmap;
}
@Mariuxtheone
Copy link
Author

This is a simple method you can use to apply a subtle Blur to bitmaps. This is done using Renderscript Intrinsics, a built-in function of Renderscript you can use out-of-the-box to achieve complex image manipulation like blur or matrix convolution.

More infos about Intrinsics here: http://android-developers.blogspot.it/2013/08/renderscript-intrinsics.html

@cbedoy
Copy link

cbedoy commented Oct 9, 2014

But this code only allow with api level 11....

@anugotta
Copy link

This supports API level 17 and Above.....

@prasad456
Copy link

Hi,

I am getting two errors.

  1. The method U8_4(RenderScript) is undefined for the type Element.
  2. ARGB_8888 cannot be resolved or is not a field

@dka09
Copy link

dka09 commented May 20, 2016

No errors here, seems to be fine. Only checked on API23, Android Studio 2.1.1
Can this be used to blur part of an image? Like the example you show here: https://plus.google.com/+MarioViviani/posts/fhuzYkji9zz

@zeroarst
Copy link

I wonder in the future if we can use radius greater than 25. Currently use loop to handle it but the performance is not good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment