Skip to content

Instantly share code, notes, and snippets.

@fadelakin
Created May 12, 2015 18:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fadelakin/efc32c6c132cce1df713 to your computer and use it in GitHub Desktop.
Save fadelakin/efc32c6c132cce1df713 to your computer and use it in GitHub Desktop.
blurring bitmaps in android using renderscript
// blurring with renderscript requires API 17 and above
private 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(), Bitmap.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
if(bitmap!=outBitmap) {
bitmap.recycle();
bitmap = null;
}
//After finishing everything, we destroy the Renderscript.
rs.destroy();
return outBitmap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment