From ac5be50f2b678e987b95fce4c9461c4b2048876b Mon Sep 17 00:00:00 2001 From: leca Date: Wed, 2 Oct 2024 01:55:46 +0300 Subject: [PATCH] image thumbnails generation --- .../AddProductFragment.kt | 11 +-------- .../StorageFragment.kt | 1 + .../barcodescannerforemployees/Utils.kt | 23 +++++++++++++++++++ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt index c962c08..1d4f76b 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt @@ -1,8 +1,6 @@ package org.foxarmy.barcodescannerforemployees import android.content.ContentValues -import android.graphics.Bitmap -import android.graphics.BitmapFactory import android.os.Build import android.os.Bundle import android.util.Log @@ -12,7 +10,6 @@ import android.view.ViewGroup import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import androidx.annotation.RequiresApi -import androidx.core.graphics.scale import androidx.fragment.app.Fragment import androidx.navigation.fragment.findNavController import com.google.mlkit.vision.barcode.common.Barcode @@ -20,7 +17,6 @@ import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions import com.google.mlkit.vision.codescanner.GmsBarcodeScanning import org.foxarmy.barcodescannerforemployees.databinding.AddProductFragmentBinding import java.io.File -import java.io.FileOutputStream import java.nio.file.Files import java.nio.file.StandardCopyOption @@ -51,12 +47,7 @@ class AddProductFragment : Fragment() { val tempfile = File(requireContext().filesDir, "image.png") val imageContent = tempfile.inputStream().readBytes() val imageHash = imageContent.toString(Charsets.UTF_8).md5() - val thumbnailsDir = File(requireContext().cacheDir, "thumbnails") - thumbnailsDir.mkdirs() - val thumbnailFile = File(thumbnailsDir, "$imageHash.webp") - var img = BitmapFactory.decodeByteArray(imageContent, 0, imageContent.size) - img = img.scale(img.width/4,img.height/4) - img.compress(Bitmap.CompressFormat.WEBP_LOSSY, 25, FileOutputStream(thumbnailFile)) + generateThumbnailForImage(context!!, imageHash) pictureFile = File(picturesPath, "$imageHash.png") Files.move(tempfile.toPath(), pictureFile.toPath(), StandardCopyOption.REPLACE_EXISTING) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/StorageFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/StorageFragment.kt index fcb5c13..bfade28 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/StorageFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/StorageFragment.kt @@ -42,6 +42,7 @@ class StorageFragment : Fragment() { val netWeight = getDouble(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NET_WEIGHT)) val productImageHash = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.IMAGE_FILENAME)) + generateThumbnailForImage(context!!, productImageHash) val abstractProduct = AbstractProductView( requireActivity(), diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt index fdbaa01..28bc288 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/Utils.kt @@ -1,16 +1,39 @@ package org.foxarmy.barcodescannerforemployees import android.app.Activity +import android.content.Context +import android.graphics.Bitmap +import android.graphics.BitmapFactory import android.net.Uri +import android.os.Build +import androidx.annotation.RequiresApi import androidx.core.content.FileProvider +import androidx.core.graphics.scale import com.google.firebase.components.BuildConfig import java.io.File +import java.io.FileOutputStream import java.security.MessageDigest fun getImageUri(activity: Activity, imageFile: File): Uri? { return FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + "." + activity.localClassName + ".provider", imageFile) } +@RequiresApi(Build.VERSION_CODES.R) +fun generateThumbnailForImage(context: Context, imageHash: String) { + val thumbnailsDir = File(context.cacheDir, "thumbnails") + thumbnailsDir.mkdirs() + val picturesDir = File(context.filesDir, "pictures") + picturesDir.mkdirs() + val thumbnailFile = File(thumbnailsDir, "$imageHash.webp") + if (thumbnailFile.exists()) return + + val imageFile = File(picturesDir, "$imageHash.png") + val imageContent = imageFile.inputStream().readBytes() + var img = BitmapFactory.decodeByteArray(imageContent, 0, imageContent.size) + img = img.scale(img.width/4,img.height/4) + img.compress(Bitmap.CompressFormat.WEBP_LOSSY, 25, FileOutputStream(thumbnailFile)) +} + @OptIn(ExperimentalStdlibApi::class) fun String.md5(): String { val md = MessageDigest.getInstance("MD5")