mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-11-10 21:16:46 +01:00
Create private functions for image vs video compression and when statement to select on content type
This commit is contained in:
@@ -58,102 +58,126 @@ class MediaCompressor {
|
|||||||
|
|
||||||
checkNotInMainThread()
|
checkNotInMainThread()
|
||||||
|
|
||||||
if (contentType?.startsWith("video", true) == true) {
|
// branch into compression based on content type
|
||||||
val videoQuality =
|
when {
|
||||||
when (mediaQuality) {
|
contentType?.startsWith("video", ignoreCase = true) == true
|
||||||
CompressorQuality.VERY_LOW -> VideoQuality.VERY_LOW
|
-> compressVideo(uri, contentType, applicationContext, onReady, onError, mediaQuality)
|
||||||
CompressorQuality.LOW -> VideoQuality.LOW
|
contentType?.startsWith("image", ignoreCase = true) == true && !contentType.contains("gif") && !contentType.contains("svg")
|
||||||
CompressorQuality.MEDIUM -> VideoQuality.MEDIUM
|
-> compressImage(uri, contentType, applicationContext, onReady, onError, mediaQuality)
|
||||||
CompressorQuality.HIGH -> VideoQuality.HIGH
|
else -> onReady(uri, contentType, null)
|
||||||
CompressorQuality.VERY_HIGH -> VideoQuality.VERY_HIGH
|
}
|
||||||
else -> VideoQuality.MEDIUM // dodgy
|
}
|
||||||
}
|
|
||||||
Log.d("MediaCompressor", "Using video compression $mediaQuality")
|
|
||||||
VideoCompressor.start(
|
|
||||||
// => This is required
|
|
||||||
context = applicationContext,
|
|
||||||
// => Source can be provided as content uris
|
|
||||||
uris = listOf(uri),
|
|
||||||
isStreamable = false,
|
|
||||||
// THIS STORAGE
|
|
||||||
// sharedStorageConfiguration = SharedStorageConfiguration(
|
|
||||||
// saveAt = SaveLocation.movies, // => default is movies
|
|
||||||
// videoName = "compressed_video" // => required name
|
|
||||||
// ),
|
|
||||||
// OR AND NOT BOTH
|
|
||||||
appSpecificStorageConfiguration = AppSpecificStorageConfiguration(),
|
|
||||||
configureWith =
|
|
||||||
Configuration(
|
|
||||||
quality = videoQuality,
|
|
||||||
// => required name
|
|
||||||
videoNames = listOf(UUID.randomUUID().toString()),
|
|
||||||
),
|
|
||||||
listener =
|
|
||||||
object : CompressionListener {
|
|
||||||
override fun onProgress(
|
|
||||||
index: Int,
|
|
||||||
percent: Float,
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStart(index: Int) {
|
private fun compressVideo(
|
||||||
// Compression start
|
uri: Uri,
|
||||||
}
|
contentType: String?,
|
||||||
|
applicationContext: Context,
|
||||||
override fun onSuccess(
|
onReady: (Uri, String?, Long?) -> Unit,
|
||||||
index: Int,
|
onError: (Int) -> Unit,
|
||||||
size: Long,
|
mediaQuality: CompressorQuality,
|
||||||
path: String?,
|
) {
|
||||||
) {
|
val videoQuality =
|
||||||
if (path != null) {
|
when (mediaQuality) {
|
||||||
Log.d("MediaCompressor", "Video compression success. Compressed size [$size]")
|
CompressorQuality.VERY_LOW -> VideoQuality.VERY_LOW
|
||||||
onReady(Uri.fromFile(File(path)), contentType, size)
|
CompressorQuality.LOW -> VideoQuality.LOW
|
||||||
} else {
|
CompressorQuality.MEDIUM -> VideoQuality.MEDIUM
|
||||||
Log.d("MediaCompressor", "Video compression successful, but returned null path")
|
CompressorQuality.HIGH -> VideoQuality.HIGH
|
||||||
onError(R.string.compression_returned_null)
|
CompressorQuality.VERY_HIGH -> VideoQuality.VERY_HIGH
|
||||||
}
|
else -> VideoQuality.MEDIUM
|
||||||
}
|
|
||||||
|
|
||||||
override fun onFailure(
|
|
||||||
index: Int,
|
|
||||||
failureMessage: String,
|
|
||||||
) {
|
|
||||||
// keeps going with original video
|
|
||||||
Log.d("MediaCompressor", "Video compression failed: $failureMessage")
|
|
||||||
onReady(uri, contentType, null)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCancelled(index: Int) {
|
|
||||||
onError(R.string.compression_cancelled)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
} else if (contentType?.startsWith("image", true) == true && !contentType.contains("gif") && !contentType.contains("svg")) {
|
|
||||||
val imageQuality =
|
|
||||||
when (mediaQuality) {
|
|
||||||
CompressorQuality.VERY_LOW -> 40
|
|
||||||
CompressorQuality.LOW -> 50
|
|
||||||
CompressorQuality.MEDIUM -> 60
|
|
||||||
CompressorQuality.HIGH -> 80
|
|
||||||
CompressorQuality.VERY_HIGH -> 90
|
|
||||||
else -> 60
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
Log.d("MediaCompressor", "Using image compression $mediaQuality")
|
|
||||||
val tempFile = from(uri, contentType, applicationContext)
|
|
||||||
val compressedImageFile =
|
|
||||||
Compressor.compress(applicationContext, tempFile) {
|
|
||||||
default(width = 640, format = Bitmap.CompressFormat.JPEG, quality = imageQuality)
|
|
||||||
}
|
|
||||||
Log.d("MediaCompressor", "Image compression success. Original size [${tempFile.length()}], new size [${compressedImageFile.length()}]")
|
|
||||||
onReady(compressedImageFile.toUri(), contentType, compressedImageFile.length())
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.d("MediaCompressor", "Image compression failed: ${e.message}")
|
|
||||||
if (e is CancellationException) throw e
|
|
||||||
e.printStackTrace()
|
|
||||||
onReady(uri, contentType, null)
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
|
Log.d("MediaCompressor", "Using video compression $mediaQuality")
|
||||||
|
|
||||||
|
VideoCompressor.start(
|
||||||
|
// => This is required
|
||||||
|
context = applicationContext,
|
||||||
|
// => Source can be provided as content uris
|
||||||
|
uris = listOf(uri),
|
||||||
|
isStreamable = false,
|
||||||
|
// THIS STORAGE
|
||||||
|
// sharedStorageConfiguration = SharedStorageConfiguration(
|
||||||
|
// saveAt = SaveLocation.movies, // => default is movies
|
||||||
|
// videoName = "compressed_video" // => required name
|
||||||
|
// ),
|
||||||
|
// OR AND NOT BOTH
|
||||||
|
appSpecificStorageConfiguration = AppSpecificStorageConfiguration(),
|
||||||
|
configureWith =
|
||||||
|
Configuration(
|
||||||
|
quality = videoQuality,
|
||||||
|
// => required name
|
||||||
|
videoNames = listOf(UUID.randomUUID().toString()),
|
||||||
|
),
|
||||||
|
listener =
|
||||||
|
object : CompressionListener {
|
||||||
|
override fun onProgress(
|
||||||
|
index: Int,
|
||||||
|
percent: Float,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onStart(index: Int) {}
|
||||||
|
|
||||||
|
override fun onSuccess(
|
||||||
|
index: Int,
|
||||||
|
size: Long,
|
||||||
|
path: String?,
|
||||||
|
) {
|
||||||
|
if (path != null) {
|
||||||
|
Log.d("MediaCompressor", "Video compression success. Compressed size [$size]")
|
||||||
|
onReady(Uri.fromFile(File(path)), contentType, size)
|
||||||
|
} else {
|
||||||
|
Log.d("MediaCompressor", "Video compression successful, but returned null path")
|
||||||
|
onError(R.string.compression_returned_null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onFailure(
|
||||||
|
index: Int,
|
||||||
|
failureMessage: String,
|
||||||
|
) {
|
||||||
|
Log.d("MediaCompressor", "Video compression failed: $failureMessage")
|
||||||
|
// keeps going with original video
|
||||||
|
onReady(uri, contentType, null)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCancelled(index: Int) {
|
||||||
|
onError(R.string.compression_cancelled)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
CompressorQuality.LOW -> 50
|
||||||
|
CompressorQuality.MEDIUM -> 60
|
||||||
|
CompressorQuality.HIGH -> 80
|
||||||
|
CompressorQuality.VERY_HIGH -> 90
|
||||||
|
else -> 60
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Log.d("MediaCompressor", "Using image compression $mediaQuality")
|
||||||
|
val tempFile = from(uri, contentType, context)
|
||||||
|
val compressedImageFile =
|
||||||
|
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()}]")
|
||||||
|
onReady(compressedImageFile.toUri(), contentType, compressedImageFile.length())
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.d("MediaCompressor", "Image compression failed: ${e.message}")
|
||||||
|
if (e is CancellationException) throw e
|
||||||
|
e.printStackTrace()
|
||||||
onReady(uri, contentType, null)
|
onReady(uri, contentType, null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user