Fixed abstract product image rotation, added image compression

This commit is contained in:
leca 2024-11-11 17:00:21 +03:00
parent 3a296e7a26
commit 767e1ec818
6 changed files with 47 additions and 12 deletions

View File

@ -6,6 +6,7 @@ import android.content.ContextWrapper
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.graphics.Matrix import android.graphics.Matrix
import android.media.ExifInterface
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
@ -42,11 +43,18 @@ fun generateThumbnailForImage(context: Context, imageHash: String) {
val imageContent = imageFile.inputStream().readBytes() val imageContent = imageFile.inputStream().readBytes()
var img = BitmapFactory.decodeByteArray(imageContent, 0, imageContent.size) var img = BitmapFactory.decodeByteArray(imageContent, 0, imageContent.size)
val matrix = Matrix(); val exif = ExifInterface(imageFile.absoluteFile.toString())
matrix.postRotate(90f) val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
val scaled = Bitmap.createScaledBitmap(img, img.width/4, img.height/4, true) val matrix = Matrix()
val rotated = Bitmap.createBitmap(scaled, 0, 0, scaled.width, scaled.height, matrix, true)
rotated.compress(Bitmap.CompressFormat.WEBP_LOSSY, 25, FileOutputStream(thumbnailFile)) 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) @OptIn(ExperimentalStdlibApi::class)

View File

@ -3,6 +3,10 @@ package org.foxarmy.barcodescannerforemployees.activities
import android.content.DialogInterface import android.content.DialogInterface
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences 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.Build
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
@ -25,6 +29,7 @@ import org.foxarmy.barcodescannerforemployees.database.CategoryDAO
import org.foxarmy.barcodescannerforemployees.database.DBStorageController import org.foxarmy.barcodescannerforemployees.database.DBStorageController
import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct
import java.io.File import java.io.File
import java.io.FileOutputStream
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.StandardCopyOption import java.nio.file.StandardCopyOption
import kotlin.concurrent.thread import kotlin.concurrent.thread
@ -276,6 +281,28 @@ class AddAbstractProductActivity : AppCompatActivity() {
pictureFile = File(picturesPath, "$imageHash.png") pictureFile = File(picturesPath, "$imageHash.png")
Files.move(tempfile.toPath(), pictureFile.toPath(), StandardCopyOption.REPLACE_EXISTING) Files.move(tempfile.toPath(), pictureFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
tempfile.delete() 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) generateThumbnailForImage(this, imageHash)
imageView.setImageURI(getImageUri(this, pictureFile)) imageView.setImageURI(getImageUri(this, pictureFile))

View File

@ -51,7 +51,7 @@ class SettingsActivity : AppCompatActivity() {
binding.saveButton.setOnClickListener { binding.saveButton.setOnClickListener {
sharedPreferences.edit().putString("currentGroup", net.getGroupId(binding.currentGroupSpinner.selectedItem.toString())).apply() 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) setResult(Activity.RESULT_OK)
finish() finish()
} }
@ -63,8 +63,8 @@ class SettingsActivity : AppCompatActivity() {
} }
private fun setUpImageCompressionSeekBar() { private fun setUpImageCompressionSeekBar() {
val compressionFactor = sharedPreferences.getInt("imageCompression", 0) val compressionFactor = sharedPreferences.getInt("imageCompression", 1)
binding.imageCompressionFactorSeekBar.progress = compressionFactor binding.imageCompressionFactorSeekBar.progress = compressionFactor - 1
} }
private fun fillUpCurrentGroupSpinner() { private fun fillUpCurrentGroupSpinner() {

View File

@ -10,7 +10,7 @@
app:layout_constraintStart_toStartOf="parent" android:id="@+id/imageCompressionSetting" app:layout_constraintStart_toStartOf="parent" android:id="@+id/imageCompressionSetting"
android:layout_marginTop="16dp"> android:layout_marginTop="16dp">
<TextView <TextView
android:text="@string/image_scale_factor" android:text="@string/image_compress_factor"
android:layout_width="187dp" android:layout_width="187dp"
android:layout_height="30dp" android:id="@+id/imageCompressionFactorTextView" android:textSize="20sp" android:layout_height="30dp" android:id="@+id/imageCompressionFactorTextView" android:textSize="20sp"
android:textAlignment="center" android:textAlignment="center"
@ -19,7 +19,7 @@
style="@style/Widget.AppCompat.SeekBar.Discrete" style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="219dp" android:layout_width="219dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:max="16" android:max="15"
android:progress="0" android:progress="0"
android:id="@+id/imageCompressionFactorSeekBar" android:id="@+id/imageCompressionFactorSeekBar"
android:tickMark="@drawable/seekbar_tick" android:tickMark="@drawable/seekbar_tick"

View File

@ -111,7 +111,7 @@
<string name="leave_group">Leave group</string> <string name="leave_group">Leave group</string>
<string name="rename_group">Rename group</string> <string name="rename_group">Rename group</string>
<string name="join_or_create_group">Join or create a group</string> <string name="join_or_create_group">Join or create a group</string>
<string name="image_scale_factor">Image scale factor\n</string> <string name="image_compress_factor">Image compression factor</string>
<string name="current_group">Current group</string> <string name="current_group">Current group</string>
<string name="cancel">Cancel</string> <string name="cancel">Cancel</string>
<string-array name="languages"> <string-array name="languages">

View File

@ -109,7 +109,7 @@
<string name="leave_group">Leave group</string> <string name="leave_group">Leave group</string>
<string name="rename_group">Rename group</string> <string name="rename_group">Rename group</string>
<string name="join_or_create_group">Join or create a group</string> <string name="join_or_create_group">Join or create a group</string>
<string name="image_scale_factor">Image scale factor\n</string> <string name="image_compress_factor">Image compression factor</string>
<string name="current_group">Current group</string> <string name="current_group">Current group</string>
<string name="cancel">Cancel</string> <string name="cancel">Cancel</string>
<string-array name="languages"> <string-array name="languages">