mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-11-11 21:16:55 +01:00
Create private functions for image vs video compression and when statement to select on content type
This commit is contained in:
@@ -58,7 +58,24 @@ class MediaCompressor {
|
||||
|
||||
checkNotInMainThread()
|
||||
|
||||
if (contentType?.startsWith("video", true) == true) {
|
||||
// branch into compression based on content type
|
||||
when {
|
||||
contentType?.startsWith("video", ignoreCase = true) == true
|
||||
-> compressVideo(uri, contentType, applicationContext, onReady, onError, mediaQuality)
|
||||
contentType?.startsWith("image", ignoreCase = true) == true && !contentType.contains("gif") && !contentType.contains("svg")
|
||||
-> compressImage(uri, contentType, applicationContext, onReady, onError, mediaQuality)
|
||||
else -> onReady(uri, contentType, null)
|
||||
}
|
||||
}
|
||||
|
||||
private fun compressVideo(
|
||||
uri: Uri,
|
||||
contentType: String?,
|
||||
applicationContext: Context,
|
||||
onReady: (Uri, String?, Long?) -> Unit,
|
||||
onError: (Int) -> Unit,
|
||||
mediaQuality: CompressorQuality,
|
||||
) {
|
||||
val videoQuality =
|
||||
when (mediaQuality) {
|
||||
CompressorQuality.VERY_LOW -> VideoQuality.VERY_LOW
|
||||
@@ -66,9 +83,11 @@ class MediaCompressor {
|
||||
CompressorQuality.MEDIUM -> VideoQuality.MEDIUM
|
||||
CompressorQuality.HIGH -> VideoQuality.HIGH
|
||||
CompressorQuality.VERY_HIGH -> VideoQuality.VERY_HIGH
|
||||
else -> VideoQuality.MEDIUM // dodgy
|
||||
else -> VideoQuality.MEDIUM
|
||||
}
|
||||
|
||||
Log.d("MediaCompressor", "Using video compression $mediaQuality")
|
||||
|
||||
VideoCompressor.start(
|
||||
// => This is required
|
||||
context = applicationContext,
|
||||
@@ -96,9 +115,7 @@ class MediaCompressor {
|
||||
) {
|
||||
}
|
||||
|
||||
override fun onStart(index: Int) {
|
||||
// Compression start
|
||||
}
|
||||
override fun onStart(index: Int) {}
|
||||
|
||||
override fun onSuccess(
|
||||
index: Int,
|
||||
@@ -118,8 +135,8 @@ class MediaCompressor {
|
||||
index: Int,
|
||||
failureMessage: String,
|
||||
) {
|
||||
// keeps going with original video
|
||||
Log.d("MediaCompressor", "Video compression failed: $failureMessage")
|
||||
// keeps going with original video
|
||||
onReady(uri, contentType, null)
|
||||
}
|
||||
|
||||
@@ -128,7 +145,16 @@ class MediaCompressor {
|
||||
}
|
||||
},
|
||||
)
|
||||
} else if (contentType?.startsWith("image", true) == true && !contentType.contains("gif") && !contentType.contains("svg")) {
|
||||
}
|
||||
|
||||
private suspend fun compressImage(
|
||||
uri: Uri,
|
||||
contentType: String?,
|
||||
context: Context,
|
||||
onReady: (Uri, String?, Long?) -> Unit,
|
||||
onError: (Int) -> Unit,
|
||||
mediaQuality: CompressorQuality,
|
||||
) {
|
||||
val imageQuality =
|
||||
when (mediaQuality) {
|
||||
CompressorQuality.VERY_LOW -> 40
|
||||
@@ -138,11 +164,12 @@ class MediaCompressor {
|
||||
CompressorQuality.VERY_HIGH -> 90
|
||||
else -> 60
|
||||
}
|
||||
|
||||
try {
|
||||
Log.d("MediaCompressor", "Using image compression $mediaQuality")
|
||||
val tempFile = from(uri, contentType, applicationContext)
|
||||
val tempFile = from(uri, contentType, context)
|
||||
val compressedImageFile =
|
||||
Compressor.compress(applicationContext, tempFile) {
|
||||
Compressor.compress(context, tempFile) {
|
||||
default(width = 640, format = Bitmap.CompressFormat.JPEG, quality = imageQuality)
|
||||
}
|
||||
Log.d("MediaCompressor", "Image compression success. Original size [${tempFile.length()}], new size [${compressedImageFile.length()}]")
|
||||
@@ -153,9 +180,6 @@ class MediaCompressor {
|
||||
e.printStackTrace()
|
||||
onReady(uri, contentType, null)
|
||||
}
|
||||
} else {
|
||||
onReady(uri, contentType, null)
|
||||
}
|
||||
}
|
||||
|
||||
private fun from(
|
||||
|
||||
Reference in New Issue
Block a user