@@ -19,6 +19,7 @@ package io.element.android.libraries.mediaupload
19
19
import android.content.Context
20
20
import android.graphics.Bitmap
21
21
import android.graphics.BitmapFactory
22
+ import androidx.exifinterface.media.ExifInterface
22
23
import io.element.android.libraries.androidutils.bitmap.calculateInSampleSize
23
24
import io.element.android.libraries.androidutils.bitmap.resizeToMax
24
25
import io.element.android.libraries.androidutils.bitmap.rotateToMetadataOrientation
@@ -37,17 +38,18 @@ class ImageCompressor @Inject constructor(
37
38
38
39
/* *
39
40
* Decodes the [inputStream] into a [Bitmap] and applies the needed transformations (rotation, scale) based on [resizeMode], then writes it into a
40
- * temporary file using the passed [format] and [desiredQuality].
41
+ * temporary file using the passed [format], [orientation] and [desiredQuality].
41
42
* @return a [Result] containing the resulting [ImageCompressionResult] with the temporary [File] and some metadata.
42
43
*/
43
44
suspend fun compressToTmpFile (
44
45
inputStream : InputStream ,
45
46
resizeMode : ResizeMode ,
46
47
format : Bitmap .CompressFormat = Bitmap .CompressFormat .JPEG ,
48
+ orientation : Int = ExifInterface .ORIENTATION_UNDEFINED ,
47
49
desiredQuality : Int = 80,
48
50
): Result <ImageCompressionResult > = withContext(Dispatchers .IO ) {
49
51
runCatching {
50
- val compressedBitmap = compressToBitmap(inputStream, resizeMode).getOrThrow()
52
+ val compressedBitmap = compressToBitmap(inputStream, resizeMode, orientation ).getOrThrow()
51
53
// Encode bitmap to the destination temporary file
52
54
val tmpFile = context.createTmpFile(extension = " jpeg" )
53
55
tmpFile.outputStream().use {
@@ -63,19 +65,20 @@ class ImageCompressor @Inject constructor(
63
65
}
64
66
65
67
/* *
66
- * Decodes the [inputStream] into a [Bitmap] and applies the needed transformations (rotation, scale) based on [resizeMode].
68
+ * Decodes the [inputStream] into a [Bitmap] and applies the needed transformations (rotation, scale) based on [resizeMode] and [orientation] .
67
69
* @return a [Result] containing the resulting [Bitmap].
68
70
*/
69
71
fun compressToBitmap (
70
72
inputStream : InputStream ,
71
73
resizeMode : ResizeMode ,
74
+ orientation : Int ,
72
75
): Result <Bitmap > = runCatching {
73
76
BufferedInputStream (inputStream).use { input ->
74
77
val options = BitmapFactory .Options ()
75
78
calculateDecodingScale(input, resizeMode, options)
76
79
val decodedBitmap = BitmapFactory .decodeStream(input, null , options)
77
80
? : error(" Decoding Bitmap from InputStream failed" )
78
- val rotatedBitmap = decodedBitmap.rotateToMetadataOrientation(input).getOrThrow( )
81
+ val rotatedBitmap = decodedBitmap.rotateToMetadataOrientation(orientation )
79
82
if (resizeMode is ResizeMode .Strict ) {
80
83
rotatedBitmap.resizeToMax(resizeMode.maxWidth, resizeMode.maxHeight)
81
84
} else {
0 commit comments