image thumbnails generation
This commit is contained in:
parent
229941e6de
commit
ac5be50f2b
|
@ -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)
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue