rotate images by exif on SD card on Nougat+

This commit is contained in:
tibbi 2018-04-01 11:20:26 +02:00
parent 750ff3f782
commit 1bade23297
2 changed files with 26 additions and 26 deletions

View File

@ -45,7 +45,7 @@ ext {
} }
dependencies { dependencies {
implementation 'com.simplemobiletools:commons:3.17.19' implementation 'com.simplemobiletools:commons:3.17.21'
debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakCanaryVersion" debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakCanaryVersion"
releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$leakCanaryVersion" releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$leakCanaryVersion"

View File

@ -15,7 +15,11 @@ import com.simplemobiletools.camera.extensions.config
import com.simplemobiletools.camera.extensions.getOutputMediaFile import com.simplemobiletools.camera.extensions.getOutputMediaFile
import com.simplemobiletools.camera.extensions.getPreviewRotation import com.simplemobiletools.camera.extensions.getPreviewRotation
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import java.io.* import com.simplemobiletools.commons.helpers.isNougatPlus
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.OutputStream
class PhotoProcessor(val activity: MainActivity, val uri: Uri?, val currCameraId: Int, val deviceOrientation: Int) : AsyncTask<ByteArray, Void, String>() { class PhotoProcessor(val activity: MainActivity, val uri: Uri?, val currCameraId: Int, val deviceOrientation: Int) : AsyncTask<ByteArray, Void, String>() {
@ -70,15 +74,11 @@ class PhotoProcessor(val activity: MainActivity, val uri: Uri?, val currCameraId
val deviceRot = deviceOrientation.compensateDeviceRotation(currCameraId) val deviceRot = deviceOrientation.compensateDeviceRotation(currCameraId)
val previewRot = activity.getPreviewRotation(currCameraId) val previewRot = activity.getPreviewRotation(currCameraId)
val imageRot = when (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED)) { val orient = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED)
ExifInterface.ORIENTATION_ROTATE_90 -> 90 val imageRot = orient.degreesFromOrientation()
ExifInterface.ORIENTATION_ROTATE_180 -> 180
ExifInterface.ORIENTATION_ROTATE_270 -> 270
else -> 0
}
val totalRotation = (imageRot + deviceRot + previewRot) % 360 val totalRotation = (imageRot + deviceRot + previewRot) % 360
if (!path.startsWith(activity.internalStoragePath)) { if (!path.startsWith(activity.internalStoragePath) && !isNougatPlus()) {
image = rotate(image, totalRotation) image = rotate(image, totalRotation)
} }
@ -97,23 +97,24 @@ class PhotoProcessor(val activity: MainActivity, val uri: Uri?, val currCameraId
} }
} }
image.compress(Bitmap.CompressFormat.JPEG, activity.config.photoQuality, fos)
val fileExif = ExifInterface(path) val fileExif = ExifInterface(path)
var exifOrientation = ExifInterface.ORIENTATION_NORMAL.toString()
image.compress(Bitmap.CompressFormat.JPEG, activity.config.photoQuality, fos)
if (path.startsWith(activity.internalStoragePath)) { if (path.startsWith(activity.internalStoragePath)) {
exifOrientation = getExifOrientation(totalRotation) saveExifRotation(ExifInterface(path), totalRotation)
} else if (isNougatPlus()) {
val documentFile = activity.getSomeDocumentFile(path)
if (documentFile != null) {
val parcelFileDescriptor = activity.contentResolver.openFileDescriptor(documentFile.uri, "rw")
val fileDescriptor = parcelFileDescriptor.fileDescriptor
saveExifRotation(ExifInterface(fileDescriptor), totalRotation)
}
} }
if (activity.config.savePhotoMetadata) { if (activity.config.savePhotoMetadata) {
tempExif.copyTo(fileExif) tempExif.copyTo(fileExif)
} }
fileExif.setAttribute(ExifInterface.TAG_ORIENTATION, exifOrientation)
try {
fileExif.saveAttributes()
} catch (e: IOException) {
}
return photoFile.absolutePath return photoFile.absolutePath
} catch (e: FileNotFoundException) { } catch (e: FileNotFoundException) {
} finally { } finally {
@ -123,18 +124,17 @@ class PhotoProcessor(val activity: MainActivity, val uri: Uri?, val currCameraId
return "" return ""
} }
private fun getExifOrientation(degrees: Int): String { private fun saveExifRotation(exif: ExifInterface, degrees: Int) {
return when (degrees) { val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
90 -> ExifInterface.ORIENTATION_ROTATE_90 val orientationDegrees = (orientation.degreesFromOrientation() + degrees) % 360
180 -> ExifInterface.ORIENTATION_ROTATE_180 exif.setAttribute(ExifInterface.TAG_ORIENTATION, orientationDegrees.orientationFromDegrees())
270 -> ExifInterface.ORIENTATION_ROTATE_270 exif.saveAttributes()
else -> ExifInterface.ORIENTATION_NORMAL
}.toString()
} }
private fun rotate(bitmap: Bitmap, degree: Int): Bitmap? { private fun rotate(bitmap: Bitmap, degree: Int): Bitmap? {
if (degree == 0) if (degree == 0) {
return bitmap return bitmap
}
val width = bitmap.width val width = bitmap.width
val height = bitmap.height val height = bitmap.height