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:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.lyrebirdstudio:Croppy:0.2'
}
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:
val externalCropRequest = CropRequest.Auto(sourceUri = uri, requestCode = RC_CROP_IMAGE)
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:
//Start croppy with your custom request.
Croppy.start(this, cropRequest)
Demo
Here is the demo:
Links