Created
March 24, 2015 02:40
-
-
Save boxme/5079826746987c000ef8 to your computer and use it in GitHub Desktop.
Blurring an image using Renderscript
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
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; | |
} |
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
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; | |
} |
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
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