Easy to use image cropping library for Jetpack compose, with support for shapes, aspect-ratios, transformations, large images, auto zoom ...
Add a dependency on the library to your Android project (Desktop not supported for now)
dependencies {
implementation("io.github.mr0xf00:easycrop:0.1.1")
}val imageCropper = rememberImageCropper()class MyViewModel : ViewModel {
val imageCropper = ImageCropper()
}scope.launch {
val result = imageCropper.crop(bitmap) // Suspends until user accepts or cancels cropping
when (result) {
CropResult.Cancelled -> { }
is CropError -> { }
is CropResult.Success -> { result.bitmap }
}
}val cropState = imageCropper.cropState
if(cropState != null) ImageCropperDialog(state = cropState)That's it !
The crop function provides overloads for ImageBitmap, Uri, File, but it is also possible to use a custom ImageSrc.
You can use the rememberImagePicker function to easily pick an image and crop it :
val scope = rememberCoroutineScope()
val context = LocalContext.current
val imagePicker = rememberImagePicker(onImage = { uri ->
scope.launch {
val result = imageCropper.crop(uri, context)
}
})To customize the ui of the image cropper you can provide a different implementation of CropperStyle to the cropper dialog.
You can also use the CropperStyle factory function. example :
ImageCropperDialog(
state = cropState,
style = CropperStyle(
overlay = Color.Red.copy(alpha = .5f),
autoZoom = false,
guidelines = null,
)
)