Skip to content

Commit

Permalink
Adding compression quality as parameter of ResizeOption. Fixes #41.
Browse files Browse the repository at this point in the history
  • Loading branch information
shtolik committed Jan 11, 2024
1 parent 9fd2a82 commit a371788
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private fun pickSingleImage(
uri = uri,
width = resizeOptions.width,
height = resizeOptions.height,
compressionQuality = resizeOptions.compressionQuality,
filterOptions = filterOptions,
) { resizedImage ->
if (resizedImage != null) {
Expand Down Expand Up @@ -127,6 +128,7 @@ private fun pickMultipleImages(
uri = uri,
width = resizeOptions.width,
height = resizeOptions.height,
compressionQuality = resizeOptions.compressionQuality,
filterOptions = filterOptions,
) { resizedImage ->
resizedImage?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ internal object PeekabooImageResizer {
uri: Uri,
width: Int,
height: Int,
compressionQuality: Double,
filterOptions: FilterOptions,
onResult: (ByteArray?) -> Unit,
) {
coroutineScope.launch(Dispatchers.IO) {
val byteArray = resizeImage(context, uri, width, height, filterOptions)
val byteArray = resizeImage(context, uri, width, height, compressionQuality, filterOptions)
withContext(Dispatchers.Main) {
onResult(byteArray)
}
Expand All @@ -54,6 +55,7 @@ internal object PeekabooImageResizer {
uri: Uri,
width: Int,
height: Int,
compression: Double,
filterOptions: FilterOptions,
): ByteArray? {
val resizeCacheKey = "${uri}_w${width}_h$height"
Expand Down Expand Up @@ -90,7 +92,7 @@ internal object PeekabooImageResizer {
val filteredBitmap = applyFilter(rotatedBitmap, filterOptions)

ByteArrayOutputStream().use { byteArrayOutputStream ->
filteredBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream)
filteredBitmap.compress(Bitmap.CompressFormat.JPEG, (100 * compression).toInt(), byteArrayOutputStream)
val byteArray = byteArrayOutputStream.toByteArray()
PeekabooBitmapCache.instance.put(filterCacheKey, filteredBitmap)
return byteArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ sealed class SelectionMode {
data class ResizeOptions(
val width: Int = 800,
val height: Int = 800,
val compressionQuality: Double = 1.0,
)

sealed interface FilterOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ actual fun rememberImagePickerLauncher(
resizeOptions.height,
filterOptions,
)
val bytes = resizedImage?.toByteArray()
val bytes = resizedImage?.toByteArray(resizeOptions.compressionQuality)
if (bytes != null) {
imageData.add(bytes)
}
Expand Down Expand Up @@ -121,8 +121,8 @@ actual fun rememberImagePickerLauncher(
}

@OptIn(ExperimentalForeignApi::class)
private fun UIImage.toByteArray(): ByteArray {
val jpegData = UIImageJPEGRepresentation(this, 1.0)!!
private fun UIImage.toByteArray(compressionQuality: Double): ByteArray {
val jpegData = UIImageJPEGRepresentation(this, compressionQuality)!!
return ByteArray(jpegData.length.toInt()).apply {
memcpy(this.refTo(0), jpegData.bytes, jpegData.length)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fun App() {
selectionMode = SelectionMode.Multiple(maxSelection = 5),
scope = scope,
// Resize options are customizable. Default is set to 800 x 800 pixels.
resizeOptions = ResizeOptions(width = 1200, height = 1200),
resizeOptions = ResizeOptions(width = 1200, height = 1200, compressionQuality = 1.0),
// Default is 'Default', which applies no filter.
// Other available options: GrayScale, Sepia, Invert.
filterOptions = FilterOptions.GrayScale,
Expand Down

0 comments on commit a371788

Please sign in to comment.