From 8181da421eb7b55641865c067894f4f653bc45ad Mon Sep 17 00:00:00 2001 From: soraefir Date: Sun, 24 Mar 2024 12:40:18 +0100 Subject: [PATCH] Deleted GoogleML usage --- app/build.gradle | 1 - .../fidelity/activity/fragment/Scanner.kt | 12 +-- .../fidelity/tools/BarcodeFormatConverter.kt | 21 +++-- .../helcel/fidelity/tools/BarcodeScanner.kt | 76 +++++++------------ app/src/main/res/layout/frag_scanner.xml | 1 + build.gradle | 1 + settings.gradle | 1 + 7 files changed, 49 insertions(+), 64 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 4640aa5..f2e9d77 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -77,6 +77,5 @@ dependencies { implementation 'com.google.code.gson:gson:2.10.1' implementation 'com.google.android.material:material:1.11.0' implementation 'com.google.zxing:core:3.5.3' - implementation 'com.google.mlkit:barcode-scanning:17.2.0' } \ No newline at end of file diff --git a/app/src/main/java/net/helcel/fidelity/activity/fragment/Scanner.kt b/app/src/main/java/net/helcel/fidelity/activity/fragment/Scanner.kt index 9b8fdfa..3ab5af4 100644 --- a/app/src/main/java/net/helcel/fidelity/activity/fragment/Scanner.kt +++ b/app/src/main/java/net/helcel/fidelity/activity/fragment/Scanner.kt @@ -45,6 +45,7 @@ class Scanner : Fragment() { return binding.root } + private fun startCreateEntry() { val createEntryFragment = CreateEntry() createEntryFragment.arguments = @@ -89,13 +90,14 @@ class Scanner : Fragment() { } val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA val analysisUseCase = getAnalysisUseCase { code, format -> - if (code != null && format != null) { + if (!code.isNullOrEmpty() && !format.isNullOrEmpty()) { this.code = code this.fmt = format - binding.btnScanDone.isEnabled = true - - } else { - binding.btnScanDone.isEnabled = false + } + val isDone = this.code.isNotEmpty() && this.fmt.isNotEmpty() + requireActivity().runOnUiThread { + binding.btnScanDone.isEnabled = isDone + binding.ScanActive.isEnabled = !isDone } } try { diff --git a/app/src/main/java/net/helcel/fidelity/tools/BarcodeFormatConverter.kt b/app/src/main/java/net/helcel/fidelity/tools/BarcodeFormatConverter.kt index 0d00e08..df30328 100644 --- a/app/src/main/java/net/helcel/fidelity/tools/BarcodeFormatConverter.kt +++ b/app/src/main/java/net/helcel/fidelity/tools/BarcodeFormatConverter.kt @@ -1,6 +1,5 @@ package net.helcel.fidelity.tools -import com.google.mlkit.vision.barcode.common.Barcode import com.google.zxing.BarcodeFormat object BarcodeFormatConverter { @@ -20,17 +19,17 @@ object BarcodeFormatConverter { } } - fun formatToString(f: Int): String { + fun formatToString(f: BarcodeFormat): String { return when (f) { - Barcode.FORMAT_CODE_128 -> "CODE_128" - Barcode.FORMAT_CODE_39 -> "CODE_39" - Barcode.FORMAT_CODE_93 -> "CODE_93" - Barcode.FORMAT_EAN_8 -> "EAN_8" - Barcode.FORMAT_EAN_13 -> "EAN_13" - Barcode.FORMAT_QR_CODE -> "CODE_QR" - Barcode.FORMAT_UPC_A -> "UPC_A" - Barcode.FORMAT_UPC_E -> "UPC_E" - Barcode.FORMAT_PDF417 -> "PDF_417" + BarcodeFormat.CODE_128 -> "CODE_128" + BarcodeFormat.CODE_39 -> "CODE_39" + BarcodeFormat.CODE_93 -> "CODE_93" + BarcodeFormat.EAN_8 -> "EAN_8" + BarcodeFormat.EAN_13 -> "EAN_13" + BarcodeFormat.QR_CODE -> "CODE_QR" + BarcodeFormat.UPC_A -> "UPC_A" + BarcodeFormat.UPC_E -> "UPC_E" + BarcodeFormat.PDF_417 -> "PDF_417" else -> throw Exception("Unsupported Format: $f") } } diff --git a/app/src/main/java/net/helcel/fidelity/tools/BarcodeScanner.kt b/app/src/main/java/net/helcel/fidelity/tools/BarcodeScanner.kt index bc24ef2..f356320 100644 --- a/app/src/main/java/net/helcel/fidelity/tools/BarcodeScanner.kt +++ b/app/src/main/java/net/helcel/fidelity/tools/BarcodeScanner.kt @@ -1,16 +1,16 @@ package net.helcel.fidelity.tools -import android.content.ContentValues -import android.util.Log +import android.graphics.Bitmap import androidx.annotation.OptIn import androidx.camera.core.ExperimentalGetImage import androidx.camera.core.ImageAnalysis import androidx.camera.core.ImageProxy -import com.google.mlkit.vision.barcode.BarcodeScanner -import com.google.mlkit.vision.barcode.BarcodeScannerOptions -import com.google.mlkit.vision.barcode.BarcodeScanning -import com.google.mlkit.vision.barcode.common.Barcode -import com.google.mlkit.vision.common.InputImage +import com.google.zxing.BinaryBitmap +import com.google.zxing.MultiFormatReader +import com.google.zxing.NotFoundException +import com.google.zxing.RGBLuminanceSource +import com.google.zxing.ReaderException +import com.google.zxing.common.HybridBinarizer import net.helcel.fidelity.tools.BarcodeFormatConverter.formatToString import java.util.concurrent.Executors @@ -19,57 +19,39 @@ object BarcodeScanner { @OptIn(ExperimentalGetImage::class) private fun processImageProxy( - barcodeScanner: BarcodeScanner, imageProxy: ImageProxy, cb: (String?, String?) -> Unit ) { - - imageProxy.image?.let { image -> - val inputImage = - InputImage.fromMediaImage( - image, - imageProxy.imageInfo.rotationDegrees - ) - - barcodeScanner.process(inputImage) - .addOnSuccessListener { barcodeList -> - val barcode = - barcodeList.getOrNull(0) - if (barcode != null) - cb(barcode.displayValue, formatToString(barcode.format)) - } - .addOnFailureListener { - Log.e(ContentValues.TAG, it.message.orEmpty()) - }.addOnCompleteListener { - imageProxy.image?.close() - imageProxy.close() - } + val bitmap = imageProxy.toBitmap() // Convert ImageProxy to Bitmap + val binaryBitmap = createBinaryBitmap(bitmap) + val reader = MultiFormatReader() + try { + val result = reader.decode(binaryBitmap) + cb(result.text, formatToString(result.barcodeFormat)) + } catch (e: NotFoundException) { + cb(null, null) + } catch (e: ReaderException) { + cb(null, null) + } finally { + imageProxy.close() } } + private fun createBinaryBitmap(bitmap: Bitmap): BinaryBitmap { + val pixels = IntArray(bitmap.width * bitmap.height) + bitmap.getPixels(pixels, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height) + val source = + RGBLuminanceSource(bitmap.width, bitmap.height, pixels) + return BinaryBitmap(HybridBinarizer(source)) + } + fun getAnalysisUseCase(cb: (String?, String?) -> Unit): ImageAnalysis { - val options = BarcodeScannerOptions.Builder().setBarcodeFormats( - Barcode.FORMAT_CODE_128, - Barcode.FORMAT_CODE_39, - Barcode.FORMAT_CODE_93, - Barcode.FORMAT_EAN_8, - Barcode.FORMAT_EAN_13, - Barcode.FORMAT_QR_CODE, - Barcode.FORMAT_UPC_A, - Barcode.FORMAT_UPC_E, - Barcode.FORMAT_PDF417 - ).build() - val scanner = BarcodeScanning.getClient(options) - val analysisUseCase = ImageAnalysis.Builder() - .build() - + val analysisUseCase = ImageAnalysis.Builder().build() analysisUseCase.setAnalyzer( Executors.newSingleThreadExecutor() ) { imageProxy -> - processImageProxy(scanner, imageProxy, cb) + processImageProxy(imageProxy, cb) } return analysisUseCase } - - } \ No newline at end of file diff --git a/app/src/main/res/layout/frag_scanner.xml b/app/src/main/res/layout/frag_scanner.xml index a83093c..b5719f8 100644 --- a/app/src/main/res/layout/frag_scanner.xml +++ b/app/src/main/res/layout/frag_scanner.xml @@ -25,6 +25,7 @@ android:contentDescription="@string/manual" />