Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
private static WeakReference<RenderScript> rsRef = new WeakReference<>(null);
private static WeakReference<ScriptIntrinsicBlur> blurScriptRef = new WeakReference<>(null);
/**
* Use {@link RenderScript} to blur bitmap.
*
* @param bitmap to be blurred
* @param radius of blur
* @throws RSRuntimeException when some .so libs are not available (e.g. on Genymotion emulator)
*/
public static synchronized void applyBlur(Context context, Bitmap bitmap, Float radius) throws RSRuntimeException {
RenderScript rs = rsRef.get();
if (rs == null) {
rs = RenderScript.create(context.getApplicationContext());
rsRef = new WeakReference<>(rs);
}
//this will blur the bitmap with a provided radius and save it in bitmap
final Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT | Allocation.USAGE_SHARED);
final Allocation output = Allocation.createTyped(rs, input.getType());
ScriptIntrinsicBlur blurScript = blurScriptRef.get();
if (blurScript == null) {
blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
blurScriptRef = new WeakReference<>(blurScript);
}
blurScript.setRadius(radius);
blurScript.setInput(input);
blurScript.forEach(output);
output.copyTo(bitmap);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment