Skip to content

Instantly share code, notes, and snippets.

@lucamtudor
Created July 2, 2013 10:14
Show Gist options
  • Save lucamtudor/5908180 to your computer and use it in GitHub Desktop.
Save lucamtudor/5908180 to your computer and use it in GitHub Desktop.
ImageHelper that has the following features: - getRoundedCornerBitmap() : Create a round shape cropped Bitmap from an existing Bitmap. - decodeUri() : Obtains an image at a specified Uri.
/**
* @author Tudor Luca
*/
public class ImageHelper {
public static class Options {
/**
* If not set, the source Bitmap width will be used.
*/
public int bitmapWidth;
/**
* If not set, the source Bitmap height will be used.
*/
public int bitmapHeight;
/**
* The border of the image, in pixels.
*/
public int borderWidth = 0;
/**
* The border color of the image. Default color is WHITE.
*/
public int borderColor = Color.WHITE;
/**
* Border shadow X offset.
*/
public float borderShadowDx = 0;
/**
* Border shadow Y offset.
*/
public float borderShadowDy = 0;
/**
* If shadow radius = 0, then the shadow will be removed.
*/
public float borderShadowRadius = 0;
/**
* Color for border shadow. Default color is BLACK.
*/
public int borderShadowColor = Color.BLACK;
/**
* The radius used to round the corners of the image.
*/
public float cornerRadius = 10;
/**
* If <b><code>true</code></b>, the image will be a circle. Also {@link #cornerRadius} will be ignored.
*/
public boolean inCircle = false;
public Options(int bitmapWidth, int bitmapHeight, boolean inCircle) {
super();
this.bitmapWidth = bitmapWidth;
this.bitmapHeight = bitmapHeight;
this.inCircle = inCircle;
if (inCircle)
cornerRadius = bitmapWidth / 2;
}
}
/**
* Obtains an image at a specified {@link Uri}.
*
* @param context - it is needed to open the InputStream to get the image
* @param imgUri - the {@link Uri} where the picture is located
* @param width - width at which the picture will be scaled
* @param height - height at which the picture will be scaled
* @return {@link Bitmap} which contains the picture
* @throws FileNotFoundException if the {@link Uri} imgUri is broken or there is no picture with the specified
* {@link Uri}
*/
public static Bitmap decodeUri(Context context, Uri imgUri, int width, int height) throws FileNotFoundException {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imgUri), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE_W = width;
final int REQUIRED_SIZE_H = height;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE_W || height_tmp / 2 < REQUIRED_SIZE_H) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
// #CODE
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imgUri), null, o2);
}
/**
* Create a round shape cropped Bitmap from an existing Bitmap.
*
* @param source - the original Bitmap that will be transformed.
* @param options - {@link Options} used to transform the source Bitmap.
* @return rounded corners {@link Bitmap}.
*/
public static Bitmap getRoundedCornerBitmap(Bitmap source, ImageHelper.Options options) {
// #CODE
if (options == null)
options = new Options(source.getWidth(), source.getHeight(), false);
Bitmap roundedBitmap = Bitmap.createBitmap(options.bitmapWidth, options.bitmapHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(roundedBitmap);
final Paint bitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
final Paint borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
borderPaint.setStyle(Style.STROKE);
borderPaint.setColor(options.borderColor);
borderPaint.setStrokeWidth(options.borderWidth);
borderPaint.setShadowLayer(options.borderShadowRadius, options.borderShadowDx, options.borderShadowDy,
Color.BLACK);
final Rect srcRect = new Rect(0, 0, source.getWidth(), source.getHeight());
final Rect rndRect = new Rect(0, 0, options.bitmapWidth, options.bitmapHeight);
final RectF borderRect = new RectF(options.borderWidth / 2 + options.borderShadowRadius, options.borderWidth / 2
+ options.borderShadowRadius,
options.bitmapWidth - options.borderWidth / 2 - options.borderShadowRadius, options.bitmapHeight
- options.borderWidth / 2 - options.borderShadowRadius);
canvas.drawARGB(0, 0, 0, 0); // Draw the background
canvas.drawRoundRect(borderRect, options.cornerRadius, options.cornerRadius, bitmapPaint); // Draw
// the
// rounded
// shape
// where
// the
// source
// bitmap
// will
// be
// drawn
bitmapPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(source, srcRect, rndRect, bitmapPaint); // Draw the
// source
// bitmap
if (options.borderWidth > 0)
canvas.drawRoundRect(borderRect, options.cornerRadius, options.cornerRadius, borderPaint); // Draw
// the
// border,
// if
// any
return roundedBitmap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment