Last active
December 12, 2018 05:08
-
-
Save naveen-aluri/1a81c8dbfcd30fbd1f8d02001e2db2bf to your computer and use it in GitHub Desktop.
Android image blur helper class.
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
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.renderscript.Allocation; | |
import android.renderscript.Element; | |
import android.renderscript.RenderScript; | |
import android.renderscript.ScriptIntrinsicBlur; | |
public class BlurBuilder { | |
private static final float BITMAP_SCALE = 0.4f; | |
private static final float BLUR_RADIUS = 20f; | |
public static Bitmap blur(Context context, Bitmap image) { | |
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 theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); | |
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap); | |
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap); | |
theIntrinsic.setRadius(BLUR_RADIUS); | |
theIntrinsic.setInput(tmpIn); | |
theIntrinsic.forEach(tmpOut); | |
tmpOut.copyTo(outputBitmap); | |
return outputBitmap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment