Skip to content

Commit

Permalink
Merge branch 'master' into fixed-aspect-ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
SmartToolFactory authored May 16, 2023
2 parents ba80310 + 28a3506 commit 8733d2f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
31 changes: 21 additions & 10 deletions cropper/src/main/java/com/smarttoolfactory/cropper/ImageCropper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ fun ImageCropper(
cropState.cropRect,
cropOutline,
onCropStart,
onCropSuccess
onCropSuccess,
cropProperties.requiredSize
)

val imageModifier = Modifier
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,6 +39,7 @@ object CropDefaults {
zoomable: Boolean = true,
rotatable: Boolean = false,
fixedAspectRatio: Boolean = false,
requiredSize: IntSize? = null
): CropProperties {
return CropProperties(
cropType = cropType,
Expand All @@ -52,6 +54,7 @@ object CropDefaults {
zoomable = zoomable,
rotatable = rotatable,
fixedAspectRatio = fixedAspectRatio,
requiredSize = requiredSize
)
}

Expand Down Expand Up @@ -97,6 +100,7 @@ data class CropProperties internal constructor(
val zoomable: Boolean,
val maxZoom: Float,
val fixedAspectRatio: Boolean = false,
val requiredSize: IntSize? = null
)

/**
Expand Down

0 comments on commit 8733d2f

Please sign in to comment.