aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/util/BitmapOptimizer.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/net/lacolaco/smileessence/util/BitmapOptimizer.kt')
-rw-r--r--app/src/main/java/net/lacolaco/smileessence/util/BitmapOptimizer.kt122
1 files changed, 122 insertions, 0 deletions
diff --git a/app/src/main/java/net/lacolaco/smileessence/util/BitmapOptimizer.kt b/app/src/main/java/net/lacolaco/smileessence/util/BitmapOptimizer.kt
new file mode 100644
index 00000000..b49c9f08
--- /dev/null
+++ b/app/src/main/java/net/lacolaco/smileessence/util/BitmapOptimizer.kt
@@ -0,0 +1,122 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2012-2014 lacolaco.net
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package net.lacolaco.smileessence.util
+
+import android.app.Activity
+import android.graphics.Bitmap
+import android.graphics.BitmapFactory
+import android.graphics.Matrix
+import android.media.ExifInterface
+import android.os.Environment
+import net.lacolaco.smileessence.logging.Logger
+
+import java.io.File
+import java.io.FileOutputStream
+import java.io.IOException
+import java.io.OutputStream
+
+object BitmapOptimizer {
+
+ // -------------------------- STATIC METHODS --------------------------
+
+ fun rotateImageByExif(activity: Activity, filePath: String): String {
+ var filePath = filePath
+ filePath = filePath.replace("file://", "")
+ val degree = getRotateDegreeFromExif(filePath)
+ if (degree > 0) {
+ var out: OutputStream? = null
+ var bitmap: Bitmap? = null
+ var rotatedImage: Bitmap? = null
+ try {
+ val mat = Matrix()
+ mat.postRotate(degree.toFloat())
+ val opt = BitmapFactory.Options()
+ opt.inJustDecodeBounds = true
+ BitmapFactory.decodeFile(filePath, opt)
+ val width = 480
+ var scale = 1
+ if (opt.outWidth > width) {
+ scale = opt.outWidth / width + 2
+ }
+ opt.inJustDecodeBounds = false
+ opt.inSampleSize = scale
+ bitmap = BitmapFactory.decodeFile(filePath, opt)
+ rotatedImage = Bitmap.createBitmap(bitmap, 0, 0, bitmap!!.width, bitmap.height, mat, true)
+ val file = File(filePath)
+ val outPath = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES).toString() + "/" + file.name
+ Logger.debug(outPath)
+ out = FileOutputStream(outPath)
+ rotatedImage!!.compress(Bitmap.CompressFormat.JPEG, 100, out)
+ return outPath
+ } catch (e: Exception) {
+ e.printStackTrace()
+ Logger.error(e)
+ } finally {
+ if (out != null) {
+ try {
+ out.close()
+ } catch (e: IOException) {
+ e.printStackTrace()
+ Logger.error(e)
+ }
+
+ }
+ if (bitmap != null) {
+ bitmap.recycle()
+ }
+ if (rotatedImage != null) {
+ rotatedImage.recycle()
+ }
+ }
+ }
+ return filePath
+ }
+
+ private fun getRotateDegreeFromExif(filePath: String): Int {
+ var degree = 0
+ try {
+ val exifInterface = ExifInterface(filePath)
+ val orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED)
+ if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
+ degree = 90
+ } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
+ degree = 180
+ } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
+ degree = 270
+ }
+ if (degree != 0) {
+ exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION, "0")
+ exifInterface.saveAttributes()
+ }
+ } catch (e: IOException) {
+ degree = -1
+ e.printStackTrace()
+ Logger.error(e)
+ }
+
+ Logger.debug("Exif degree = " + degree)
+ return degree
+ }
+}