Skip to content

Commit

Permalink
fix(#254): resize and compress large images...
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrimault committed Apr 15, 2024
1 parent 9df0406 commit 5e6fa4c
Showing 1 changed file with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package fr.geonature.occtax.ui.input.counting

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.provider.MediaStore
import android.webkit.MimeTypeMap
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.ActivityResultRegistry
import androidx.activity.result.contract.ActivityResultContracts
Expand All @@ -29,6 +30,8 @@ class TakePhotoLifecycleObserver(
) :
DefaultLifecycleObserver {

private val imageMaxSize = 2048
private val imageQuality = 80
private lateinit var takeImageResultLauncher: ActivityResultLauncher<Intent>
private var chosenImageContinuation: CancellableContinuation<File?>? = null
private var baseFilePath: File? = null
Expand All @@ -48,6 +51,7 @@ class TakePhotoLifecycleObserver(

chosenImageContinuation?.resumeWith(Result.success(null))
}

AppCompatActivity.RESULT_OK -> {

val imageFile = (result.data?.data ?: imageUri)?.let { asFile(it) }
Expand Down Expand Up @@ -91,9 +95,10 @@ class TakePhotoLifecycleObserver(
baseFilePath,
"${Date().time}.jpg"
)
).also {
imageUri = it
}
)
.also {
imageUri = it
}
)
}
)
Expand All @@ -105,13 +110,7 @@ class TakePhotoLifecycleObserver(

private fun asFile(uri: Uri): File {
val filename = (uri.lastPathSegment ?: "${Date().time}").let {
"${it.substringBeforeLast(".")}${
it.substringAfterLast(
".",
MimeTypeMap.getSingleton()
.getExtensionFromMimeType(applicationContext.contentResolver.getType(uri)) ?: ""
).takeIf { ext -> ext.isNotEmpty() }?.let { ext -> ".$ext" } ?: ""
}"
"${it.substringBeforeLast(".")}.jpg"
}

if (!File(
Expand All @@ -124,17 +123,41 @@ class TakePhotoLifecycleObserver(
File(
baseFilePath,
filename
).outputStream().use { outputSteam ->
inputStream.copyTo(outputSteam)
outputSteam.flush()
}
).outputStream()
.use { outputSteam ->
inputStream.copyTo(outputSteam)
outputSteam.flush()
}
}
}

return File(
baseFilePath,
filename
)
).also {
resizeAndCompress(it)
}
}

private fun resizeAndCompress(file: File, scaleTo: Int = imageMaxSize) {
val bmOptions = BitmapFactory.Options()
bmOptions.inJustDecodeBounds = true
BitmapFactory.decodeFile(file.absolutePath, bmOptions)
val width = bmOptions.outWidth
val height = bmOptions.outHeight

// determine how much to scale down the image
val scaleFactor = (width / scaleTo).coerceAtLeast(height / scaleTo)

bmOptions.inJustDecodeBounds = false
bmOptions.inSampleSize = scaleFactor

val resized = BitmapFactory.decodeFile(file.absolutePath, bmOptions) ?: return
file.outputStream().use {
resized.compress(Bitmap.CompressFormat.JPEG, imageQuality, it)
resized.recycle()
it.flush()
}
}

enum class ImagePicker {
Expand Down

0 comments on commit 5e6fa4c

Please sign in to comment.