Mercurial > cgi-bin > hgweb.cgi > SimpleResizer
comparison app/src/main/java/com/bartsent/simpleresizer/EditImage.kt @ 5:247e03baf77c
Fix rotation.
author | David Barts <n5jrn@me.com> |
---|---|
date | Wed, 10 Feb 2021 16:31:01 -0800 |
parents | b6a217c850fb |
children | e8059b166de1 |
comparison
equal
deleted
inserted
replaced
4:b6a217c850fb | 5:247e03baf77c |
---|---|
15 import android.widget.EditText | 15 import android.widget.EditText |
16 import androidx.appcompat.app.AlertDialog | 16 import androidx.appcompat.app.AlertDialog |
17 import androidx.appcompat.app.AppCompatActivity | 17 import androidx.appcompat.app.AppCompatActivity |
18 import androidx.appcompat.widget.PopupMenu | 18 import androidx.appcompat.widget.PopupMenu |
19 import com.bartsent.simpleresizer.databinding.ActivityEditImageBinding | 19 import com.bartsent.simpleresizer.databinding.ActivityEditImageBinding |
20 import java.io.File | |
20 import java.io.IOException | 21 import java.io.IOException |
21 import kotlin.math.roundToInt | 22 import kotlin.math.roundToInt |
22 | 23 |
23 class EditImage : AppCompatActivity() { | 24 class EditImage : AppCompatActivity() { |
24 object State { | 25 private object State { |
25 var uri: Uri? = null | 26 var uri: Uri? = null |
26 var bitmap: Bitmap? = null | 27 var bitmap: Bitmap? = null |
27 } | 28 } |
28 | 29 |
29 private val STDDIMS = arrayOf<Int>(1600, 1280, 1024, 800, 640, 512, 400, 320).apply { | 30 private val STDDIMS = arrayOf<Int>(1600, 1280, 1024, 800, 640, 512, 400, 320).apply { |
203 } | 204 } |
204 } | 205 } |
205 | 206 |
206 fun doRotate(deg: Int): Unit { | 207 fun doRotate(deg: Int): Unit { |
207 val oldBitmap = State.bitmap!! | 208 val oldBitmap = State.bitmap!! |
208 val newBitmap = when (deg) { | 209 if (deg % 90 != 0) { |
209 90, 270 -> Bitmap.createBitmap(oldBitmap.height, oldBitmap.width, oldBitmap.config) | 210 throw IllegalArgumentException("$deg not a multiple of 90") |
210 180 -> Bitmap.createBitmap(oldBitmap.width, oldBitmap.height, oldBitmap.config) | 211 } |
211 else -> throw IllegalArgumentException("deg must be 90, 180, or 270") | 212 val (w, h) = if (deg % 180 == 0) Pair(oldBitmap.width, oldBitmap.height) else Pair(oldBitmap.height, oldBitmap.width) |
212 } | 213 val newBitmap = Bitmap.createBitmap(w, h, oldBitmap.config) |
213 copyColorSpace(oldBitmap, newBitmap) | 214 copyColorSpace(oldBitmap, newBitmap) |
214 val rotater = Matrix().apply { | 215 val rotater = Matrix().apply { |
215 setRotate(deg.toFloat(), oldBitmap.width.toFloat()/2.0f, oldBitmap.height.toFloat()/2.0f) | 216 setRotate(deg.toFloat(), oldBitmap.width.toFloat()/2.0f, oldBitmap.height.toFloat()/2.0f) |
217 postTranslate((w - oldBitmap.width).toFloat()/2.0f, (h - oldBitmap.height).toFloat()/2.0f) | |
216 } | 218 } |
217 Canvas(newBitmap).run { | 219 Canvas(newBitmap).run { |
218 drawBitmap(oldBitmap, rotater, null) | 220 drawBitmap(oldBitmap, rotater, null) |
219 } | 221 } |
220 setImage(newBitmap) | 222 setImage(newBitmap) |
243 } | 245 } |
244 } | 246 } |
245 put(MediaStore.MediaColumns.DISPLAY_NAME, fileName) | 247 put(MediaStore.MediaColumns.DISPLAY_NAME, fileName) |
246 put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg") | 248 put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg") |
247 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) { | 249 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) { |
248 put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES) | 250 put(MediaStore.MediaColumns.RELATIVE_PATH, |
251 File(Environment.DIRECTORY_PICTURES, getString(R.string.app_name)).getPath()) | |
249 } | 252 } |
250 } | 253 } |
251 try { | 254 try { |
252 val myUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues) | 255 val myUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues) |
253 if (myUri == null) { | 256 if (myUri == null) { |
264 } | 267 } |
265 } | 268 } |
266 } catch (ioe: IOException) { | 269 } catch (ioe: IOException) { |
267 showError(ioe.message ?: getString(R.string.error_io)) | 270 showError(ioe.message ?: getString(R.string.error_io)) |
268 } | 271 } |
272 finish() | |
269 } | 273 } |
270 } | 274 } |