done addproductfragment

This commit is contained in:
leca 2024-09-27 16:49:11 +03:00
parent daed52e004
commit f4ddfd042e
1 changed files with 36 additions and 47 deletions

View File

@ -1,6 +1,8 @@
package org.foxarmy.barcodescannerforemployees package org.foxarmy.barcodescannerforemployees
//import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions //import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions
import android.content.ContentValues
import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
@ -13,8 +15,13 @@ import androidx.annotation.RequiresApi
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import com.google.firebase.components.BuildConfig import com.google.firebase.components.BuildConfig
import com.google.mlkit.vision.barcode.common.Barcode
import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions
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.nio.file.Files
import java.nio.file.StandardCopyOption
import java.security.MessageDigest import java.security.MessageDigest
@ -28,37 +35,58 @@ fun String.md5(): String {
class AddProductFragment : Fragment() { class AddProductFragment : Fragment() {
private var _binding: AddProductFragmentBinding? = null private var _binding: AddProductFragmentBinding? = null
private lateinit var pictureFile: File
private val binding get() = _binding!! private val binding get() = _binding!!
private lateinit var picturesPath: File
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View { ): View {
picturesPath = File(requireContext().filesDir, "pictures")
picturesPath.mkdirs()
_binding = AddProductFragmentBinding.inflate(inflater, container, false) _binding = AddProductFragmentBinding.inflate(inflater, container, false)
return binding.root return binding.root
} }
fun getImageUri(imageFile: File): Uri? {
return FileProvider.getUriForFile(requireActivity(), BuildConfig.APPLICATION_ID + "." + requireActivity().localClassName + ".provider", imageFile)
}
@RequiresApi(Build.VERSION_CODES.O)
val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) { success: Boolean -> val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) { success: Boolean ->
if (success) { if (success) {
Log.i("QWERTYUIOP", "Done") //Move picture to a proper directory according to its calculated hash
val tempfile = File(requireContext().filesDir, "image.png")
val imageHash = tempfile.inputStream().readBytes().toString(Charsets.UTF_8).md5()
pictureFile = File(picturesPath, "$imageHash.png")
Files.move(tempfile.toPath(), pictureFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
tempfile.delete()
binding.imageView.setImageURI(getImageUri(pictureFile))
Log.i("QWERTYUIOP", "Picture saved")
} else { } else {
Log.e("QWERTYUIOP", "Xuinya") Log.e("QWERTYUIOP", "Cannot save a picture")
} }
} }
@RequiresApi(Build.VERSION_CODES.O)
fun getPicture () { fun getPicture () {
//Saving picture to a temp file for further hash calculation and moving to a proper directory
val imageFile = File(requireContext().filesDir, "image.png") val imageFile = File(requireContext().filesDir, "image.png")
val imageUri = FileProvider.getUriForFile(requireActivity(), BuildConfig.APPLICATION_ID + "." + requireActivity().localClassName + ".provider", imageFile) val imageUri = getImageUri(imageFile)//FileProvider.getUriForFile(requireActivity(), BuildConfig.APPLICATION_ID + "." + requireActivity().localClassName + ".provider", imageFile)
takePicture.launch(imageUri) takePicture.launch(imageUri)
} }
@RequiresApi(Build.VERSION_CODES.O)
val requestPermissionLauncher = val requestPermissionLauncher =
registerForActivityResult( registerForActivityResult(
ActivityResultContracts.RequestPermission() ActivityResultContracts.RequestPermission()
) { isGranted: Boolean -> ) { isGranted: Boolean ->
if (isGranted) { if (isGranted) {
Toast.makeText(requireContext(), "Granted", Toast.LENGTH_LONG).show() Toast.makeText(requireContext(), "Granted", Toast.LENGTH_LONG).show()
// getFuckingPicture() getPicture()
} else { } else {
Toast.makeText(requireContext(), "I need permission in order to take a picture", Toast.LENGTH_LONG).show() Toast.makeText(requireContext(), "I need permission in order to take a picture", Toast.LENGTH_LONG).show()
} }
@ -68,39 +96,14 @@ class AddProductFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
binding.takePictureButton.setOnClickListener {
// requestPermissionLauncher.launch(android.Manifest.permission.CAMERA)
try {
getPicture()
} catch (e:Exception) {
Log.e("QWERTYUIOP", e.message.toString())
}
// try {
// when{
// ContextCompat.checkSelfPermission(requireContext(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED -> {
// getFuckingPicture()
// } else -> {
//// requestPermissionLauncher.launch(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
//// ActivityCompat.requestPermissions(requireActivity(), arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), 200)
//
// Log.e("QWERTYUIOP", "хуй соси губой тряси")
// }
// }
//
// } catch (e:Exception) {
// Log.e("QWERTYUIOP", e.message.toString())
// }
}
/*
binding.saveButton.setOnClickListener { binding.saveButton.setOnClickListener {
val productName = binding.productName.text.toString() val productName = binding.productName.text.toString()
val netWeight = binding.netWeight.text val netWeight = binding.netWeight.text
if (picture == null) { if (!this::pictureFile.isInitialized || !pictureFile.exists()) {
Toast.makeText(context, "Please, make a picture of a product!", Toast.LENGTH_SHORT).show() Toast.makeText(context, "Please, make a picture of a product!", Toast.LENGTH_SHORT).show()
return@setOnClickListener return@setOnClickListener
} }
if (productName == "") { if (productName == "") {
Toast.makeText(context, "Please, write a name of a product!", Toast.LENGTH_SHORT).show() Toast.makeText(context, "Please, write a name of a product!", Toast.LENGTH_SHORT).show()
return@setOnClickListener return@setOnClickListener
@ -109,26 +112,12 @@ class AddProductFragment : Fragment() {
Toast.makeText(context, "Please, write a valid net weight of a product!", Toast.LENGTH_SHORT).show() Toast.makeText(context, "Please, write a valid net weight of a product!", Toast.LENGTH_SHORT).show()
} }
val appFiles = context?.filesDir
val picturesDir = File(appFiles, "pictures")
picturesDir.mkdirs()
val imageFilename = picture.toString().md5() + ".png"
val imageFile = File(picturesDir, imageFilename)
val stream = FileOutputStream(imageFile)
picture!!.compress(Bitmap.CompressFormat.PNG, 100, stream)
stream.use {
val buffer = ByteBuffer.allocate(picture!!.byteCount)
picture!!.copyPixelsToBuffer(buffer)
it.write(buffer.array())
}
val db = DBStorageController(requireContext()).writableDatabase val db = DBStorageController(requireContext()).writableDatabase
val values = ContentValues().apply { val values = ContentValues().apply {
put(ProductContract.ProductEntry.PRODUCT_NAME, productName) put(ProductContract.ProductEntry.PRODUCT_NAME, productName)
put(ProductContract.ProductEntry.PRODUCT_NET_WEIGHT, netWeight.toString()) put(ProductContract.ProductEntry.PRODUCT_NET_WEIGHT, netWeight.toString())
put(ProductContract.ProductEntry.IMAGE_FILENAME, imageFilename) put(ProductContract.ProductEntry.IMAGE_FILENAME, pictureFile.name)
} }
db.insert(ProductContract.ProductEntry.TABLE_NAME, null, values) db.insert(ProductContract.ProductEntry.TABLE_NAME, null, values)
@ -168,7 +157,7 @@ class AddProductFragment : Fragment() {
).show() ).show()
} }
} }
*/
} }
override fun onDestroyView() { override fun onDestroyView() {