From 0fc0a24b3526dba6d4140589e0fc9390c6865281 Mon Sep 17 00:00:00 2001 From: HenryZhang0222 <125983242+HenryZhang0222@users.noreply.github.com> Date: Sat, 18 Nov 2023 07:08:36 +1100 Subject: [PATCH] Issue #987 Crop feature cam scanner (#1145) --- .../fragment/ImageToPdfFragment.java | 95 ++++++++++++------- app/src/main/res/values/strings.xml | 3 + 2 files changed, 62 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/swati4star/createpdf/fragment/ImageToPdfFragment.java b/app/src/main/java/swati4star/createpdf/fragment/ImageToPdfFragment.java index 130f3aa3..3562d207 100755 --- a/app/src/main/java/swati4star/createpdf/fragment/ImageToPdfFragment.java +++ b/app/src/main/java/swati4star/createpdf/fragment/ImageToPdfFragment.java @@ -64,13 +64,11 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import swati4star.createpdf.R; -import swati4star.createpdf.activity.CropImageActivity; import swati4star.createpdf.activity.ImageEditor; import swati4star.createpdf.activity.PreviewActivity; import swati4star.createpdf.activity.RearrangeImages; @@ -130,7 +128,8 @@ public class ImageToPdfFragment extends Fragment implements OnItemClickListener, private int mMarginRight = 38; private String mPageNumStyle; private int mChoseId; - + private int mImageCounter = 0; + private boolean mShouldCropImages = false; @Override public void onAttach(Context context) { @@ -296,35 +295,6 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { return; switch (requestCode) { - case INTENT_REQUEST_GET_IMAGES: - mImagesUri.clear(); - mUnarrangedImagesUri.clear(); - mImagesUri.addAll(Matisse.obtainPathResult(data)); - mUnarrangedImagesUri.addAll(mImagesUri); - if (mImagesUri.size() > 0) { - mNoOfImages.setText(String.format(mActivity.getResources() - .getString(R.string.images_selected), mImagesUri.size())); - mNoOfImages.setVisibility(View.VISIBLE); - StringUtils.getInstance().showSnackbar(mActivity, R.string.snackbar_images_added); - mCreatePdf.setEnabled(true); - mCreatePdf.unblockTouch(); - } - mMorphButtonUtility.morphToSquare(mCreatePdf, mMorphButtonUtility.integer()); - mOpenPdf.setVisibility(View.GONE); - break; - - case CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE: - HashMap croppedImageUris = - (HashMap) data.getSerializableExtra(CropImage.CROP_IMAGE_EXTRA_RESULT); - - for (int i = 0; i < mImagesUri.size(); i++) { - if (croppedImageUris.get(i) != null) { - mImagesUri.set(i, croppedImageUris.get(i).getPath()); - StringUtils.getInstance().showSnackbar(mActivity, R.string.snackbar_imagecropped); - } - } - break; - case INTENT_REQUEST_APPLY_FILTER: mImagesUri.clear(); ArrayList mFilterUris = data.getStringArrayListExtra(RESULT); @@ -360,6 +330,57 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { mCreatePdf.setEnabled(false); } break; + + case INTENT_REQUEST_GET_IMAGES: + mImagesUri.clear(); + mUnarrangedImagesUri.clear(); + mImagesUri.addAll(Matisse.obtainPathResult(data)); + mUnarrangedImagesUri.addAll(mImagesUri); + if (mImagesUri.size() > 0) { + mNoOfImages.setText(String.format(mActivity.getResources() + .getString(R.string.images_selected), mImagesUri.size())); + mNoOfImages.setVisibility(View.VISIBLE); + StringUtils.getInstance().showSnackbar(mActivity, R.string.snackbar_images_added); + mCreatePdf.setEnabled(true); + mCreatePdf.unblockTouch(); + mMorphButtonUtility.morphToSquare(mCreatePdf, mMorphButtonUtility.integer()); + mOpenPdf.setVisibility(View.GONE); + + // Prompt user to crop images + new MaterialDialog.Builder(mActivity) + .title(R.string.crop_images) + .content(R.string.crop_images_content) + .positiveText(R.string.yes) + .negativeText(R.string.no) + .onPositive((dialog, which) -> { + mShouldCropImages = true; + mImageCounter = 0; + cropImage(Uri.parse(mImagesUri.get(mImageCounter))); + }) + .onNegative((dialog, which) -> { + // No cropping, continue with other operations... + }) + .show(); + } + break; + + case CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE: + CropImage.ActivityResult result = CropImage.getActivityResult(data); + if (resultCode == Activity.RESULT_OK && mShouldCropImages) { + Uri resultUri = result.getUri(); + mImagesUri.set(mImageCounter, resultUri.toString()); + mImageCounter++; + + if (mImageCounter < mImagesUri.size()) { + cropImage(Uri.parse(mImagesUri.get(mImageCounter))); + } else { + mShouldCropImages = false; + mImageCounter = 0; + } + } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) { + Exception error = result.getError(); + } + break; } } @@ -375,7 +396,7 @@ public void onItemClick(int position) { passwordProtectPDF(); break; case 1: - cropImage(); + cropImage(Uri.parse(mImagesUri.get(0))); break; case 2: compressImage(); @@ -700,9 +721,10 @@ public void onPDFCreated(boolean success, String path) { resetValues(); } - private void cropImage() { - Intent intent = new Intent(mActivity, CropImageActivity.class); - startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE); + private void cropImage(Uri imageUri) { + //Intent intent = new Intent(mActivity, CropImageActivity.class); + //startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE); + CropImage.activity(imageUri).start(getContext(), this); } private void getRuntimePermissions() { @@ -816,3 +838,4 @@ private void addPageNumbers() { materialDialog.show(); } } + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 695d5855..91a7325f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -285,6 +285,9 @@ Converting your files to PDF. Select Text File Please install a File Manager + Crop Images + Do you want to crop the images before processing? + No Text To PDF