Skip to content

Instantly share code, notes, and snippets.

@alxscms
Created February 9, 2015 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alxscms/ca68d3217fd3c9f09929 to your computer and use it in GitHub Desktop.
Save alxscms/ca68d3217fd3c9f09929 to your computer and use it in GitHub Desktop.
Picasso transformation that crops center the image to the specified dimensions and blurs it
public class BlurPicassoTransformation implements Transformation {
private Context mContext;
private int mWidth;
private int mHeight;
public BlurPicassoTransformation(Context context, int width, int height) {
mContext = context;
mWidth = width;
mHeight = height;
}
@Override
public Bitmap transform(Bitmap source) {
// Scale center crop bitmap
Bitmap scaledCenteredCroppedBitmap = scaleCenterCrop(source, mWidth, mHeight);
// Blur bitmap and return it
return blur(mContext, scaledCenteredCroppedBitmap, 25);
}
@Override
public String key() {
return "blur(width=" + mWidth + ",height=" + mHeight + ")";
}
private Bitmap scaleCenterCrop(Bitmap source, int newWidth, int newHeight) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
// Compute the scaling factors to fit the new height and width, respectively.
// To cover the final image, the final scaling will be the bigger
// of these two.
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.max(xScale, yScale);
// Now get the size of the source bitmap when scaled
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;
// Let's find out the upper left coordinates if the scaled bitmap
// should be centered in the new size give by the parameters
float left = (newWidth - scaledWidth) / 2;
float top = (newHeight - scaledHeight) / 2;
// The target rectangle for the new, scaled version of the source bitmap will now
// be
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
// Finally, we create a new bitmap of the specified size and draw our new,
// scaled bitmap onto it.
Bitmap outBitmap = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(outBitmap);
canvas.drawBitmap(source, null, targetRect, null);
// Recycle the original bitmap
source.recycle();
return outBitmap;
}
private Bitmap blur(Context context, Bitmap bitmap, float radius) {
// Let's create an empty bitmap with the same size of the bitmap we want to blur
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), android.graphics.Bitmap.Config.ARGB_8888);
// Instantiate a new Renderscript
RenderScript rs = RenderScript.create(context);
// Create an Intrinsic Blur Script using the Renderscript
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
// Create the in/out Allocations with the Renderscript and the in/out bitmaps
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
// Set the radius of the blur
blurScript.setRadius(radius);
// Perform the Renderscript
blurScript.setInput(allIn);
blurScript.forEach(allOut);
// Copy the final bitmap created by the out Allocation to the outBitmap
allOut.copyTo(outBitmap);
// recycle the original bitmap
bitmap.recycle();
// After finishing everything, we destroy the Renderscript.
rs.destroy();
return outBitmap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment