Best Android Image Cropping Libraries
When you are capturing images from the Camera, you sometimes need to crop those images before saving them in external storage or uploading them to the server. Luckily there are already great and modern solutions to allow us integrate cropping capability in our android apps, be it via Kotlin or Java.
In this piece we will look at these options, how to install them and code snippets of how to use them.
(a). Croppy
This is a modern Image Cropping Library for Android with several cool features. Here are it's features:
Double tap focus
It zooms-in to the touch points on double tap. Restores default state when user double taps on max scale state.
Zoom in and out with two finger.
You can crop your image in free mode. In any size.
Enhanced aspect ratio mode will help you while cropping. Aspect ratio will be fixed while you play with cropper. So for your social media apps, it will help you to crop in fixed size (instagram, facebook, twitter, 16:9, 1:2, 3:2 and more..)
While you scale your image, size displayer indicates the bitmap size reactively. It will provide a smoother experience to the user.
What ever you do while cropping, we center the bitmap with animation. Smoother experience for user.
We use animation everywhere in this cropper. User zoomed-out too much? We zoom back in with animation. User scrolled image out of borders? We scroll it back with animation.
Steps for using Croppy
Step 1: Installation
First install Croppy from jitpack:
Step 2: Basic usage
Croppy is very simple to use. You just need to start it:
//Start croppy (source uri is the original image.)
val cropRequest = CropRequest.Auto(sourceUri = uri, requestCode = 101)
Croppy.start(this, cropRequest)
Then listen to results:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 101) {
imageview.setImageURI(data.data)
}
}
Step 3: Custom Usage
For more advanced usage you can for example save the cropped result in the external storage:
You can also save it in the cache:
val cacheCropRequest = CropRequest.Auto(
sourceUri = uri,
requestCode = RC_CROP_IMAGE,
storageType = StorageType.CACHE
)
You can also specify a custom destination for the cropped image:
val destinationUri = ...
val manuelCropRequest = CropRequest.Manual(
sourceUri = uri,
destinationUri = destinationUri,
requestCode = RC_CROP_IMAGE
)
If you want to exclude some specific aspect ratio from bottom aspect ratio list view.
val excludeAspectRatiosCropRequest = CropRequest.Manual(
sourceUri = uri,
destinationUri = destinationUri,
requestCode = RC_CROP_IMAGE,
excludedAspectRatios = arrayListOf(AspectRatio.ASPECT_FREE)
)
If you want to give specific theme as accent color.
val themeCropRequest = CropRequest.Manual(
sourceUri = uri,
destinationUri = destinationUri,
requestCode = RC_CROP_IMAGE,
croppyTheme = CroppyTheme(R.color.blue)
)
Ultimately you have to start croppy:
Demo
Here is the demo:
Links
Yalantis/uCrop
Image Cropping Library for Android.
uCrop aims to provide an ultimate and flexible image cropping experience.
Here is a sample screenshot:
Step 1: Install it
- Include the library as a local library project.
A lightweight general solution:
Get power of the native code to preserve image quality (+ about 1.5 MB to an apk size):Step 2: Add to Manifest
Add UCropActivity into your AndroidManifest.xml
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
Step 3: Write Code:
The uCrop configuration is created using the builder pattern.
UCrop.of(sourceUri, destinationUri)
.withAspectRatio(16, 9)
.withMaxResultSize(maxWidth, maxHeight)
.start(context);
Then Override onActivityResult
method and handle uCrop result:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
final Uri resultUri = UCrop.getOutput(data);
} else if (resultCode == UCrop.RESULT_ERROR) {
final Throwable cropError = UCrop.getError(data);
}
}
You may want to add this to your PROGUARD config:
-dontwarn com.yalantis.ucrop**
-keep class com.yalantis.ucrop** { *; }
-keep interface com.yalantis.ucrop** { *; }
Customization
If you want to let your users choose crop ratio dynamically, just do not call withAspectRatio(x, y)
.
uCrop builder class has method withOptions(UCrop.Options options)
which extends library configurations.
Currently, you can change:
- image compression format (e.g. PNG, JPEG, WEBP), compression
- image compression quality [0 - 100]. PNG which is lossless, will ignore the quality setting.
- whether all gestures are enabled simultaneously
- maximum size for Bitmap that is decoded from source Uri and used within crop view. If you want to override the default behaviour.
- toggle whether to show crop frame/guidelines
- setup color/width/count of crop frame/rows/columns
- choose whether you want rectangle or oval(
options.setCircleDimmedLayer(true)
) crop area - the UI colors (Toolbar, StatusBar, active widget state)
- and more...
Compatibility
- Library - Android ICS 4.0+ (API 14) (Android GINGERBREAD 2.3+ (API 10) for versions <= 1.3.2)
- Sample - Android ICS 4.0+ (API 14)
- CPU - armeabi armeabi-v7a x86 x86_64 arm64-v8a (for versions >= 2.1.2)
Reference
Read more here.