diff options
Diffstat (limited to 'app/src/main/java/app/fedilab/android/activities/PhotoEditorActivity.java')
-rw-r--r-- | app/src/main/java/app/fedilab/android/activities/PhotoEditorActivity.java | 94 |
1 files changed, 65 insertions, 29 deletions
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..76588895d 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,11 @@ 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; @@ -45,6 +47,7 @@ import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Typeface; import android.provider.MediaStore; + import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.ConstraintSet; import androidx.transition.ChangeBounds; @@ -52,6 +55,7 @@ import androidx.transition.TransitionManager; import androidx.appcompat.app.AlertDialog; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; + import android.view.View; import android.view.animation.AnticipateOvershootInterpolator; import android.widget.Button; @@ -59,10 +63,11 @@ import android.widget.ImageView; import android.widget.TextView; -import com.yalantis.ucrop.UCrop; +import com.theartofdev.edmodo.cropper.CropImage; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; @@ -72,7 +77,7 @@ import ja.burhanrashid52.photoeditor.SaveSettings; import ja.burhanrashid52.photoeditor.ViewType; -public class PhotoEditorActivity extends BaseActivity implements OnPhotoEditorListener, +public class PhotoEditorActivity extends BaseActivity implements OnPhotoEditorListener, View.OnClickListener, PropertiesBSFragment.Properties, EmojiBSFragment.EmojiListener, @@ -105,7 +110,7 @@ public class PhotoEditorActivity extends BaseActivity implements OnPhotoEditorL requestWindowFeature(Window.FEATURE_NO_TITLE); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK); - switch (theme){ + switch (theme) { case Helper.THEME_LIGHT: setTheme(R.style.AppTheme); break; @@ -120,12 +125,12 @@ public class PhotoEditorActivity extends BaseActivity implements OnPhotoEditorL } Bundle b = getIntent().getExtras(); - if( getSupportActionBar() != null) + if (getSupportActionBar() != null) getSupportActionBar().hide(); String path = null; - if(b != null) + if (b != null) path = b.getString("imageUri", null); - if( path == null) { + if (path == null) { finish(); } uri = Uri.parse(path); @@ -154,7 +159,6 @@ public class PhotoEditorActivity extends BaseActivity implements OnPhotoEditorL mRvFilters.setAdapter(mFilterViewAdapter); - Typeface mEmojiTypeFace = Typeface.createFromAsset(getAssets(), "emojione-android.ttf"); mPhotoEditor = new PhotoEditor.Builder(this, mPhotoEditorView) @@ -163,13 +167,24 @@ public class PhotoEditorActivity extends BaseActivity implements OnPhotoEditorL .build(); // build photo editor sdk - mPhotoEditor.setOnPhotoEditorListener(this); //Set Image Dynamically 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 +196,17 @@ 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 +258,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) { @@ -288,9 +311,9 @@ public class PhotoEditorActivity extends BaseActivity implements OnPhotoEditorL showLoading(getString(R.string.saving)); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); String myDir = sharedpreferences.getString(Helper.SET_FOLDER_RECORD, getCacheDir().getAbsolutePath()); - String filename = System.currentTimeMillis()+"_"+Helper.getFileName(PhotoEditorActivity.this, uri); + String filename = System.currentTimeMillis() + "_" + Helper.getFileName(PhotoEditorActivity.this, uri); - File file = new File(myDir+"/"+filename); + File file = new File(myDir + "/" + filename); try { file.createNewFile(); @@ -305,7 +328,7 @@ public class PhotoEditorActivity extends BaseActivity implements OnPhotoEditorL hideLoading(); showSnackbar(getString(R.string.image_saved)); mPhotoEditorView.getSource().setImageURI(Uri.fromFile(new File(imagePath))); - if( exit ){ + if (exit) { Intent intentImage = new Intent(Helper.INTENT_SEND_MODIFIED_IMAGE); intentImage.putExtra("imgpath", imagePath); LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intentImage); @@ -331,11 +354,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,20 +380,23 @@ 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(); } break; - case UCrop.REQUEST_CROP: - final Uri resultUri = UCrop.getOutput(data); - if( resultUri != null) { + case CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE: + CropImage.ActivityResult result = CropImage.getActivityResult(data); + Uri resultUri = result.getUri(); + if (resultUri != null) { mPhotoEditorView.getSource().setImageURI(resultUri); + mPhotoEditorView.getSource().setRotation(rotationInDegrees); File fdelete = new File(uri.getPath()); if (fdelete.exists()) { - fdelete.delete(); + fdelete.delete(); } uri = resultUri; - String filename = System.currentTimeMillis()+"_"+Helper.getFileName(PhotoEditorActivity.this, uri); + String filename = System.currentTimeMillis() + "_" + Helper.getFileName(PhotoEditorActivity.this, uri); tempname = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(new Date()) + filename; } @@ -409,13 +449,13 @@ public class PhotoEditorActivity extends BaseActivity implements OnPhotoEditorL int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK); if (theme == Helper.THEME_DARK) { style = R.style.DialogDark; - } else if (theme == Helper.THEME_BLACK){ + } else if (theme == Helper.THEME_BLACK) { style = R.style.DialogBlack; - }else { + } else { style = R.style.Dialog; } AlertDialog.Builder builder = new AlertDialog.Builder(this, style); - builder.setMessage( getString(R.string.confirm_exit_editing)); + builder.setMessage(getString(R.string.confirm_exit_editing)); builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { @@ -477,17 +517,13 @@ public class PhotoEditorActivity extends BaseActivity implements OnPhotoEditorL mStickerBSFragment.show(getSupportFragmentManager(), mStickerBSFragment.getTag()); break; case CROP: - String filename = System.currentTimeMillis()+"_"+Helper.getFileName(PhotoEditorActivity.this, uri); - tempname = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(new Date()) + filename; - UCrop.of(uri, Uri.fromFile(new File(getCacheDir(),tempname))) - .start(PhotoEditorActivity.this); + CropImage.activity(uri) + .start(this); break; } } - - void showFilter(boolean isVisible) { mIsFilterVisible = isVisible; mConstraintSet.clone(mRootView); |