diff --git a/cropper/src/main/java/com/smarttoolfactory/cropper/ImageCropper.kt b/cropper/src/main/java/com/smarttoolfactory/cropper/ImageCropper.kt index 0e8eb78..b79fac4 100644 --- a/cropper/src/main/java/com/smarttoolfactory/cropper/ImageCropper.kt +++ b/cropper/src/main/java/com/smarttoolfactory/cropper/ImageCropper.kt @@ -142,7 +142,8 @@ fun ImageCropper( cropState.cropRect, cropOutline, onCropStart, - onCropSuccess + onCropSuccess, + cropProperties.requiredSize ) val imageModifier = Modifier @@ -303,7 +304,8 @@ private fun Crop( cropRect: Rect, cropOutline: CropOutline, onCropStart: () -> Unit, - onCropSuccess: (ImageBitmap) -> Unit + onCropSuccess: (ImageBitmap) -> Unit, + requiredSize: IntSize?, ) { val density = LocalDensity.current @@ -315,15 +317,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 0c2ffba..b598aa4 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 @@ -38,6 +39,7 @@ object CropDefaults { zoomable: Boolean = true, rotatable: Boolean = false, fixedAspectRatio: Boolean = false, + requiredSize: IntSize? = null ): CropProperties { return CropProperties( cropType = cropType, @@ -52,6 +54,7 @@ object CropDefaults { zoomable = zoomable, rotatable = rotatable, fixedAspectRatio = fixedAspectRatio, + requiredSize = requiredSize ) } @@ -97,6 +100,7 @@ data class CropProperties internal constructor( val zoomable: Boolean, val maxZoom: Float, val fixedAspectRatio: Boolean = false, + val requiredSize: IntSize? = null ) /**