diff app/src/main/java/com/bartsent/simpleresizer/EditImage.kt @ 15:20da616dcda0

Add preferences.
author David Barts <n5jrn@me.com>
date Thu, 18 Feb 2021 22:12:19 -0800
parents 678adef4774f
children 3ed74dc0e34a
line wrap: on
line diff
--- a/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Thu Feb 18 15:19:07 2021 -0800
+++ b/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Thu Feb 18 22:12:19 2021 -0800
@@ -1,6 +1,8 @@
 package com.bartsent.simpleresizer
 
+import android.annotation.SuppressLint
 import android.content.ContentValues
+import android.content.Intent
 import android.graphics.Bitmap
 import android.graphics.BitmapFactory
 import android.graphics.Canvas
@@ -10,6 +12,8 @@
 import android.os.Environment
 import android.provider.MediaStore
 import android.provider.OpenableColumns
+import android.util.Log
+import android.view.Menu
 import android.view.MenuItem
 import android.view.View
 import android.widget.EditText
@@ -22,7 +26,7 @@
 import java.io.File
 import java.io.IOException
 import kotlin.concurrent.thread
-import kotlin.math.roundToInt
+import androidx.preference.PreferenceManager
 
 class EditImage : AppCompatActivity() {
     private object State {
@@ -40,6 +44,22 @@
         super.onCreate(savedInstanceState)
         binding = ActivityEditImageBinding.inflate(layoutInflater)
         setContentView(binding.root)
+        PreferenceManager.setDefaultValues(applicationContext, R.xml.root_preferences, false)
+    }
+
+    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
+        menuInflater.inflate(R.menu.menu_edit, menu)
+        return super.onCreateOptionsMenu(menu)
+    }
+
+    override fun onOptionsItemSelected(item: MenuItem): Boolean {
+        if (item.itemId == R.id.settings_item) {
+            startActivity(
+                Intent(Intent.ACTION_APPLICATION_PREFERENCES, null, this,
+                    SettingsActivity::class.java))
+            return true
+        }
+        return false
     }
 
     // Cribbed from: https://stackoverflow.com/questions/5568874/how-to-extract-the-file-name-from-uri-returned-from-intent-action-get-content
@@ -142,11 +162,17 @@
         if (factor >= 1.0) {
             throw IllegalArgumentException("can only scale down")
         }
+        val scaleType = PreferenceManager.getDefaultSharedPreferences(applicationContext).getString(
+            "scale_type", "speed" )
+        Log.d("EditImage", "scaling, scale_type = $scaleType")
         binding.progressBar.visibility = ProgressBar.VISIBLE
         thread {
-            val newBitmap = oldBitmap.getScaledInstance(
-                    (oldBitmap.width.toDouble() * factor + 0.5).toInt(),
-                    (oldBitmap.height.toDouble() * factor + 0.5).toInt())
+            val newWidth = (oldBitmap.width.toDouble() * factor + 0.5).toInt()
+            val newHeight = (oldBitmap.height.toDouble() * factor + 0.5).toInt()
+            val newBitmap = if (scaleType == "quality")
+                oldBitmap.getScaledInstance(newWidth, newHeight)
+            else
+                Bitmap.createScaledBitmap(oldBitmap, newWidth, newHeight, true)
             runOnUiThread {
                 binding.progressBar.visibility = ProgressBar.INVISIBLE
                 setImage(newBitmap)
@@ -272,9 +298,12 @@
             if (stream == null) {
                 throw IOException(getString(R.string.error_get_output))
             }
-            // fixme: use quality from settings
+            val quality = maxOf(0, minOf(100,
+                PreferenceManager.getDefaultSharedPreferences(applicationContext).getInt(
+                    "jpeg_quality", 85)))
+            Log.d("EditImage", "saving, jpeg_quality = $quality")
             stream.use {
-                if (!State.bitmap!!.compress(Bitmap.CompressFormat.JPEG, 85, it)) {
+                if (!State.bitmap!!.compress(Bitmap.CompressFormat.JPEG, quality, it)) {
                     throw IOException(getString(R.string.error_save_bitmap))
                 }
             }