Skip to content

Instantly share code, notes, and snippets.

@boxme
Created March 24, 2015 02:40
Show Gist options
  • Save boxme/5079826746987c000ef8 to your computer and use it in GitHub Desktop.
Save boxme/5079826746987c000ef8 to your computer and use it in GitHub Desktop.
Blurring an image using Renderscript
public static Bitmap blendRenderScript(RenderScript rs, Bitmap originalBitmap) {
// Creates a matching Renderscript allocation object and
// copies the contents of the bitmap into the allocation
Allocation input = Allocation.createFromBitmap(rs, originalBitmap,
Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
// Generates an Allocation identical in structure to the first
Allocation output = Allocation.createTyped(rs, input.getType());
// Uses Renderscript ScriptIntrinsicBlur, a Gaussian blur filter
ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
// Control the strength of the blur
script.setRadius(10f);
script.setInput(input);
// Blur
script.forEach(output);
// Copy the blurred image back to Java memory space
output.copyTo(originalBitmap);
return originalBitmap;
}
public static Bitmap getBitmapFromView(View v) {
Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
return bitmap;
}
public static Bitmap createScaledBitmapForBlurring(Bitmap originalBitmap) {
return Bitmap.createScaledBitmap(
originalBitmap,
originalBitmap.getWidth()/10,
originalBitmap.getHeight()/10, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment