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
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ankita Singh
Ankita is an Android Application Developer. She has experience in developing android application by using Java and xml.