content: package com.adamstyrc.cookiecutter;
  
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.Canvas;
  import android.graphics.Paint;
  import android.graphics.Point;
  import android.graphics.PorterDuff;
  import android.graphics.PorterDuffXfermode;
  import android.graphics.Rect;
  import android.net.Uri;
  import android.view.Display;
  import android.view.WindowManager;
  
  import java.io.FileNotFoundException;
  
  /**
   * Created by adamstyrc on 04/04/16.
   */
  public class ImageUtils {
  
      public static Bitmap getCircularBitmap(Bitmap bitmap) {
          Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                  bitmap.getHeight(), Bitmap.Config.ARGB_8888);
          Canvas canvas = new Canvas(output);
  
          final int color = 0xff424242;
          final Paint paint = new Paint();
          final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
  
          paint.setAntiAlias(true);
          canvas.drawARGB(0, 0, 0, 0);
          paint.setColor(color);
          // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
          canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
                  bitmap.getWidth() / 2, paint);
          paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
          canvas.drawBitmap(bitmap, rect, rect, paint);
          //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
          //return _bmp;
          return output;
      }
  
      public static Point getScreenSize(Context context) {
          WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
          Display display = windowManager.getDefaultDisplay();
          Point size = new Point();
          display.getSize(size);
          return size;
      }
  
      public static Bitmap decodeUriToScaledBitmap(Context context, Uri uri, int reqWidth, int reqHeight)
              throws FileNotFoundException {
          BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = true;
          BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri), null, options);
  
          // Calculate inSampleSize
          options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  
          // Decode bitmap with inSampleSize set
          options.inJustDecodeBounds = false;
          return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri), null, options);
      }
  
      public static int calculateInSampleSize(
              BitmapFactory.Options options, int reqWidth, int reqHeight) {
          // Raw height and width of image
          final int height = options.outHeight;
          final int width = options.outWidth;
          int inSampleSize = 1;
  
          if (height / 2> reqHeight || width > reqWidth) {
  
              final int halfHeight = height / 2;
              final int halfWidth = width / 2;
  
              // Calculate the largest inSampleSize value that is a power of 2 and keeps both
              // height and width larger than the requested height and width.
              while ((halfHeight / inSampleSize) > reqHeight
                      && (halfWidth / inSampleSize) > reqWidth) {
                  inSampleSize *= 2;
              }
          }
  
          return inSampleSize;
      }
  }
  