diff --git a/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt b/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt index 53a92f3..04b592e 100644 --- a/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt +++ b/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt @@ -35,6 +35,7 @@ import com.facebook.react.bridge.ReadableMap import com.facebook.react.bridge.ReadableType import com.facebook.react.bridge.WritableMap import com.facebook.react.common.ReactConstants +import java.io.BufferedInputStream import java.io.ByteArrayInputStream import java.io.File import java.io.FileInputStream @@ -223,7 +224,7 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) { val imageHeight: Int = decoder.height val imageWidth: Int = decoder.width - val orientation = getOrientation(reactContext, Uri.parse(uri)) + val orientation = getOrientation(Uri.parse(uri), headers) val (left, top) = when (orientation) { @@ -285,7 +286,7 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) { @Suppress("DEPRECATION") BitmapRegionDecoder.newInstance(it, false) } ?: throw Error("Could not create bitmap decoder. Uri: $uri") - val orientation = getOrientation(reactContext, Uri.parse(uri)) + val orientation = getOrientation(Uri.parse(uri), headers) val (x, y) = when (orientation) { 90 -> yPos to decoder.height - rectWidth - xPos @@ -350,6 +351,32 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) { } } + /** + * The rotation in degrees the source image has to be rotated clockwise by to be displayed + * upright, or 0 when it carries no such information. Read from the image data itself, so it + * works for remote and `data:` URIs as well as files. + */ + private fun getOrientation(uri: Uri, headers: HashMap?): Int { + val exif = + try { + openBitmapInputStream(uri.toString(), headers)?.let { input -> + input.use { ExifInterface(BufferedInputStream(it)) } + } + } catch (e: IOException) { + // Images without Exif data are not decodable as Exif: treat them as upright + // rather than failing the crop. + null + } ?: return 0 + return when ( + exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL) + ) { + ExifInterface.ORIENTATION_ROTATE_90 -> 90 + ExifInterface.ORIENTATION_ROTATE_180 -> 180 + ExifInterface.ORIENTATION_ROTATE_270 -> 270 + else -> 0 + } + } + private fun openBitmapInputStream(uri: String, headers: HashMap?): InputStream? { return if (uri.startsWith("data:")) { val src = uri.substring(uri.indexOf(",") + 1) @@ -564,25 +591,6 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) { return mimeType } - private fun getOrientation(context: Context, uri: Uri): Int { - val file = getFileFromUri(context, uri) - if (file == null) { - return 0 - } - val exif = ExifInterface(file.absolutePath) - return when ( - exif.getAttributeInt( - ExifInterface.TAG_ORIENTATION, - ExifInterface.ORIENTATION_NORMAL - ) - ) { - ExifInterface.ORIENTATION_ROTATE_90 -> 90 - ExifInterface.ORIENTATION_ROTATE_180 -> 180 - ExifInterface.ORIENTATION_ROTATE_270 -> 270 - else -> 0 - } - } - @Throws(IOException::class) private fun copyExif(context: Context, oldImage: Uri, newFile: File) { val oldFile = getFileFromUri(context, oldImage)