This commit is contained in:
leca 2024-09-29 01:50:56 +03:00
parent f4ddfd042e
commit 0a035d9399
8 changed files with 114 additions and 68 deletions

View File

@ -1,47 +1,58 @@
package org.foxarmy.barcodescannerforemployees package org.foxarmy.barcodescannerforemployees
import android.app.Activity
import android.content.Context import android.content.Context
import android.graphics.Bitmap import android.os.Bundle
import android.graphics.BitmapFactory
import android.util.Log import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import androidx.core.net.toFile
import org.foxarmy.barcodescannerforemployees.databinding.AbstractProductViewBinding
import org.foxarmy.barcodescannerforemployees.databinding.AddProductFragmentBinding
import java.io.File import java.io.File
/**
* TODO: document your custom view class.
*/
class AbstractProductView: LinearLayout { class AbstractProductView: LinearLayout {
private var _binding: AbstractProductViewBinding? = null
private val productImage = ImageView(context) private val productImage = ImageView(context)
private val productNameField = TextView(context) private val productNameField = TextView(context)
private val netWeightField = TextView(context) private val netWeightField = TextView(context)
private val categoryField = TextView(context) private val categoryField = TextView(context)
fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
constructor(context: Context, productImageFile: String, productName: String, netWeight: Double, category: Int) : super(context) { _binding = AbstractProductViewBinding.inflate(inflater, container, false)
this.orientation = LinearLayout.VERTICAL
val imageFile = File(File(context.filesDir, "pictures"), productImageFile)
val image: ByteArray = imageFile.readBytes()
if (image.isEmpty()) {
Toast.makeText(context, image.size.toString(), Toast.LENGTH_LONG).show()
} }
constructor(activity: Activity, context: Context, productImageFile: String, productName: String, netWeight: Double, category: Int) : super(context) {
this.orientation = VERTICAL
val picturesDir = File(context.filesDir, "pictures")
picturesDir.mkdirs()
val imageUri = getImageUri(activity, File(picturesDir, productImageFile))
if (imageUri != null) {
Log.d("QWERTYUIOP", "хуй: ${imageUri.path.toString()}")
try { try {
Log.d("org.foxarmy.barcodescannerforemployees", "[ХУЙ]" + imageFile.absolutePath) Log.d("QWERTYUIOP", "Does exist: ${imageUri.toFile().absolutePath}")
val bitmap: Bitmap = BitmapFactory.decodeFile(imageFile.absolutePath)
} catch (e:Exception) { } catch (e:Exception) {
Log.d("org.foxarmy.barcodescannerforemployees", "[ХУЙ] " + e) Log.d("QWERTYUIOP", e.message.toString())
} }
// productImage.setImageBitmap(bitmap) }
productNameField.setText(productName) productImage.setImageURI(imageUri)
netWeightField.setText(netWeight.toString()) productNameField.text = productName
netWeightField.text = netWeight.toString()
addView(productNameField) addView(productNameField)
addView(netWeightField) addView(netWeightField)
// if(findViewById(R.id.productLayout) == null) { // if(findViewById(R.id.productLayout) == null) {
// Toast.makeText(context, "AAAAAAAAAAA", Toast.LENGTH_LONG).show() // Toast.makeText(context, "AAAAAAAAAAA", Toast.LENGTH_LONG).show()
// } // }

View File

@ -1,8 +1,6 @@
package org.foxarmy.barcodescannerforemployees package org.foxarmy.barcodescannerforemployees
//import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions
import android.content.ContentValues 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
@ -12,9 +10,7 @@ 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.content.FileProvider
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import com.google.firebase.components.BuildConfig
import com.google.mlkit.vision.barcode.common.Barcode import com.google.mlkit.vision.barcode.common.Barcode
import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions
import com.google.mlkit.vision.codescanner.GmsBarcodeScanning import com.google.mlkit.vision.codescanner.GmsBarcodeScanning
@ -22,16 +18,8 @@ import org.foxarmy.barcodescannerforemployees.databinding.AddProductFragmentBind
import java.io.File import java.io.File
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.StandardCopyOption import java.nio.file.StandardCopyOption
import java.security.MessageDigest
@OptIn(ExperimentalStdlibApi::class)
fun String.md5(): String {
val md = MessageDigest.getInstance("MD5")
val digest = md.digest(this.toByteArray())
return digest.toHexString()
}
class AddProductFragment : Fragment() { class AddProductFragment : Fragment() {
private var _binding: AddProductFragmentBinding? = null private var _binding: AddProductFragmentBinding? = null
@ -51,10 +39,6 @@ class AddProductFragment : Fragment() {
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) @RequiresApi(Build.VERSION_CODES.O)
val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) { success: Boolean -> val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) { success: Boolean ->
if (success) { if (success) {
@ -64,7 +48,7 @@ class AddProductFragment : Fragment() {
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)
tempfile.delete() tempfile.delete()
binding.imageView.setImageURI(getImageUri(pictureFile)) binding.imageView.setImageURI(getImageUri(requireActivity(), pictureFile))
Log.i("QWERTYUIOP", "Picture saved") Log.i("QWERTYUIOP", "Picture saved")
} else { } else {
Log.e("QWERTYUIOP", "Cannot save a picture") Log.e("QWERTYUIOP", "Cannot save a picture")
@ -75,7 +59,10 @@ class AddProductFragment : Fragment() {
fun getPicture () { fun getPicture () {
//Saving picture to a temp file for further hash calculation and moving to a proper directory //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 = getImageUri(imageFile)//FileProvider.getUriForFile(requireActivity(), BuildConfig.APPLICATION_ID + "." + requireActivity().localClassName + ".provider", imageFile) val imageUri = getImageUri(requireActivity(), imageFile)
if (imageUri != null) {
Log.d("QWERTYUIOP", imageUri.path.toString())
}
takePicture.launch(imageUri) takePicture.launch(imageUri)
} }
@ -113,7 +100,7 @@ class AddProductFragment : Fragment() {
} }
val db = DBStorageController(requireContext()).writableDatabase val db = DBStorageController(requireContext()).writableDatabase
Log.d("QWERTYUIOP", "Putting ${pictureFile.name}")
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())

View File

@ -18,7 +18,6 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val dbHelper = DBStorageController(applicationContext)
binding = ActivityMainBinding.inflate(layoutInflater) binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root) setContentView(binding.root)

View File

@ -34,7 +34,7 @@ class StorageFragment : Fragment() {
val projection = arrayOf(BaseColumns._ID, ProductContract.ProductEntry.PRODUCT_NAME, ProductContract.ProductEntry.PRODUCT_NET_WEIGHT, ProductContract.ProductEntry.IMAGE_FILENAME) val projection = arrayOf(BaseColumns._ID, ProductContract.ProductEntry.PRODUCT_NAME, ProductContract.ProductEntry.PRODUCT_NET_WEIGHT, ProductContract.ProductEntry.IMAGE_FILENAME)
val selection = "${ProductContract.ProductEntry.PRODUCT_NAME} = ?" val selection = "${ProductContract.ProductEntry.PRODUCT_NAME} = ?"
val selectionArgs = arrayOf("test") val selectionArgs = arrayOf("обогреватель")
val cursor = db.query(ProductContract.ProductEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null) val cursor = db.query(ProductContract.ProductEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null)
@ -46,6 +46,7 @@ class StorageFragment : Fragment() {
getView()?.findViewById<ConstraintLayout>(R.id.storageLayout)?.addView( getView()?.findViewById<ConstraintLayout>(R.id.storageLayout)?.addView(
AbstractProductView( AbstractProductView(
requireActivity(),
requireContext(), requireContext(),
productImageFile = pictureFilename, productImageFile = pictureFilename,
productName = productName, productName = productName,

View File

@ -0,0 +1,19 @@
package org.foxarmy.barcodescannerforemployees
import android.app.Activity
import android.net.Uri
import androidx.core.content.FileProvider
import com.google.firebase.components.BuildConfig
import java.io.File
import java.security.MessageDigest
fun getImageUri(activity: Activity, imageFile: File): Uri? {
return FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + "." + activity.localClassName + ".provider", imageFile)
}
@OptIn(ExperimentalStdlibApi::class)
fun String.md5(): String {
val md = MessageDigest.getInstance("MD5")
val digest = md.digest(this.toByteArray())
return digest.toHexString()
}

View File

@ -0,0 +1,52 @@
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="300dp"
android:layout_height="300dp">
<org.foxarmy.barcodescannerforemployees.AbstractProductView
style="@style/Widget.Theme.BarcodeScannerForEmployees.MyView"
android:layout_width="300dp"
android:layout_height="300dp"
android:paddingLeft="20dp"
android:paddingBottom="40dp"
app:exampleDimension="24sp"
app:exampleString="Hello, AbstractProductView"
app:exampleDrawable="@android:drawable/ic_menu_add">
</org.foxarmy.barcodescannerforemployees.AbstractProductView>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="300dp"
android:layout_height="300dp" android:id="@+id/productLayout">
<ImageView
android:layout_width="match_parent"
android:layout_height="179dp" app:srcCompat="@android:drawable/ic_btn_speak_now"
android:id="@+id/productPicture"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<TextView
android:text="@string/sample_product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/productNameView"
app:layout_constraintTop_toBottomOf="@+id/productPicture"
android:layout_marginTop="15dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="25dp" android:fontFamily="monospace" android:textSize="20sp"
android:maxLines="2"/>
<TextView
android:text="@string/sample_product_net_weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/productNetWeightView" android:layout_weight="1"
app:layout_constraintTop_toBottomOf="@+id/productNameView"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="25dp"
android:layout_marginTop="10dp" android:textSize="12sp" android:fontFamily="monospace"
app:layout_constraintEnd_toStartOf="@+id/unitView" android:layout_marginEnd="2dp"
/>
<TextView
android:text="@string/sample_unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/unitView"
app:layout_constraintStart_toEndOf="@+id/productNetWeightView"
app:layout_constraintTop_toBottomOf="@+id/productNameView" android:layout_marginTop="10dp"
android:fontFamily="monospace" android:textSize="12sp" android:layout_marginStart="8dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

View File

@ -1,26 +0,0 @@
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="300dp"
android:layout_height="300dp">
<org.foxarmy.barcodescannerforemployees.AbstractProductView
style="@style/Widget.Theme.BarcodeScannerForEmployees.MyView"
android:layout_width="300dp"
android:layout_height="300dp"
android:paddingLeft="20dp"
android:paddingBottom="40dp"
app:exampleDimension="24sp"
app:exampleString="Hello, AbstractProductView"
app:exampleDrawable="@android:drawable/ic_menu_add"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="300dp"
android:layout_height="300dp" android:id="@+id/productLayout">
<LinearLayout
android:orientation="vertical"
android:layout_width="298dp"
android:layout_height="298dp" tools:layout_editor_absoluteY="1dp" tools:layout_editor_absoluteX="1dp">
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

View File

@ -13,4 +13,7 @@
<string name="productName">Product name</string> <string name="productName">Product name</string>
<string name="productType">Product type</string> <string name="productType">Product type</string>
<string name="imageOfAProduct">Image of a product</string> <string name="imageOfAProduct">Image of a product</string>
<string name="sample_product_name">Уззкое название товара</string>
<string name="sample_product_net_weight">1998</string>
<string name="sample_unit">g</string>
</resources> </resources>