diff --git a/app/build.gradle b/app/build.gradle index a9d457926..8050ee39e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -104,7 +104,7 @@ dependencies { implementation 'com.github.stom79:horizontalbargraph:1.5' implementation 'jp.wasabeef:glide-transformations:4.0.0' playstoreImplementation "io.github.kobakei:ratethisapp:$ratethisappLibraryVersion" - implementation 'ja.burhanrashid52:photoeditor:0.3.3' + implementation 'ja.burhanrashid52:photoeditor:0.4.0' implementation 'com.github.yalantis:ucrop:2.2.3' implementation "net.gotev:uploadservice:$uploadServiceVersion" diff --git a/app/src/main/java/app/fedilab/android/activities/PhotoEditorActivity.java b/app/src/main/java/app/fedilab/android/activities/PhotoEditorActivity.java index 60657f7ed..408bbdd33 100644 --- a/app/src/main/java/app/fedilab/android/activities/PhotoEditorActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/PhotoEditorActivity.java @@ -18,9 +18,10 @@ import android.annotation.SuppressLint; import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; -import android.os.Environment; import androidx.annotation.NonNull; +import androidx.exifinterface.media.ExifInterface; import androidx.localbroadcastmanager.content.LocalBroadcastManager; + import android.view.Window; @@ -63,6 +64,7 @@ import com.yalantis.ucrop.UCrop; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; @@ -170,6 +172,18 @@ public class PhotoEditorActivity extends BaseActivity implements OnPhotoEditorL mPhotoEditorView.getSource().setImageURI(uri); + if( uri != null ) { + try (InputStream inputStream = getContentResolver().openInputStream(uri)) { + assert inputStream != null; + ExifInterface exif = new ExifInterface(inputStream); + int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); + int rotationInDegrees = exifToDegrees(rotation); + mPhotoEditorView.getSource().setRotation(rotationInDegrees); + } catch (Exception e) { + e.printStackTrace(); + } + } + Button send = findViewById(R.id.send); send.setOnClickListener(new View.OnClickListener() { @@ -181,6 +195,13 @@ public class PhotoEditorActivity extends BaseActivity implements OnPhotoEditorL }); } + private static int exifToDegrees(int exifOrientation) { + if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; } + else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; } + else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; } + return 0; + } + private void initViews() { ImageView imgUndo; ImageView imgRedo; @@ -232,9 +253,6 @@ public class PhotoEditorActivity extends BaseActivity implements OnPhotoEditorL public void onAddViewListener(ViewType viewType, int numberOfAddedViews) { } - @Override - public void onRemoveViewListener(int numberOfAddedViews) { - } @Override public void onRemoveViewListener(ViewType viewType, int numberOfAddedViews) { @@ -331,11 +349,25 @@ public class PhotoEditorActivity extends BaseActivity implements OnPhotoEditorL protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { + ExifInterface exif = null; + int rotation = 0; + int rotationInDegrees = 0; + if( data != null && data.getData() != null) { + try (InputStream inputStream = getContentResolver().openInputStream(data.getData())) { + assert inputStream != null; + exif = new androidx.exifinterface.media.ExifInterface(inputStream); + rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); + rotationInDegrees = exifToDegrees(rotation); + } catch (Exception e) { + e.printStackTrace(); + } + } switch (requestCode) { case CAMERA_REQUEST: mPhotoEditor.clearAllViews(); Bitmap photo = (Bitmap) data.getExtras().get("data"); mPhotoEditorView.getSource().setImageBitmap(photo); + mPhotoEditorView.getSource().setRotation(rotationInDegrees); break; case PICK_REQUEST: try { @@ -343,6 +375,7 @@ public class PhotoEditorActivity extends BaseActivity implements OnPhotoEditorL Uri uri = data.getData(); Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri); mPhotoEditorView.getSource().setImageBitmap(bitmap); + mPhotoEditorView.getSource().setRotation(rotationInDegrees); } catch (IOException e) { e.printStackTrace(); } @@ -351,6 +384,7 @@ public class PhotoEditorActivity extends BaseActivity implements OnPhotoEditorL final Uri resultUri = UCrop.getOutput(data); if( resultUri != null) { mPhotoEditorView.getSource().setImageURI(resultUri); + mPhotoEditorView.getSource().setRotation(rotationInDegrees); File fdelete = new File(uri.getPath()); if (fdelete.exists()) { fdelete.delete();