Skip to content

Commit 28a3506

Browse files
Merge pull request #20 from Nimrodda/resize-exact
Added an option to resize the cropped bitmap to specific size
2 parents 3b6997c + 2bfd7eb commit 28a3506

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

cropper/src/main/java/com/smarttoolfactory/cropper/ImageCropper.kt

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ fun ImageCropper(
135135
cropState.cropRect,
136136
cropOutline,
137137
onCropStart,
138-
onCropSuccess
138+
onCropSuccess,
139+
cropProperties.requiredSize
139140
)
140141

141142
val imageModifier = Modifier
@@ -296,7 +297,8 @@ private fun Crop(
296297
cropRect: Rect,
297298
cropOutline: CropOutline,
298299
onCropStart: () -> Unit,
299-
onCropSuccess: (ImageBitmap) -> Unit
300+
onCropSuccess: (ImageBitmap) -> Unit,
301+
requiredSize: IntSize?,
300302
) {
301303

302304
val density = LocalDensity.current
@@ -308,15 +310,24 @@ private fun Crop(
308310
LaunchedEffect(crop) {
309311
if (crop) {
310312
flow {
311-
emit(
312-
cropAgent.crop(
313-
scaledImageBitmap,
314-
cropRect,
315-
cropOutline,
316-
layoutDirection,
317-
density
318-
)
313+
val croppedImageBitmap = cropAgent.crop(
314+
scaledImageBitmap,
315+
cropRect,
316+
cropOutline,
317+
layoutDirection,
318+
density
319319
)
320+
if (requiredSize != null) {
321+
emit(
322+
cropAgent.resize(
323+
croppedImageBitmap,
324+
requiredSize.width,
325+
requiredSize.height,
326+
)
327+
)
328+
} else {
329+
emit(croppedImageBitmap)
330+
}
320331
}
321332
.flowOn(Dispatchers.Default)
322333
.onStart {

cropper/src/main/java/com/smarttoolfactory/cropper/crop/CropAgent.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ package com.smarttoolfactory.cropper.crop
33
import android.graphics.Bitmap
44
import androidx.compose.ui.geometry.Offset
55
import androidx.compose.ui.geometry.Rect
6-
import androidx.compose.ui.graphics.*
6+
import androidx.compose.ui.graphics.BlendMode
7+
import androidx.compose.ui.graphics.Canvas
8+
import androidx.compose.ui.graphics.ImageBitmap
9+
import androidx.compose.ui.graphics.Paint
10+
import androidx.compose.ui.graphics.Path
11+
import androidx.compose.ui.graphics.addOutline
12+
import androidx.compose.ui.graphics.asAndroidBitmap
13+
import androidx.compose.ui.graphics.asAndroidPath
14+
import androidx.compose.ui.graphics.asImageBitmap
15+
import androidx.compose.ui.graphics.nativeCanvas
16+
import androidx.compose.ui.graphics.toComposeRect
717
import androidx.compose.ui.unit.Density
818
import androidx.compose.ui.unit.LayoutDirection
919
import com.smarttoolfactory.cropper.model.CropImageMask
@@ -138,5 +148,20 @@ class CropAgent {
138148
}
139149
}
140150
}
151+
152+
fun resize(
153+
croppedImageBitmap: ImageBitmap,
154+
requiredWidth: Int,
155+
requiredHeight: Int
156+
): ImageBitmap {
157+
val resizedBitmap: Bitmap = Bitmap.createScaledBitmap(
158+
croppedImageBitmap.asAndroidBitmap(),
159+
requiredWidth,
160+
requiredHeight,
161+
true
162+
)
163+
164+
return resizedBitmap.asImageBitmap()
165+
}
141166
}
142167

cropper/src/main/java/com/smarttoolfactory/cropper/settings/CropDefaults.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import androidx.compose.ui.Modifier
55
import androidx.compose.ui.graphics.Color
66
import androidx.compose.ui.layout.ContentScale
77
import androidx.compose.ui.unit.Dp
8+
import androidx.compose.ui.unit.IntSize
89
import androidx.compose.ui.unit.dp
910
import com.smarttoolfactory.cropper.ImageCropper
1011
import com.smarttoolfactory.cropper.crop
@@ -36,7 +37,8 @@ object CropDefaults {
3637
pannable: Boolean = true,
3738
fling: Boolean = false,
3839
zoomable: Boolean = true,
39-
rotatable: Boolean = false
40+
rotatable: Boolean = false,
41+
requiredSize: IntSize? = null,
4042
): CropProperties {
4143
return CropProperties(
4244
cropType = cropType,
@@ -49,7 +51,8 @@ object CropDefaults {
4951
pannable = pannable,
5052
fling = fling,
5153
zoomable = zoomable,
52-
rotatable = rotatable
54+
rotatable = rotatable,
55+
requiredSize = requiredSize,
5356
)
5457
}
5558

@@ -94,6 +97,7 @@ data class CropProperties internal constructor(
9497
val rotatable: Boolean,
9598
val zoomable: Boolean,
9699
val maxZoom: Float,
100+
val requiredSize: IntSize? = null,
97101
)
98102

99103
/**

0 commit comments

Comments
 (0)