Skip to content

Commit

Permalink
android: swap dimension also when doing the resize
Browse files Browse the repository at this point in the history
  • Loading branch information
GiovanniVisentiniCasavo committed Sep 16, 2023
1 parent 10c8e21 commit 10b09e3
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions android/src/main/java/com/imagepicker/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,7 @@ public static int[] getImageDimensions(Uri uri, Context reactContext) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);

if (orientation.equals(String.valueOf(ExifInterface.ORIENTATION_ROTATE_90))
|| orientation.equals(String.valueOf(ExifInterface.ORIENTATION_ROTATE_270))) {
if (needToSwapDimension(orientation)) {
return new int[]{options.outHeight, options.outWidth};
}else {
return new int[]{options.outWidth, options.outHeight};
Expand All @@ -178,7 +176,7 @@ public static int[] getImageDimensions(Uri uri, Context reactContext) {

static boolean hasPermission(final Activity activity) {
final int writePermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
return writePermission == PackageManager.PERMISSION_GRANTED ? true : false;
return writePermission == PackageManager.PERMISSION_GRANTED;
}

static String getBase64String(Uri uri, Context reactContext) {
Expand All @@ -199,6 +197,11 @@ static String getBase64String(Uri uri, Context reactContext) {
}
}

private static boolean needToSwapDimension(String orientation){
return orientation.equals(String.valueOf(ExifInterface.ORIENTATION_ROTATE_90))
|| orientation.equals(String.valueOf(ExifInterface.ORIENTATION_ROTATE_270));
}

// Resize image
// When decoding a jpg to bitmap all exif meta data will be lost, so make sure to copy orientation exif to new file else image might have wrong orientations
public static Uri resizeImage(Uri uri, Context context, Options options) {
Expand All @@ -214,10 +217,14 @@ public static Uri resizeImage(Uri uri, Context context, Options options) {
try (InputStream imageStream = context.getContentResolver().openInputStream(uri)) {
String mimeType = getMimeType(uri, context);
Bitmap b = BitmapFactory.decodeStream(imageStream);

b = Bitmap.createScaledBitmap(b, newDimens[0], newDimens[1], true);
String originalOrientation = getOrientation(uri, context);

if (needToSwapDimension(originalOrientation)) {
b = Bitmap.createScaledBitmap(b, newDimens[1], newDimens[0], true);
}else {
b = Bitmap.createScaledBitmap(b, newDimens[0], newDimens[1], true);
}

File file = createFile(context, getFileTypeFromMime(mimeType));

try (OutputStream os = context.getContentResolver().openOutputStream(Uri.fromFile(file))) {
Expand Down

0 comments on commit 10b09e3

Please sign in to comment.