Calculate video output size taking into account portrait mode (#5068)

This commit is contained in:
Jorge Martin Espinosa 2025-07-23 14:19:08 +02:00 committed by GitHub
parent b50e5430cb
commit 6c184076a9
3 changed files with 9 additions and 5 deletions

View file

@ -64,7 +64,7 @@ fun BitmapFactory.Options.calculateInSampleSize(desiredWidth: Int, desiredHeight
* Decodes the [inputStream] into a [Bitmap] and applies the needed rotation based on [orientation]. * Decodes the [inputStream] into a [Bitmap] and applies the needed rotation based on [orientation].
* This orientation value must be one of `ExifInterface.ORIENTATION_*` constants. * This orientation value must be one of `ExifInterface.ORIENTATION_*` constants.
*/ */
fun Bitmap.rotateToMetadataOrientation(orientation: Int): Bitmap { fun Bitmap.rotateToExifMetadataOrientation(orientation: Int): Bitmap {
val matrix = Matrix() val matrix = Matrix()
when (orientation) { when (orientation) {
ExifInterface.ORIENTATION_ROTATE_270 -> matrix.postRotate(270f) ExifInterface.ORIENTATION_ROTATE_270 -> matrix.postRotate(270f)

View file

@ -13,7 +13,7 @@ import android.graphics.BitmapFactory
import androidx.exifinterface.media.ExifInterface import androidx.exifinterface.media.ExifInterface
import io.element.android.libraries.androidutils.bitmap.calculateInSampleSize import io.element.android.libraries.androidutils.bitmap.calculateInSampleSize
import io.element.android.libraries.androidutils.bitmap.resizeToMax import io.element.android.libraries.androidutils.bitmap.resizeToMax
import io.element.android.libraries.androidutils.bitmap.rotateToMetadataOrientation import io.element.android.libraries.androidutils.bitmap.rotateToExifMetadataOrientation
import io.element.android.libraries.androidutils.file.createTmpFile import io.element.android.libraries.androidutils.file.createTmpFile
import io.element.android.libraries.core.coroutine.CoroutineDispatchers import io.element.android.libraries.core.coroutine.CoroutineDispatchers
import io.element.android.libraries.core.extensions.runCatchingExceptions import io.element.android.libraries.core.extensions.runCatchingExceptions
@ -78,7 +78,7 @@ class ImageCompressor @Inject constructor(
options.inJustDecodeBounds = false options.inJustDecodeBounds = false
val decodedBitmap = BitmapFactory.decodeStream(input, null, options) val decodedBitmap = BitmapFactory.decodeStream(input, null, options)
?: error("Decoding Bitmap from InputStream failed") ?: error("Decoding Bitmap from InputStream failed")
val rotatedBitmap = decodedBitmap.rotateToMetadataOrientation(orientation) val rotatedBitmap = decodedBitmap.rotateToExifMetadataOrientation(orientation)
if (resizeMode is ResizeMode.Strict) { if (resizeMode is ResizeMode.Strict) {
rotatedBitmap.resizeToMax(resizeMode.maxWidth, resizeMode.maxHeight) rotatedBitmap.resizeToMax(resizeMode.maxWidth, resizeMode.maxHeight)
} else { } else {

View file

@ -81,8 +81,12 @@ internal class VideoResizer(
) { ) {
fun getOutputSize(inputSize: Size): Size { fun getOutputSize(inputSize: Size): Size {
val resultMajor = min(inputSize.major(), maxSize) val resultMajor = min(inputSize.major(), maxSize)
val aspectRatio = inputSize.major().toFloat() / inputSize.minor().toFloat() val aspectRatio = inputSize.width.toFloat() / inputSize.height.toFloat()
return Size(resultMajor, (resultMajor / aspectRatio).roundToInt()) return if (inputSize.width > inputSize.height) {
Size(resultMajor, (resultMajor / aspectRatio).roundToInt())
} else {
Size((resultMajor * aspectRatio).roundToInt(), resultMajor)
}
} }
} }