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 7c982a30cb
commit 33aa7a914f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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].
* 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()
when (orientation) {
ExifInterface.ORIENTATION_ROTATE_270 -> matrix.postRotate(270f)

View file

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

View file

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