From 2bfd7eb9ae5cfcc0254c0bdee0e5f173c28a5902 Mon Sep 17 00:00:00 2001 From: Nimrod Dayan Date: Sat, 15 Apr 2023 20:40:13 +0300 Subject: [PATCH] Added an option to resize the cropped bitmap to specific size --- .../smarttoolfactory/cropper/ImageCropper.kt | 31 +++++++++++++------ .../cropper/crop/CropAgent.kt | 27 +++++++++++++++- .../cropper/settings/CropDefaults.kt | 8 +++-- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/cropper/src/main/java/com/smarttoolfactory/cropper/ImageCropper.kt b/cropper/src/main/java/com/smarttoolfactory/cropper/ImageCropper.kt index f6efbd5..b2ab370 100644 --- a/cropper/src/main/java/com/smarttoolfactory/cropper/ImageCropper.kt +++ b/cropper/src/main/java/com/smarttoolfactory/cropper/ImageCropper.kt @@ -135,7 +135,8 @@ fun ImageCropper( cropState.cropRect, cropOutline, onCropStart, - onCropSuccess + onCropSuccess, + cropProperties.requiredSize ) val imageModifier = Modifier @@ -296,7 +297,8 @@ private fun Crop( cropRect: Rect, cropOutline: CropOutline, onCropStart: () -> Unit, - onCropSuccess: (ImageBitmap) -> Unit + onCropSuccess: (ImageBitmap) -> Unit, + requiredSize: IntSize?, ) { val density = LocalDensity.current @@ -308,15 +310,24 @@ private fun Crop( LaunchedEffect(crop) { if (crop) { flow { - emit( - cropAgent.crop( - scaledImageBitmap, - cropRect, - cropOutline, - layoutDirection, - density - ) + val croppedImageBitmap = cropAgent.crop( + scaledImageBitmap, + cropRect, + cropOutline, + layoutDirection, + density ) + if (requiredSize != null) { + emit( + cropAgent.resize( + croppedImageBitmap, + requiredSize.width, + requiredSize.height, + ) + ) + } else { + emit(croppedImageBitmap) + } } .flowOn(Dispatchers.Default) .onStart { diff --git a/cropper/src/main/java/com/smarttoolfactory/cropper/crop/CropAgent.kt b/cropper/src/main/java/com/smarttoolfactory/cropper/crop/CropAgent.kt index 7166e8d..5e8967f 100644 --- a/cropper/src/main/java/com/smarttoolfactory/cropper/crop/CropAgent.kt +++ b/cropper/src/main/java/com/smarttoolfactory/cropper/crop/CropAgent.kt @@ -3,7 +3,17 @@ package com.smarttoolfactory.cropper.crop import android.graphics.Bitmap import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Rect -import androidx.compose.ui.graphics.* +import androidx.compose.ui.graphics.BlendMode +import androidx.compose.ui.graphics.Canvas +import androidx.compose.ui.graphics.ImageBitmap +import androidx.compose.ui.graphics.Paint +import androidx.compose.ui.graphics.Path +import androidx.compose.ui.graphics.addOutline +import androidx.compose.ui.graphics.asAndroidBitmap +import androidx.compose.ui.graphics.asAndroidPath +import androidx.compose.ui.graphics.asImageBitmap +import androidx.compose.ui.graphics.nativeCanvas +import androidx.compose.ui.graphics.toComposeRect import androidx.compose.ui.unit.Density import androidx.compose.ui.unit.LayoutDirection import com.smarttoolfactory.cropper.model.CropImageMask @@ -138,5 +148,20 @@ class CropAgent { } } } + + fun resize( + croppedImageBitmap: ImageBitmap, + requiredWidth: Int, + requiredHeight: Int + ): ImageBitmap { + val resizedBitmap: Bitmap = Bitmap.createScaledBitmap( + croppedImageBitmap.asAndroidBitmap(), + requiredWidth, + requiredHeight, + true + ) + + return resizedBitmap.asImageBitmap() + } } diff --git a/cropper/src/main/java/com/smarttoolfactory/cropper/settings/CropDefaults.kt b/cropper/src/main/java/com/smarttoolfactory/cropper/settings/CropDefaults.kt index 2145f2f..cbf8692 100644 --- a/cropper/src/main/java/com/smarttoolfactory/cropper/settings/CropDefaults.kt +++ b/cropper/src/main/java/com/smarttoolfactory/cropper/settings/CropDefaults.kt @@ -5,6 +5,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.IntSize import androidx.compose.ui.unit.dp import com.smarttoolfactory.cropper.ImageCropper import com.smarttoolfactory.cropper.crop @@ -36,7 +37,8 @@ object CropDefaults { pannable: Boolean = true, fling: Boolean = false, zoomable: Boolean = true, - rotatable: Boolean = false + rotatable: Boolean = false, + requiredSize: IntSize? = null, ): CropProperties { return CropProperties( cropType = cropType, @@ -49,7 +51,8 @@ object CropDefaults { pannable = pannable, fling = fling, zoomable = zoomable, - rotatable = rotatable + rotatable = rotatable, + requiredSize = requiredSize, ) } @@ -94,6 +97,7 @@ data class CropProperties internal constructor( val rotatable: Boolean, val zoomable: Boolean, val maxZoom: Float, + val requiredSize: IntSize? = null, ) /**