From d90d25d25f835a3cd54a55ac407496bd029e7510 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 27 Mar 2019 11:14:09 +0000 Subject: [PATCH] Auto rotate inline images --- app/build.gradle | 4 ++ .../eu/faircode/email/FragmentCompose.java | 51 ++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index a9c45a2277..bb16e9a72e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -112,6 +112,7 @@ dependencies { def paging_version = "2.1.0" def preference_version = "1.0.0" def work_version = "1.0.0" + def exif_version = "1.0.0" def billingclient_version = "1.2.2" def javamail_version = "1.6.3" def jsoup_version = "1.11.3" @@ -158,6 +159,9 @@ dependencies { // https://mvnrepository.com/artifact/android.arch.work/work-runtime implementation "android.arch.work:work-runtime:$work_version" + // https://mvnrepository.com/artifact/androidx.exifinterface/exifinterface + implementation "androidx.exifinterface:exifinterface:$exif_version" + // https://developer.android.com/google/play/billing/billing_library_releases_notes implementation "com.android.billingclient:billing:$billingclient_version" diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 5447252db8..16af2880d2 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -34,6 +34,7 @@ import android.content.res.Resources; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.Matrix; import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.graphics.drawable.LevelListDrawable; @@ -123,6 +124,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.constraintlayout.widget.Group; import androidx.cursoradapter.widget.SimpleCursorAdapter; +import androidx.exifinterface.media.ExifInterface; import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Observer; @@ -1549,13 +1551,21 @@ public class FragmentCompose extends FragmentBase { options.outHeight / factor > REDUCED_IMAGE_SIZE) factor *= 2; - if (factor > 1) { + Matrix rotation = getImageRotation(file); + + if (factor > 1 || rotation != null) { options.inJustDecodeBounds = false; options.inSampleSize = factor; Bitmap scaled = BitmapFactory.decodeFile(file.getAbsolutePath(), options); if (scaled != null) { - Log.i("Image target size=" + scaled.getWidth() + "x" + scaled.getHeight()); + Log.i("Image target size=" + scaled.getWidth() + "x" + scaled.getHeight() + " rotation=" + rotation); + + if (rotation != null) { + Bitmap rotated = Bitmap.createBitmap(scaled, 0, 0, scaled.getWidth(), scaled.getHeight(), rotation, true); + scaled.recycle(); + scaled = rotated; + } try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) { scaled.compress("image/jpeg".equals(attachment.type) @@ -1583,6 +1593,43 @@ public class FragmentCompose extends FragmentBase { return attachment; } + private static Matrix getImageRotation(File file) throws IOException { + ExifInterface exif = new ExifInterface(file.getAbsolutePath()); + int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); + + Matrix matrix = new Matrix(); + switch (orientation) { + case ExifInterface.ORIENTATION_NORMAL: + return null; + case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: + matrix.setScale(-1, 1); + return matrix; + case ExifInterface.ORIENTATION_FLIP_VERTICAL: + matrix.setRotate(180); + matrix.postScale(-1, 1); + return matrix; + case ExifInterface.ORIENTATION_TRANSPOSE: + matrix.setRotate(90); + matrix.postScale(-1, 1); + return matrix; + case ExifInterface.ORIENTATION_TRANSVERSE: + matrix.setRotate(-90); + matrix.postScale(-1, 1); + return matrix; + case ExifInterface.ORIENTATION_ROTATE_90: + matrix.setRotate(90); + return matrix; + case ExifInterface.ORIENTATION_ROTATE_180: + matrix.setRotate(180); + return matrix; + case ExifInterface.ORIENTATION_ROTATE_270: + matrix.setRotate(-90); + return matrix; + default: + return null; + } + } + private SimpleTask draftLoader = new SimpleTask() { @Override protected EntityMessage onExecute(Context context, Bundle args) throws IOException {