Last active
November 16, 2024 09:42
-
-
Save Hamidreza-mj/816852c415e0cbaf84d5a8954316ee91 to your computer and use it in GitHub Desktop.
A class for Blurring Images in android (Worked with Bitmap)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Helpers; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.drawable.BitmapDrawable; | |
import android.renderscript.Allocation; | |
import android.renderscript.Element; | |
import android.renderscript.RenderScript; | |
import android.renderscript.ScriptIntrinsicBlur; | |
import android.widget.ImageView; | |
import androidx.annotation.NonNull; | |
/** | |
* @author HamidReza-mj <br> | |
* <p> | |
* Main Source Link: https://ssaurel.medium.com/create-a-blur-effect-on-android-with-renderscript-aa05dae0bd7d <br> <br> | |
* <p> | |
* this class add blur effect to bitmap images | |
* <p> | |
* for use this, must be add `renderscriptSupportModeEnabled true` in to build.gradle (app level) in defaultConfig scope | |
* like this: | |
* <p> | |
* defaultConfig {<br> | |
* ....<br> | |
* renderscriptSupportModeEnabled true<br> | |
* } | |
* <p> | |
* for set this to image, call setImageBitmap and pass the argument bitmap to this | |
* | |
* <p> | |
* it is better to use in try catch block | |
*/ | |
public class BlurBuilder { | |
private static final float BITMAP_SCALE = 0.6f; | |
/** | |
* if need raduis > 25, must be call again & againg result bitmap with the blur method | or call this with level {@link #multipleBlur(Context, Bitmap, int)} | |
*/ | |
private static final float DEFAULT_RADIUS = 25f; | |
/** | |
* blur image bit map | |
* | |
* @param context | |
* @param image | |
* @return Bitmap for set the result blur to ImageView with `imageView.setImageBitmap(resultBitmap)` | |
*/ | |
public static Bitmap blur(Context context, Bitmap image) { | |
if (image == null) | |
return null; | |
int width = Math.round(image.getWidth() * BITMAP_SCALE); | |
int height = Math.round(image.getHeight() * BITMAP_SCALE); | |
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false); | |
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap); | |
RenderScript rs = RenderScript.create(context); | |
ScriptIntrinsicBlur intrinsicBlur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); | |
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap); | |
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap); | |
intrinsicBlur.setRadius(DEFAULT_RADIUS); | |
intrinsicBlur.setInput(tmpIn); | |
intrinsicBlur.forEach(tmpOut); | |
tmpOut.copyTo(outputBitmap); | |
return outputBitmap; | |
} | |
/** | |
* get Bitmap from ImageView to pass {@link #blur(Context, Bitmap)} | |
* | |
* @param image an instance of ImageView or childs | |
* @return Bitmap | |
*/ | |
public static Bitmap getBitmapFromImage(@NonNull ImageView image) { | |
return ((BitmapDrawable) image.getDrawable()).getBitmap(); | |
} | |
/** | |
* set multi level blur to image Bitmap | |
* | |
* @param context | |
* @param imageBitmap | |
* @param level this determines how many levels should be set filter blurred | |
* @return | |
*/ | |
public static Bitmap multipleBlur(Context context, Bitmap imageBitmap, int level) { | |
for (int i = 0; i < level; i++) | |
imageBitmap = blur(context, imageBitmap); | |
return imageBitmap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment