From 767e1ec818589b63f43ccdc379d71480a66a1d81 Mon Sep 17 00:00:00 2001 From: leca Date: Mon, 11 Nov 2024 17:00:21 +0300 Subject: [PATCH] Fixed abstract product image rotation, added image compression --- .../barcodescannerforemployees/Utils.kt | 18 +++++++++---- .../activities/AddAbstractProductActivity.kt | 27 +++++++++++++++++++ .../activities/SettingsActivity.kt | 6 ++--- app/src/main/res/layout/activity_settings.xml | 4 +-- app/src/main/res/values-ru/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 6 files changed, 47 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt index 802c965..07dee21 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt @@ -6,6 +6,7 @@ import android.content.ContextWrapper import android.graphics.Bitmap import android.graphics.BitmapFactory import android.graphics.Matrix +import android.media.ExifInterface import android.net.Uri import android.os.Build import androidx.annotation.RequiresApi @@ -42,11 +43,18 @@ fun generateThumbnailForImage(context: Context, imageHash: String) { val imageContent = imageFile.inputStream().readBytes() var img = BitmapFactory.decodeByteArray(imageContent, 0, imageContent.size) - val matrix = Matrix(); - matrix.postRotate(90f) - val scaled = Bitmap.createScaledBitmap(img, img.width/4, img.height/4, true) - val rotated = Bitmap.createBitmap(scaled, 0, 0, scaled.width, scaled.height, matrix, true) - rotated.compress(Bitmap.CompressFormat.WEBP_LOSSY, 25, FileOutputStream(thumbnailFile)) + val exif = ExifInterface(imageFile.absoluteFile.toString()) + val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL) + val matrix = Matrix() + + when(orientation){ + ExifInterface.ORIENTATION_ROTATE_90 -> matrix.postRotate(90F) + ExifInterface.ORIENTATION_ROTATE_180 -> matrix.postRotate(180F) + ExifInterface.ORIENTATION_ROTATE_270 -> matrix.postRotate(270F) + } + val rotated = Bitmap.createBitmap(img, 0, 0, img.width, img.height, matrix, true) + val scaled = Bitmap.createScaledBitmap(rotated, rotated.width/4, rotated.height/4, true) + scaled.compress(Bitmap.CompressFormat.WEBP_LOSSY, 25, FileOutputStream(thumbnailFile)) } @OptIn(ExperimentalStdlibApi::class) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt index fe526e2..2426f26 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/AddAbstractProductActivity.kt @@ -3,6 +3,10 @@ package org.foxarmy.barcodescannerforemployees.activities import android.content.DialogInterface import android.content.Intent import android.content.SharedPreferences +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.graphics.Matrix +import android.media.ExifInterface import android.os.Build import android.os.Bundle import android.util.Log @@ -25,6 +29,7 @@ import org.foxarmy.barcodescannerforemployees.database.CategoryDAO import org.foxarmy.barcodescannerforemployees.database.DBStorageController import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct import java.io.File +import java.io.FileOutputStream import java.nio.file.Files import java.nio.file.StandardCopyOption import kotlin.concurrent.thread @@ -276,6 +281,28 @@ class AddAbstractProductActivity : AppCompatActivity() { pictureFile = File(picturesPath, "$imageHash.png") Files.move(tempfile.toPath(), pictureFile.toPath(), StandardCopyOption.REPLACE_EXISTING) tempfile.delete() + + val imageContent = pictureFile.inputStream().readBytes() + var img = BitmapFactory.decodeByteArray(imageContent, 0, imageContent.size) + + val exif = ExifInterface(pictureFile.absoluteFile.toString()) + val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL) + val matrix = Matrix() + + when(orientation){ + ExifInterface.ORIENTATION_ROTATE_90 -> matrix.postRotate(90F) + ExifInterface.ORIENTATION_ROTATE_180 -> matrix.postRotate(180F) + ExifInterface.ORIENTATION_ROTATE_270 -> matrix.postRotate(270F) + } + val rotated = Bitmap.createBitmap(img, 0, 0, img.width, img.height, matrix, true) + + val compressionFactor = sharedPreferences.getInt("compression", 1) + rotated.compress( + Bitmap.CompressFormat.WEBP_LOSSY, + 100 * (15 / (16 - compressionFactor)), + FileOutputStream(pictureFile) + ) + generateThumbnailForImage(this, imageHash) imageView.setImageURI(getImageUri(this, pictureFile)) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/SettingsActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/SettingsActivity.kt index f15a957..ae5d172 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/SettingsActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/SettingsActivity.kt @@ -51,7 +51,7 @@ class SettingsActivity : AppCompatActivity() { binding.saveButton.setOnClickListener { sharedPreferences.edit().putString("currentGroup", net.getGroupId(binding.currentGroupSpinner.selectedItem.toString())).apply() - sharedPreferences.edit().putInt("imageCompression", binding.imageCompressionFactorSeekBar.progress) + sharedPreferences.edit().putInt("imageCompression", binding.imageCompressionFactorSeekBar.progress + 1).apply() setResult(Activity.RESULT_OK) finish() } @@ -63,8 +63,8 @@ class SettingsActivity : AppCompatActivity() { } private fun setUpImageCompressionSeekBar() { - val compressionFactor = sharedPreferences.getInt("imageCompression", 0) - binding.imageCompressionFactorSeekBar.progress = compressionFactor + val compressionFactor = sharedPreferences.getInt("imageCompression", 1) + binding.imageCompressionFactorSeekBar.progress = compressionFactor - 1 } private fun fillUpCurrentGroupSpinner() { diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml index 8a67b35..07f9193 100644 --- a/app/src/main/res/layout/activity_settings.xml +++ b/app/src/main/res/layout/activity_settings.xml @@ -10,7 +10,7 @@ app:layout_constraintStart_toStartOf="parent" android:id="@+id/imageCompressionSetting" android:layout_marginTop="16dp"> Leave group Rename group Join or create a group - Image scale factor\n + Image compression factor Current group Cancel diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d39507c..4eb50aa 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -109,7 +109,7 @@ Leave group Rename group Join or create a group - Image scale factor\n + Image compression factor Current group Cancel