package com.recruit.app.util; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Point; import android.graphics.PorterDuffXfermode; import android.graphics.RectF; import android.widget.ImageView; import com.recruit.app.util.Ln; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import static android.graphics.Bitmap.Config.ARGB_8888; import static android.graphics.Color.WHITE; import static android.graphics.PorterDuff.Mode.DST_IN; /** * Image utilities */ public class ImageUtils { /** * Get a bitmap from the image path * * @param imagePath * @return bitmap or null if read fails */ public static Bitmap getBitmap(final String imagePath) { return getBitmap(imagePath, 1); } /** * Get a bitmap from the image path * * @param imagePath * @param sampleSize * @return bitmap or null if read fails */ public static Bitmap getBitmap(final String imagePath, int sampleSize) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inDither = false; options.inSampleSize = sampleSize; RandomAccessFile file = null; try { file = new RandomAccessFile(imagePath, "r"); return BitmapFactory.decodeFileDescriptor(file.getFD(), null, options); } catch (IOException e) { Ln.d(e, "Could not get cached bitmap."); return null; } finally { if (file != null) try { file.close(); } catch (IOException e) { Ln.d(e, "Could not get cached bitmap."); } } } /** * Get size of image * * @param imagePath * @return size */ public static Point getSize(final String imagePath) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; RandomAccessFile file = null; try { file = new RandomAccessFile(imagePath, "r"); BitmapFactory.decodeFileDescriptor(file.getFD(), null, options); return new Point(options.outWidth, options.outHeight); } catch (IOException e) { Ln.d(e, "Could not get size."); return null; } finally { if (file != null) try { file.close(); } catch (IOException e) { Ln.d(e, "Could not get size."); } } } /** * Get bitmap with maximum height or width * * @param imagePath * @param width * @param height * @return image */ public static Bitmap getBitmap(final String imagePath, int width, int height) { Point size = getSize(imagePath); int currWidth = size.x; int currHeight = size.y; int scale = 1; while (currWidth >= width || currHeight >= height) { currWidth /= 2; currHeight /= 2; scale *= 2; } return getBitmap(imagePath, scale); } /** * Get bitmap with maximum height or width * * @param image * @param width * @param height * @return image */ public static Bitmap getBitmap(final File image, int width, int height) { return getBitmap(image.getAbsolutePath(), width, height); } /** * Get a bitmap from the image file * * @param image * @return bitmap or null if read fails */ public static Bitmap getBitmap(final File image) { return getBitmap(image.getAbsolutePath()); } /** * Load a {@link android.graphics.Bitmap} from the given path and set it on the given * {@link android.widget.ImageView} * * @param imagePath * @param view */ public static void setImage(final String imagePath, final ImageView view) { setImage(new File(imagePath), view); } /** * Load a {@link android.graphics.Bitmap} from the given {@link java.io.File} and set it on the given * {@link android.widget.ImageView} * * @param image * @param view */ public static void setImage(final File image, final ImageView view) { Bitmap bitmap = getBitmap(image); if (bitmap != null) view.setImageBitmap(bitmap); } /** * Round the corners of a {@link android.graphics.Bitmap} * * @param source * @param radius * @return rounded corner bitmap */ public static Bitmap roundCorners(final Bitmap source, final float radius) { int width = source.getWidth(); int height = source.getHeight(); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(WHITE); Bitmap clipped = Bitmap.createBitmap(width, height, ARGB_8888); Canvas canvas = new Canvas(clipped); canvas.drawRoundRect(new RectF(0, 0, width, height), radius, radius, paint); paint.setXfermode(new PorterDuffXfermode(DST_IN)); Bitmap rounded = Bitmap.createBitmap(width, height, ARGB_8888); canvas = new Canvas(rounded); canvas.drawBitmap(source, 0, 0, null); canvas.drawBitmap(clipped, 0, 0, paint); source.recycle(); clipped.recycle(); return rounded; } }