Skip to content

Commit

Permalink
Issue #987 Crop feature cam scanner (#1145)
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryZhang0222 authored Nov 17, 2023
1 parent 14d4d48 commit 0fc0a24
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<Integer, Uri> 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<String> mFilterUris = data.getStringArrayListExtra(RESULT);
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -375,7 +396,7 @@ public void onItemClick(int position) {
passwordProtectPDF();
break;
case 1:
cropImage();
cropImage(Uri.parse(mImagesUri.get(0)));
break;
case 2:
compressImage();
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -816,3 +838,4 @@ private void addPageNumbers() {
materialDialog.show();
}
}

3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@
<string name="converting_file_message">Converting your files to PDF.</string>
<string name="select_text_file">Select Text File</string>
<string name="install_file_manager">Please install a File Manager</string>
<string name="crop_images" translatable="false">Crop Images</string>
<string name="crop_images_content" translatable="false">Do you want to crop the images before processing?</string>
<string name="no" translatable="false">No</string>

<!-- Text to PDF -->
<string name="text_to_pdf">Text To PDF</string>
Expand Down

0 comments on commit 0fc0a24

Please sign in to comment.