Skip to content

Commit

Permalink
fix: correctly handle orientation on android (#2058)
Browse files Browse the repository at this point in the history
* fix: android: use image orientation when computing the image width/height
* android: swap dimension also when doing the resize
  • Loading branch information
GiovanniVisentiniCasavo committed Oct 23, 2023
1 parent 5c88725 commit 13cdc7c
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions android/src/main/java/com/imagepicker/Utils.java
Expand Up @@ -156,10 +156,18 @@ public static void setFrontCamera(Intent intent) {

public static int[] getImageDimensions(Uri uri, Context reactContext) {
try (InputStream inputStream = reactContext.getContentResolver().openInputStream(uri)) {

String orientation = getOrientation(uri,reactContext);

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);
return new int[]{options.outWidth, options.outHeight};
if (needToSwapDimension(orientation)) {
return new int[]{options.outHeight, options.outWidth};
}else {
return new int[]{options.outWidth, options.outHeight};
}

} catch (IOException e) {
e.printStackTrace();
return new int[]{0, 0};
Expand All @@ -168,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 @@ -189,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 @@ -204,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 13cdc7c

Please sign in to comment.