mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-11-10 23:36:42 +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()
|
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 =
|
val videoQuality =
|
||||||
when (mediaQuality) {
|
when (mediaQuality) {
|
||||||
CompressorQuality.VERY_LOW -> VideoQuality.VERY_LOW
|
CompressorQuality.VERY_LOW -> VideoQuality.VERY_LOW
|
||||||
@@ -66,9 +83,11 @@ class MediaCompressor {
|
|||||||
CompressorQuality.MEDIUM -> VideoQuality.MEDIUM
|
CompressorQuality.MEDIUM -> VideoQuality.MEDIUM
|
||||||
CompressorQuality.HIGH -> VideoQuality.HIGH
|
CompressorQuality.HIGH -> VideoQuality.HIGH
|
||||||
CompressorQuality.VERY_HIGH -> VideoQuality.VERY_HIGH
|
CompressorQuality.VERY_HIGH -> VideoQuality.VERY_HIGH
|
||||||
else -> VideoQuality.MEDIUM // dodgy
|
else -> VideoQuality.MEDIUM
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.d("MediaCompressor", "Using video compression $mediaQuality")
|
Log.d("MediaCompressor", "Using video compression $mediaQuality")
|
||||||
|
|
||||||
VideoCompressor.start(
|
VideoCompressor.start(
|
||||||
// => This is required
|
// => This is required
|
||||||
context = applicationContext,
|
context = applicationContext,
|
||||||
@@ -96,9 +115,7 @@ class MediaCompressor {
|
|||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStart(index: Int) {
|
override fun onStart(index: Int) {}
|
||||||
// Compression start
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onSuccess(
|
override fun onSuccess(
|
||||||
index: Int,
|
index: Int,
|
||||||
@@ -118,8 +135,8 @@ class MediaCompressor {
|
|||||||
index: Int,
|
index: Int,
|
||||||
failureMessage: String,
|
failureMessage: String,
|
||||||
) {
|
) {
|
||||||
// keeps going with original video
|
|
||||||
Log.d("MediaCompressor", "Video compression failed: $failureMessage")
|
Log.d("MediaCompressor", "Video compression failed: $failureMessage")
|
||||||
|
// keeps going with original video
|
||||||
onReady(uri, contentType, null)
|
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 =
|
val imageQuality =
|
||||||
when (mediaQuality) {
|
when (mediaQuality) {
|
||||||
CompressorQuality.VERY_LOW -> 40
|
CompressorQuality.VERY_LOW -> 40
|
||||||
@@ -138,11 +164,12 @@ class MediaCompressor {
|
|||||||
CompressorQuality.VERY_HIGH -> 90
|
CompressorQuality.VERY_HIGH -> 90
|
||||||
else -> 60
|
else -> 60
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Log.d("MediaCompressor", "Using image compression $mediaQuality")
|
Log.d("MediaCompressor", "Using image compression $mediaQuality")
|
||||||
val tempFile = from(uri, contentType, applicationContext)
|
val tempFile = from(uri, contentType, context)
|
||||||
val compressedImageFile =
|
val compressedImageFile =
|
||||||
Compressor.compress(applicationContext, tempFile) {
|
Compressor.compress(context, tempFile) {
|
||||||
default(width = 640, format = Bitmap.CompressFormat.JPEG, quality = imageQuality)
|
default(width = 640, format = Bitmap.CompressFormat.JPEG, quality = imageQuality)
|
||||||
}
|
}
|
||||||
Log.d("MediaCompressor", "Image compression success. Original size [${tempFile.length()}], new size [${compressedImageFile.length()}]")
|
Log.d("MediaCompressor", "Image compression success. Original size [${tempFile.length()}], new size [${compressedImageFile.length()}]")
|
||||||
@@ -153,9 +180,6 @@ class MediaCompressor {
|
|||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
onReady(uri, contentType, null)
|
onReady(uri, contentType, null)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
onReady(uri, contentType, null)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun from(
|
private fun from(
|
||||||
|
|||||||
Reference in New Issue
Block a user