diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt index b91c33e..1bb7962 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/AddProductFragment.kt @@ -1,6 +1,8 @@ package org.foxarmy.barcodescannerforemployees //import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions +import android.content.ContentValues +import android.net.Uri import android.os.Build import android.os.Bundle import android.util.Log @@ -13,8 +15,13 @@ import androidx.annotation.RequiresApi import androidx.core.content.FileProvider import androidx.fragment.app.Fragment 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 java.io.File +import java.nio.file.Files +import java.nio.file.StandardCopyOption import java.security.MessageDigest @@ -28,37 +35,58 @@ fun String.md5(): String { class AddProductFragment : Fragment() { private var _binding: AddProductFragmentBinding? = null + private lateinit var pictureFile: File private val binding get() = _binding!! + private lateinit var picturesPath: File override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { + + picturesPath = File(requireContext().filesDir, "pictures") + picturesPath.mkdirs() + _binding = AddProductFragmentBinding.inflate(inflater, container, false) 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 -> 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 { - Log.e("QWERTYUIOP", "Xuinya") + Log.e("QWERTYUIOP", "Cannot save a picture") } } + @RequiresApi(Build.VERSION_CODES.O) 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 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) } + @RequiresApi(Build.VERSION_CODES.O) val requestPermissionLauncher = registerForActivityResult( ActivityResultContracts.RequestPermission() ) { isGranted: Boolean -> if (isGranted) { Toast.makeText(requireContext(), "Granted", Toast.LENGTH_LONG).show() -// getFuckingPicture() + getPicture() } else { 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?) { 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 { val productName = binding.productName.text.toString() 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() return@setOnClickListener } + if (productName == "") { Toast.makeText(context, "Please, write a name of a product!", Toast.LENGTH_SHORT).show() 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() } - 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 values = ContentValues().apply { put(ProductContract.ProductEntry.PRODUCT_NAME, productName) 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) @@ -168,7 +157,7 @@ class AddProductFragment : Fragment() { ).show() } } - */ + } override fun onDestroyView() {