rotate images by exif on SD card on Nougat+
This commit is contained in:
parent
750ff3f782
commit
1bade23297
|
@ -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"
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue