Introduction to SmartCropper
SmartCropper is an easy-to-use intelligent image cropping library designed to handle photos such as ID cards, business cards, and documents. Its goal is to simplify the process of cropping images precisely, ensuring high recognition rates. This project, especially useful for developers, allows app users to capture exactly the relevant parts of an image without needing intricate manual adjustments.
Key Features
- Intelligent Border Detection: Utilizes an intelligent algorithm based on OpenCV to recognize image borders accurately.
- Interactive Selection Adjustment: Users can drag anchor points to adjust selections manually, with a magnifying glass effect enhancing the precision of positioning.
- Perspective Transformation: Supports cropping and correcting the selected area to restore a frontal view of the image.
- Customizable UI: Offers extensive UI customization, including grid lines, masks, anchor points, and magnifying options.
Example Usage
-
Smart Selection and Correction: After selecting an image, SmartCropper automatically identifies the area of interest. It applies perspective transformation to crop and correct the image, displaying the final result.
-
Manual Adjustment: Users can manually adjust the cropping area by dragging the anchor points. The magnifying glass effect in the upper right corner aids in precise positioning.
Animated Demo:
Algorithm Optimization (V2.1.1+)
SmartCropper replaces the Canny algorithm with a machine learning approach, enhancing detection accuracy through a TensorFlow-based HED network. Developers can integrate this optimized algorithm by configuring build settings and initializing SmartCropper correctly.
Gradle Setup
-
Add the following to the project's
build.gradle
to ensure the machine learning model is not compressed:aaptOptions { noCompress "tflite" noCompress "lite" }
-
Initialize the image detector in appropriate places (e.g.,
Application.onCreate
):SmartCropper.buildImageDetector(this);
Integration Steps
-
Add JitPack Repository: In the root
build.gradle
file:allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
-
Dependency Addition: Include the following in the
dependencies
section:dependencies { compile 'com.github.pqpo:SmartCropper:v2.1.3' }
-
ProGuard Configuration: Since JNI is used, add the following rule to prevent obfuscation:
-keep class me.pqpo.smartcropperlib.**{*;}
Usage Instructions
1. Cropping Layout
Define a CropImageView
in your XML layout:
<me.pqpo.smartcropperlib.view.CropImageView
android:id="@+id/iv_crop"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Note: CropImageView
inherits from ImageView
and requires a ScaleType
of centered types to avoid errors.
2. Set Image to Crop
Assign the image to be cropped:
ivCrop.setImageToCrop(selectedBitmap);
This method employs native code for efficient border detection, rendering the image and selection. It's optimized for performance, making it suitable even for large images.
3. Crop the Image
Execute cropping within the selected area:
Bitmap crop = ivCrop.crop();
The selected section is cropped, and perspective transformation is applied to present the image correctly.
Configurable Attributes
SmartCropper provides several customizable attributes to enhance its functionality, including mask opacity, guide line visibility, and anchor point styles.
Development and Support
Developers interested in contributing or seeking support can contact the author through various channels:
- Email: [email protected]
- GitHub: pqpo
- Personal Blog: pqpo's notes
- Twitter: Pqponet
Conclusion
SmartCropper offers sophisticated cropping capabilities, leveraging intelligent algorithms to improve accuracy and efficiency in image processing tasks. It's a valuable asset for applications requiring precise image cropping, enhancing user experience through intuitive design and functionality.