https://developer.android.com/jetpack/androidx/releases/exifinterface
dependencies {
implementation("androidx.exifinterface:exifinterface:1.3.3")
}
https://developer.android.com/reference/kotlin/androidx/exifinterface/media/ExifInterface
import androidx.exifinterface.media.ExifInterface
fun decodeExifOrientation(exifOrientation: Int): Matrix {
val matrix = Matrix()
// Apply transformation corresponding to declared EXIF orientation
when (exifOrientation) {
ExifInterface.ORIENTATION_NORMAL -> Unit
ExifInterface.ORIENTATION_UNDEFINED -> Unit
ExifInterface.ORIENTATION_ROTATE_90 -> matrix.postRotate(90F)
ExifInterface.ORIENTATION_ROTATE_180 -> matrix.postRotate(180F)
ExifInterface.ORIENTATION_ROTATE_270 -> matrix.postRotate(270F)
ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> matrix.postScale(-1F, 1F)
ExifInterface.ORIENTATION_FLIP_VERTICAL -> matrix.postScale(1F, -1F)
ExifInterface.ORIENTATION_TRANSPOSE -> {
matrix.postScale(-1F, 1F)
matrix.postRotate(270F)
}
ExifInterface.ORIENTATION_TRANSVERSE -> {
matrix.postScale(-1F, 1F)
matrix.postRotate(90F)
}
// Error out if the EXIF orientation is invalid
else -> Log.e("UIViewModel", "Invalid orientation: $exifOrientation")
}
// Return the resulting matrix
return matrix
}
val exifInterface = ExifInterface(File(filesDir, filename))
val orientation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED
)
val matrix = decodeExifOrientation(orientation)
val rotatedImg: Bitmap = Bitmap.createBitmap(
bitmap,0,0,bitmap.width,bitmap.height,matrix,true)