image thumbnails generation

This commit is contained in:
leca 2024-10-02 01:55:46 +03:00
parent 229941e6de
commit ac5be50f2b
3 changed files with 25 additions and 10 deletions

View File

@ -1,8 +1,6 @@
package org.foxarmy.barcodescannerforemployees package org.foxarmy.barcodescannerforemployees
import android.content.ContentValues import android.content.ContentValues
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
@ -12,7 +10,6 @@ import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.graphics.scale
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import com.google.mlkit.vision.barcode.common.Barcode 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 com.google.mlkit.vision.codescanner.GmsBarcodeScanning
import org.foxarmy.barcodescannerforemployees.databinding.AddProductFragmentBinding import org.foxarmy.barcodescannerforemployees.databinding.AddProductFragmentBinding
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
@ -51,12 +47,7 @@ class AddProductFragment : Fragment() {
val tempfile = File(requireContext().filesDir, "image.png") val tempfile = File(requireContext().filesDir, "image.png")
val imageContent = tempfile.inputStream().readBytes() val imageContent = tempfile.inputStream().readBytes()
val imageHash = imageContent.toString(Charsets.UTF_8).md5() val imageHash = imageContent.toString(Charsets.UTF_8).md5()
val thumbnailsDir = File(requireContext().cacheDir, "thumbnails") generateThumbnailForImage(context!!, imageHash)
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))
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)

View File

@ -42,6 +42,7 @@ class StorageFragment : Fragment() {
val netWeight = getDouble(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NET_WEIGHT)) val netWeight = getDouble(getColumnIndexOrThrow(ProductContract.ProductEntry.PRODUCT_NET_WEIGHT))
val productImageHash = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.IMAGE_FILENAME)) val productImageHash = getString(getColumnIndexOrThrow(ProductContract.ProductEntry.IMAGE_FILENAME))
generateThumbnailForImage(context!!, productImageHash)
val abstractProduct = AbstractProductView( val abstractProduct = AbstractProductView(
requireActivity(), requireActivity(),

View File

@ -1,16 +1,39 @@
package org.foxarmy.barcodescannerforemployees package org.foxarmy.barcodescannerforemployees
import android.app.Activity import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri import android.net.Uri
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.core.graphics.scale
import com.google.firebase.components.BuildConfig import com.google.firebase.components.BuildConfig
import java.io.File import java.io.File
import java.io.FileOutputStream
import java.security.MessageDigest import java.security.MessageDigest
fun getImageUri(activity: Activity, imageFile: File): Uri? { fun getImageUri(activity: Activity, imageFile: File): Uri? {
return FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + "." + activity.localClassName + ".provider", imageFile) 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) @OptIn(ExperimentalStdlibApi::class)
fun String.md5(): String { fun String.md5(): String {
val md = MessageDigest.getInstance("MD5") val md = MessageDigest.getInstance("MD5")