Merge pull request #427 from ltGuillaume/master

Lossless rotation for JPEG
This commit is contained in:
Tibor Kaputa 2017-11-01 21:25:19 +01:00 committed by GitHub
commit 0cc2c3d8f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -45,7 +45,9 @@ import com.simplemobiletools.gallery.helpers.*
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import kotlinx.android.synthetic.main.activity_medium.* import kotlinx.android.synthetic.main.activity_medium.*
import java.io.File import java.io.File
import java.io.OutputStream import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.FileNotFoundException
import java.util.* import java.util.*
class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, ViewPagerFragment.FragmentListener { class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, ViewPagerFragment.FragmentListener {
@ -463,7 +465,12 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
return@getFileOutputStream return@getFileOutputStream
} }
saveFile(tmpFile, bitmap, it) if (currPath.isJpg()) {
saveRotation(currPath, tmpFile)
} else {
saveFile(tmpFile, bitmap, it as FileOutputStream)
}
if (needsStupidWritePermissions(selectedFile.absolutePath)) { if (needsStupidWritePermissions(selectedFile.absolutePath)) {
deleteFile(selectedFile) {} deleteFile(selectedFile) {}
} }
@ -471,6 +478,12 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
renameFile(tmpFile, selectedFile) { renameFile(tmpFile, selectedFile) {
deleteFile(tmpFile) {} deleteFile(tmpFile) {}
} }
it.flush()
it.close()
toast(R.string.file_saved)
mRotationDegrees = 0f
invalidateOptionsMenu()
} }
} catch (e: OutOfMemoryError) { } catch (e: OutOfMemoryError) {
toast(R.string.out_of_memory_error) toast(R.string.out_of_memory_error)
@ -483,18 +496,48 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
} }
} }
private fun saveFile(file: File, bitmap: Bitmap, out: OutputStream) { private fun saveFile(file: File, bitmap: Bitmap, out: FileOutputStream) {
val matrix = Matrix() val matrix = Matrix()
matrix.postRotate(mRotationDegrees) matrix.postRotate(mRotationDegrees)
val bmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true) val bmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
bmp.compress(file.getCompressionFormat(), 90, out) bmp.compress(file.getCompressionFormat(), 90, out)
out.flush()
out.close()
toast(R.string.file_saved)
mRotationDegrees = 0f
invalidateOptionsMenu()
} }
private fun saveRotation(input: String, out: File) {
var inputStream: FileInputStream? = null
var outputStream: FileOutputStream? = null
try {
inputStream = FileInputStream(input)
outputStream = FileOutputStream(out)
inputStream.copyTo(outputStream)
} catch (ignored: FileNotFoundException) {
} finally {
inputStream?.close()
outputStream?.close()
}
if (out.exists()) {
val exif = ExifInterface(out.absolutePath)
var orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
var orientationDegrees = (degreesForRotation(orientation) + mRotationDegrees) % 360
exif.setAttribute(ExifInterface.TAG_ORIENTATION, rotationFromDegrees(orientationDegrees))
exif.saveAttributes()
}
}
private fun degreesForRotation(orientation: Int) = when (orientation) {
ExifInterface.ORIENTATION_ROTATE_270 -> 270f
ExifInterface.ORIENTATION_ROTATE_180 -> 180f
ExifInterface.ORIENTATION_ROTATE_90 -> 90f
else -> 0f
}
private fun rotationFromDegrees(degrees: Float) = when (degrees) {
270f -> ExifInterface.ORIENTATION_ROTATE_270
180f -> ExifInterface.ORIENTATION_ROTATE_180
90f -> ExifInterface.ORIENTATION_ROTATE_90
else -> ExifInterface.ORIENTATION_NORMAL
}.toString()
private fun isShowHiddenFlagNeeded(): Boolean { private fun isShowHiddenFlagNeeded(): Boolean {
val file = File(mPath) val file = File(mPath)
if (file.isHidden) if (file.isHidden)