Skip to content

Instantly share code, notes, and snippets.

@root-ansh
Created June 4, 2019 13:36
Show Gist options
  • Save root-ansh/49483d9fe0529b8a35af1e28917dbd6a to your computer and use it in GitHub Desktop.
Save root-ansh/49483d9fe0529b8a35af1e28917dbd6a to your computer and use it in GitHub Desktop.
Delegation to system cam
private static Bitmap getBitmapFromUri(Context context, Uri photoURI) {
// super cool decoding algorithm at https://stackoverflow.com/a/31720143/7500651
try {
int MAX_HEIGHT = 1024;
int MAX_WIDTH = 1024;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
InputStream imageStream = context.getContentResolver().openInputStream(photoURI);
BitmapFactory.decodeStream(imageStream, null, options);
if (imageStream != null) {
imageStream.close();
}
options.inSampleSize = calculateInSampleSize(options, MAX_WIDTH, MAX_HEIGHT);
options.inJustDecodeBounds = false;
imageStream = context.getContentResolver().openInputStream(photoURI);
Bitmap img = BitmapFactory.decodeStream(imageStream, null, options);
img = rotateImageIfRequired(context, img, photoURI);
return img;
}
catch (IOException e){
Log.e(TAG, "getBitmapFromUri: Error happenned while decoding bitmap" );
e.printStackTrace();
return null;
}
/*or simpy use:*/
// InputStream ims = null;
// try {
// ims = getContentResolver().openInputStream(tmpPhotoURI);
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// Log.e(TAG, "getBitmapFromUri: coudn't decode the output");
// }
// Bitmap b = BitmapFactory.decodeStream(ims);
// return b;
/*or even better , directly use:*/
//Picasso.with(context).load("file:" + photoPath).into(imageView);
}
private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
final float totalPixels = width * height;
final float totalReqPixelsCap = reqWidth * reqHeight * 2;
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++;
}
}
return inSampleSize;
}
private static Bitmap rotateImageIfRequired(Context context, Bitmap img, Uri selectedImage) throws IOException {
InputStream input = context.getContentResolver().openInputStream(selectedImage);
ExifInterface ei;
if (Build.VERSION.SDK_INT > 23)
ei = new ExifInterface(input);
else
ei = new ExifInterface(selectedImage.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotateImage(img, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return rotateImage(img, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return rotateImage(img, 270);
default:
return img;
}
}
private static Bitmap rotateImage(Bitmap img, int degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
img.recycle();
return rotatedImg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment