Blur Image in Android
Posted By : Ankita Singh | 23-May-2016

Today I will tell you the use of renderscript library.A common way to blur a very beautiful image by using renderscript library.In java blurring and image effectively was a hassel since the beginning due to limitations of java,but renderscript offer a beautiful solution to it...
public class BlurBuilder {
private static final float BITMAP_SCALE = 0.4f;
private static final float BLUR_RADIUS = 7.5f;
public static Bitmap blur(Context context, Bitmap image) {
int mWidth = Math.round(image.getWidth() * BITMAP_SCALE);
int mHeight = Math.round(image.getHeight() * BITMAP_SCALE);
Bitmap givenBitmap = Bitmap.createScaledBitmap( image, mWidth, mHeight, false);
Bitmap takenBitmap = Bitmap.createBitmap( givenBitmap);
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation tmpIn = Allocation.createFromBitmap( rs, givenBitmap);
Allocation tmpOut = Allocation.createFromBitmap( rs, takenBitmap);
theIntrinsic.setRadius(BLUR_ RADIUS);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(takenBitmap);
return takenBitmap;
}
}
You can see there is one class with one static method,method takes bitmap as a parameter and gives a blurred image as a output.
If you want to blur more and more just adjust the parameter BLUR_RADIUS and BLUR_SCALE to increase the blur of the image.This is very fast and easy to use library.Make sure you have downloaded the support library of the renderscript for older version of the android so that it can run below API level 17 also.If you want to add the blur image as a background of the view then add the below given code...
Bitmap blurredBitmap = BlurBuilder.blur(getActivity(), originalBitmap);
imageView.setBackgroundDrawable(new BitmapDrawable( getResources(),blurredBitmap));
THANKS