removed google play's barcodescanner dependency

This commit is contained in:
leca 2024-10-19 17:54:22 +03:00
parent 385a19a6ee
commit 05f95ef30d
8 changed files with 152 additions and 78 deletions

View File

@ -32,7 +32,10 @@ android {
}
buildFeatures {
viewBinding = true
buildConfig = true
}
}
dependencies {
@ -50,14 +53,14 @@ dependencies {
testImplementation(libs.junit)
implementation(libs.volley)
androidTestImplementation(libs.androidx.junit)
implementation (libs.play.services.code.scanner)
implementation(libs.zxing.android.embedded)
implementation("com.google.zxing:core:3.4.1")
androidTestImplementation(libs.androidx.espresso.core)
// implementation("com.google.android.material:1.2.0")
// Barcode scanning API
implementation (libs.barcode.scanning)
// CameraX library
// CameraX library
implementation (libs.androidx.camera.camera2)
implementation (libs.androidx.camera.lifecycle)

View File

@ -43,6 +43,10 @@
android:label="@string/title_activity_fullscreen"
android:theme="@style/Theme.BarcodeScannerForEmployees.Fullscreen"/>
<activity android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="fullSensor"
tools:replace="android:screenOrientation"/>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.google.firebase.components.activities.MainActivity.provider;com.google.firebase.components.activities.FullscreenActivity.provider;com.google.firebase.components.activities.AddAbstractProductActivity.provider;com.google.firebase.components.activities.AddProductActivity.provider"
@ -53,11 +57,6 @@
android:resource="@xml/file_path"/>
</provider>
<meta-data
android:name="com.google.mlkit.vision.DEPENDENCIES"
android:value="barcode_ui">
</meta-data>
<activity
android:name=".activities.MainActivity"
android:exported="true"

View File

@ -28,11 +28,11 @@ class Parser constructor() {
val (name, netWeight, unit) = findNetWeightInText(
text,
listOf(
Regex("[0-9]+,?[0-9]*\\s*[лЛ]"),
Regex("[0-9]+,?[0-9]*\\s*((мл)|(МЛ)|(Мл))"),
Regex("[0-9]+,?[0-9]*\\s*((кг)|(Кг))"),
Regex("[0-9]+,?[0-9]*\\s*[гГ]"),
Regex("[0-9]+,?[0-9*]\\s*((шт)|(Шт))")
Regex("\\d*[.,]?\\d+\\s*[лЛ]"),
Regex("[0-9]+((,?)|(.?))+[0-9]*\\s*((мл)|(МЛ)|(Мл))"),
Regex("[0-9]+((,?)|(.?))+[0-9]*\\s*((кг)|(Кг))"),
Regex("[0-9]+((,?)|(.?))+[0-9]*\\s*[гГ]"),
Regex("\\d+\\s*(шт)|(Шт)")
)
)
var unitNumber = -1

View File

@ -1,6 +1,8 @@
package org.foxarmy.barcodescannerforemployees.activities
import android.Manifest
//import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions
//import com.google.mlkit.vision.codescanner.GmsBarcodeScanning
import android.content.ContentValues
import android.content.DialogInterface
import android.content.Intent
@ -15,9 +17,9 @@ import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.widget.addTextChangedListener
import com.google.mlkit.vision.barcode.common.Barcode
import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions
import com.google.mlkit.vision.codescanner.GmsBarcodeScanning
import com.journeyapps.barcodescanner.ScanContract
import com.journeyapps.barcodescanner.ScanIntentResult
import com.journeyapps.barcodescanner.ScanOptions
import org.foxarmy.barcodescannerforemployees.*
import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct
import java.io.File
@ -44,6 +46,8 @@ class AddAbstractProductActivity : AppCompatActivity() {
private lateinit var picturesPath: File
private var barcode: String = ""
private var scanningBarcode = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -55,8 +59,6 @@ class AddAbstractProductActivity : AppCompatActivity() {
barcode = abstractProduct!!.barcode
}
picturesPath = File(filesDir, "pictures")
val thumbnailsDir = File(cacheDir, "thumbnails")
thumbnailsDir.mkdirs()
@ -131,36 +133,24 @@ class AddAbstractProductActivity : AppCompatActivity() {
if (abstractProduct == null) {
db.insert(AbstractProductContract.AbstractProductEntry.TABLE_NAME, null, values)
} else {
db.update(AbstractProductContract.AbstractProductEntry.TABLE_NAME, values, "${BaseColumns._ID} = ?", arrayOf(abstractProduct!!.id.toString()))
db.update(
AbstractProductContract.AbstractProductEntry.TABLE_NAME,
values,
"${BaseColumns._ID} = ?",
arrayOf(abstractProduct!!.id.toString())
)
}
finish()
}
takePictureButton.setOnClickListener {
requestPermissionLauncher.launch(Manifest.permission.CAMERA)
requestPermissionLauncher.launch(android.Manifest.permission.CAMERA)
}
scanButton.setOnClickListener {
val options = GmsBarcodeScannerOptions.Builder()
.setBarcodeFormats(
Barcode.FORMAT_EAN_13
)
.build()
val scanner = GmsBarcodeScanning.getClient(this)
scanner.startScan()
.addOnSuccessListener { barcode ->
this.barcode = barcode.rawValue.toString()
performRequest(this.barcode)
}
.addOnFailureListener { e ->
Toast.makeText(
this,
"Failed to scan barcode. Please, try again or enter data manually",
Toast.LENGTH_LONG
).show()
}
scanningBarcode = true
requestPermissionLauncher.launch(android.Manifest.permission.CAMERA)
}
}
@ -170,7 +160,11 @@ class AddAbstractProductActivity : AppCompatActivity() {
requester.request(this, barcode)
var abstractProduct: AbstractProduct
if (DBStorageController(this).findAbstractProductByBarcode(DBStorageController(this).readableDatabase, this.barcode) != null) {
if (DBStorageController(this).findAbstractProductByBarcode(
DBStorageController(this).readableDatabase,
this.barcode
) != null
) {
AlertDialog.Builder(this)
.setMessage("You've got an abstract product with such barcode in your database")
.setPositiveButton("Quit") { _: DialogInterface, _: Int ->
@ -179,7 +173,10 @@ class AddAbstractProductActivity : AppCompatActivity() {
.setNegativeButton("Edit existing") { _: DialogInterface, _: Int ->
val addProductIntent = Intent(this, AddAbstractProductActivity::class.java)
val extras = Bundle()
val existingAbstractProduct = DBStorageController(this).findAbstractProductByBarcode(DBStorageController(this).readableDatabase, this.barcode)
val existingAbstractProduct = DBStorageController(this).findAbstractProductByBarcode(
DBStorageController(this).readableDatabase,
this.barcode
)
extras.putParcelable("abstractProduct", existingAbstractProduct)
addProductIntent.putExtras(extras)
ContextCompat.startActivity(this, addProductIntent, extras)
@ -189,10 +186,12 @@ class AddAbstractProductActivity : AppCompatActivity() {
thread {
// Я сам в ахуях какой это костыль, пока хз как фиксить, потом придумаю :))
while (requester.response == "") { }
while (requester.response == "") {
}
if (requester.response == "Not found 404") {
runOnUiThread {
Toast.makeText(this, "Product not found. Please, try again or type manually", Toast.LENGTH_LONG).show()
Toast.makeText(this, "Product not found. Please, try again or type manually", Toast.LENGTH_LONG)
.show()
}
return@thread
}
@ -217,7 +216,8 @@ class AddAbstractProductActivity : AppCompatActivity() {
)
val arrayAdapter = ArrayAdapter<String>(this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, units)
val arrayAdapter =
ArrayAdapter<String>(this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, units)
arrayAdapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item)
unitTypeSpinner.adapter = arrayAdapter
}
@ -231,15 +231,24 @@ class AddAbstractProductActivity : AppCompatActivity() {
CategoriesContract.CategoryEntry.CATEGORY_NAME
)
val cursor = db.query(CategoriesContract.CategoryEntry.TABLE_NAME, projection, null, null, null, null, BaseColumns._ID+" ASC")
val cursor = db.query(
CategoriesContract.CategoryEntry.TABLE_NAME,
projection,
null,
null,
null,
null,
BaseColumns._ID + " ASC"
)
with (cursor) {
with(cursor) {
while (moveToNext()) {
categories.add(getString(getColumnIndexOrThrow(CategoriesContract.CategoryEntry.CATEGORY_NAME)))
}
}
val arrayAdapter = ArrayAdapter<String>(this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, categories)
val arrayAdapter =
ArrayAdapter<String>(this, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item, categories)
arrayAdapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item)
categorySpinner.adapter = arrayAdapter
}
@ -264,7 +273,7 @@ class AddAbstractProductActivity : AppCompatActivity() {
}
@RequiresApi(Build.VERSION_CODES.R)
fun getPicture () {
fun getPicture() {
//Saving picture to a temp file for further hash calculation and moving to a proper directory
val imageFile = File(this.filesDir, "image.png")
val imageUri = getImageUri(this, imageFile)
@ -279,9 +288,38 @@ class AddAbstractProductActivity : AppCompatActivity() {
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (isGranted) {
getPicture()
if (scanningBarcode) {
prepareBarcodeScanner()
} else {
getPicture()
}
} else {
Toast.makeText(this, "I need permission in order to take a picture", Toast.LENGTH_LONG).show()
}
}
private val scanLauncher = registerForActivityResult(ScanContract()) { result: ScanIntentResult ->
run {
if (result.contents == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show()
} else {
scanningBarcode = false
val scannedBarcode = result.contents
barcodeText.setText(scannedBarcode)
performRequest(scannedBarcode)
}
}
}
private fun prepareBarcodeScanner() {
val options = ScanOptions()
options.setDesiredBarcodeFormats(ScanOptions.EAN_13)
options.setPrompt("Scan barcode of a product")
options.setCameraId(0)
options.setBeepEnabled(false)
options.setBarcodeImageEnabled(true)
options.setOrientationLocked(false)
scanLauncher.launch(options)
}
}

View File

@ -1,12 +1,17 @@
package org.foxarmy.barcodescannerforemployees.activities
import android.app.DatePickerDialog
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.addTextChangedListener
import com.google.mlkit.vision.codescanner.GmsBarcodeScanning
import com.journeyapps.barcodescanner.ScanContract
import com.journeyapps.barcodescanner.ScanIntentResult
import com.journeyapps.barcodescanner.ScanOptions
import org.foxarmy.barcodescannerforemployees.DBStorageController
import org.foxarmy.barcodescannerforemployees.R
import org.foxarmy.barcodescannerforemployees.dataclasses.AbstractProduct
@ -77,17 +82,7 @@ class AddProductActivity : AppCompatActivity() {
}
scanButton.setOnClickListener {
val scanner = GmsBarcodeScanning.getClient(this)
scanner.startScan()
.addOnSuccessListener { bc ->
abstractProduct = DBStorageController(this).findAbstractProductByBarcode(DBStorageController(this).readableDatabase, bc.rawValue.toString())
product!!.abstractProductId = abstractProduct!!.id
displayAbstractProduct(abstractProduct!!)
}
.addOnFailureListener {
Toast.makeText(this, "Cannot scan barcode", Toast.LENGTH_SHORT).show()
}
requestPermissionLauncher.launch(android.Manifest.permission.CAMERA)
}
expiryDateRadioButton.setOnClickListener {
@ -215,4 +210,41 @@ class AddProductActivity : AppCompatActivity() {
abstractProductView.abstractProduct = abstractProduct
abstractProductView.update()
}
}
@RequiresApi(Build.VERSION_CODES.R)
val requestPermissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (isGranted) {
prepareBarcodeScanner()
} else {
Toast.makeText(this, "I need permission in order to take a picture", Toast.LENGTH_LONG).show()
}
}
private val scanLauncher = registerForActivityResult(ScanContract()) { result: ScanIntentResult ->
run {
if (result.contents == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show()
} else {
val scannedBarcode = result.contents
abstractProduct = DBStorageController(this).findAbstractProductByBarcode(DBStorageController(this).readableDatabase, scannedBarcode)
displayAbstractProduct(abstractProduct!!)
product?.abstractProductId = abstractProduct!!.id
}
}
}
private fun prepareBarcodeScanner() {
val options = ScanOptions()
options.setDesiredBarcodeFormats(ScanOptions.EAN_13)
options.setPrompt("Scan barcode of a product")
options.setCameraId(0)
options.setBeepEnabled(false)
options.setBarcodeImageEnabled(true)
options.setOrientationLocked(false)
scanLauncher.launch(options)
}
}

View File

@ -121,7 +121,6 @@ class StorageFragment : Fragment() {
)
var orderBy: String = ""
Log.d("QWERTYUIOP", binding.spinner.selectedItem.toString())
when(binding.spinner.selectedItem) {
"Name" -> {
orderBy = "${AbstractProductContract.AbstractProductEntry.PRODUCT_NAME} ASC"

View File

@ -9,7 +9,7 @@
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp">
android:padding="16dp" android:layout_gravity="center">
<Button
android:id="@+id/scan_button"
android:layout_width="100dp"
@ -17,23 +17,23 @@
android:text="@string/scan_label"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/categoryTextView"
android:layout_marginTop="15dp"/>
android:layout_marginTop="15dp" android:layout_marginStart="5dp"/>
<ImageView
android:src="@android:drawable/ic_menu_camera"
android:layout_width="356dp"
android:layout_width="0dp"
android:layout_height="303dp" android:id="@+id/imageView"
android:layout_marginBottom="25dp"
app:layout_constraintBottom_toTopOf="@+id/productName" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="15dp"/>
android:layout_marginTop="15dp" android:layout_marginStart="5dp" android:layout_marginEnd="5dp"/>
<EditText
android:layout_width="350dp"
android:layout_width="0dp"
android:layout_height="50dp"
android:inputType="text"
android:ems="10"
android:id="@+id/productName"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp" android:layout_marginEnd="8dp"
android:layout_marginStart="5dp" android:layout_marginEnd="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/netWeight"
app:layout_constraintHorizontal_bias="0.5"
@ -46,7 +46,7 @@
android:ems="10"
android:id="@+id/netWeight"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginStart="5dp"
android:visibility="visible" android:hint="@string/netWeight" android:textColorHint="#737373"
app:layout_constraintTop_toBottomOf="@+id/productName"
/>
@ -56,13 +56,14 @@
app:layout_constraintStart_toEndOf="@+id/netWeight"
app:layout_constraintTop_toBottomOf="@+id/productName" app:layout_constraintEnd_toEndOf="parent"/>
<EditText
android:layout_width="350dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="@+id/barcodeTextEdit" app:layout_constraintTop_toBottomOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="8dp" android:hint="Barcode" android:textColorHint="#737373"/>
android:layout_marginTop="8dp" android:hint="Barcode" android:textColorHint="#737373"
android:layout_marginStart="5dp" android:layout_marginEnd="5dp"/>
<TextView
android:text="@string/category"
android:layout_width="wrap_content"
@ -81,15 +82,15 @@
android:layout_width="100dp"
android:layout_height="50dp" android:id="@+id/saveButton"
app:layout_constraintTop_toBottomOf="@+id/categoryTextView"
android:layout_marginTop="15dp" app:layout_constraintEnd_toEndOf="parent"/>
android:layout_marginTop="15dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="5dp"/>
<Button
android:text="@string/takePicture"
android:layout_width="100dp"
android:layout_height="55dp" android:id="@+id/takePictureButton"
app:layout_constraintTop_toBottomOf="@+id/categoryTextView"
android:layout_marginTop="15dp" app:layout_constraintStart_toEndOf="@+id/scan_button"
android:layout_marginStart="33dp" app:layout_constraintEnd_toStartOf="@+id/saveButton"
android:layout_marginEnd="6dp" app:layout_constraintHorizontal_bias="0.0"/>
android:layout_marginTop="15dp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

View File

@ -19,6 +19,7 @@ legacySupportV4 = "1.0.0"
fragment = "1.8.4"
playServicesCodeScanner = "16.1.0"
volley = "1.2.1"
zxingAndroidEmbedded = "4.3.0"
[libraries]
androidx-camera-camera2 = { module = "androidx.camera:camera-camera2", version.ref = "cameraView" }
@ -41,6 +42,7 @@ androidx-legacy-support-v4 = { group = "androidx.legacy", name = "legacy-support
androidx-fragment = { group = "androidx.fragment", name = "fragment", version.ref = "fragment" }
play-services-code-scanner = { module = "com.google.android.gms:play-services-code-scanner", version.ref = "playServicesCodeScanner" }
volley = { module = "com.android.volley:volley", version.ref = "volley" }
zxing-android-embedded = { module = "com.journeyapps:zxing-android-embedded", version.ref = "zxingAndroidEmbedded" }
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }