Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String, Any>?): 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<String, Any>?): InputStream? {
return if (uri.startsWith("data:")) {
val src = uri.substring(uri.indexOf(",") + 1)
Expand Down Expand Up @@ -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)
Expand Down
Loading