From 0ce01b95434fec2188aac96129d8f330289241cd Mon Sep 17 00:00:00 2001 From: Muhammad Aditya Hilmy Date: Tue, 11 Jan 2022 17:52:50 +0700 Subject: [PATCH] fix: Fix image rotated when flipped in Android (#719) --- .../mrousavy/camera/utils/ImageProxy.save.kt | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/mrousavy/camera/utils/ImageProxy.save.kt b/android/src/main/java/com/mrousavy/camera/utils/ImageProxy.save.kt index 9b00a84..73deec4 100644 --- a/android/src/main/java/com/mrousavy/camera/utils/ImageProxy.save.kt +++ b/android/src/main/java/com/mrousavy/camera/utils/ImageProxy.save.kt @@ -6,6 +6,7 @@ import android.graphics.ImageFormat import android.graphics.Matrix import android.util.Log import androidx.camera.core.ImageProxy +import androidx.exifinterface.media.ExifInterface import com.mrousavy.camera.CameraView import com.mrousavy.camera.InvalidFormatError import java.io.ByteArrayOutputStream @@ -43,7 +44,34 @@ fun flip(imageBytes: ByteArray, imageWidth: Int): ByteArray { fun flipImage(imageBytes: ByteArray): ByteArray { val bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size) val matrix = Matrix() - matrix.preScale(-1f, 1f) + + val exif = ExifInterface(imageBytes.inputStream()) + val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED) + + when (orientation) { + ExifInterface.ORIENTATION_ROTATE_180 -> { + matrix.setRotate(180f) + matrix.postScale(-1f, 1f) + } + ExifInterface.ORIENTATION_FLIP_VERTICAL -> { + matrix.setRotate(180f) + } + ExifInterface.ORIENTATION_TRANSPOSE -> { + matrix.setRotate(90f) + } + ExifInterface.ORIENTATION_ROTATE_90 -> { + matrix.setRotate(90f) + matrix.postScale(-1f, 1f) + } + ExifInterface.ORIENTATION_TRANSVERSE -> { + matrix.setRotate(-90f) + } + ExifInterface.ORIENTATION_ROTATE_270 -> { + matrix.setRotate(-90f) + matrix.postScale(-1f, 1f) + } + } + val newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true) val stream = ByteArrayOutputStream() newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)