Deleted GoogleML usage

This commit is contained in:
soraefir 2024-03-24 12:40:18 +01:00
parent af97381bdb
commit 8181da421e
Signed by: sora
GPG Key ID: A362EA0491E2EEA0
7 changed files with 49 additions and 64 deletions

View File

@ -77,6 +77,5 @@ dependencies {
implementation 'com.google.code.gson:gson:2.10.1' implementation 'com.google.code.gson:gson:2.10.1'
implementation 'com.google.android.material:material:1.11.0' implementation 'com.google.android.material:material:1.11.0'
implementation 'com.google.zxing:core:3.5.3' implementation 'com.google.zxing:core:3.5.3'
implementation 'com.google.mlkit:barcode-scanning:17.2.0'
} }

View File

@ -45,6 +45,7 @@ class Scanner : Fragment() {
return binding.root return binding.root
} }
private fun startCreateEntry() { private fun startCreateEntry() {
val createEntryFragment = CreateEntry() val createEntryFragment = CreateEntry()
createEntryFragment.arguments = createEntryFragment.arguments =
@ -89,13 +90,14 @@ class Scanner : Fragment() {
} }
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
val analysisUseCase = getAnalysisUseCase { code, format -> val analysisUseCase = getAnalysisUseCase { code, format ->
if (code != null && format != null) { if (!code.isNullOrEmpty() && !format.isNullOrEmpty()) {
this.code = code this.code = code
this.fmt = format this.fmt = format
binding.btnScanDone.isEnabled = true }
val isDone = this.code.isNotEmpty() && this.fmt.isNotEmpty()
} else { requireActivity().runOnUiThread {
binding.btnScanDone.isEnabled = false binding.btnScanDone.isEnabled = isDone
binding.ScanActive.isEnabled = !isDone
} }
} }
try { try {

View File

@ -1,6 +1,5 @@
package net.helcel.fidelity.tools package net.helcel.fidelity.tools
import com.google.mlkit.vision.barcode.common.Barcode
import com.google.zxing.BarcodeFormat import com.google.zxing.BarcodeFormat
object BarcodeFormatConverter { object BarcodeFormatConverter {
@ -20,17 +19,17 @@ object BarcodeFormatConverter {
} }
} }
fun formatToString(f: Int): String { fun formatToString(f: BarcodeFormat): String {
return when (f) { return when (f) {
Barcode.FORMAT_CODE_128 -> "CODE_128" BarcodeFormat.CODE_128 -> "CODE_128"
Barcode.FORMAT_CODE_39 -> "CODE_39" BarcodeFormat.CODE_39 -> "CODE_39"
Barcode.FORMAT_CODE_93 -> "CODE_93" BarcodeFormat.CODE_93 -> "CODE_93"
Barcode.FORMAT_EAN_8 -> "EAN_8" BarcodeFormat.EAN_8 -> "EAN_8"
Barcode.FORMAT_EAN_13 -> "EAN_13" BarcodeFormat.EAN_13 -> "EAN_13"
Barcode.FORMAT_QR_CODE -> "CODE_QR" BarcodeFormat.QR_CODE -> "CODE_QR"
Barcode.FORMAT_UPC_A -> "UPC_A" BarcodeFormat.UPC_A -> "UPC_A"
Barcode.FORMAT_UPC_E -> "UPC_E" BarcodeFormat.UPC_E -> "UPC_E"
Barcode.FORMAT_PDF417 -> "PDF_417" BarcodeFormat.PDF_417 -> "PDF_417"
else -> throw Exception("Unsupported Format: $f") else -> throw Exception("Unsupported Format: $f")
} }
} }

View File

@ -1,16 +1,16 @@
package net.helcel.fidelity.tools package net.helcel.fidelity.tools
import android.content.ContentValues import android.graphics.Bitmap
import android.util.Log
import androidx.annotation.OptIn import androidx.annotation.OptIn
import androidx.camera.core.ExperimentalGetImage import androidx.camera.core.ExperimentalGetImage
import androidx.camera.core.ImageAnalysis import androidx.camera.core.ImageAnalysis
import androidx.camera.core.ImageProxy import androidx.camera.core.ImageProxy
import com.google.mlkit.vision.barcode.BarcodeScanner import com.google.zxing.BinaryBitmap
import com.google.mlkit.vision.barcode.BarcodeScannerOptions import com.google.zxing.MultiFormatReader
import com.google.mlkit.vision.barcode.BarcodeScanning import com.google.zxing.NotFoundException
import com.google.mlkit.vision.barcode.common.Barcode import com.google.zxing.RGBLuminanceSource
import com.google.mlkit.vision.common.InputImage import com.google.zxing.ReaderException
import com.google.zxing.common.HybridBinarizer
import net.helcel.fidelity.tools.BarcodeFormatConverter.formatToString import net.helcel.fidelity.tools.BarcodeFormatConverter.formatToString
import java.util.concurrent.Executors import java.util.concurrent.Executors
@ -19,57 +19,39 @@ object BarcodeScanner {
@OptIn(ExperimentalGetImage::class) @OptIn(ExperimentalGetImage::class)
private fun processImageProxy( private fun processImageProxy(
barcodeScanner: BarcodeScanner,
imageProxy: ImageProxy, imageProxy: ImageProxy,
cb: (String?, String?) -> Unit cb: (String?, String?) -> Unit
) { ) {
val bitmap = imageProxy.toBitmap() // Convert ImageProxy to Bitmap
imageProxy.image?.let { image -> val binaryBitmap = createBinaryBitmap(bitmap)
val inputImage = val reader = MultiFormatReader()
InputImage.fromMediaImage( try {
image, val result = reader.decode(binaryBitmap)
imageProxy.imageInfo.rotationDegrees cb(result.text, formatToString(result.barcodeFormat))
) } catch (e: NotFoundException) {
cb(null, null)
barcodeScanner.process(inputImage) } catch (e: ReaderException) {
.addOnSuccessListener { barcodeList -> cb(null, null)
val barcode = } finally {
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() 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 { fun getAnalysisUseCase(cb: (String?, String?) -> Unit): ImageAnalysis {
val options = BarcodeScannerOptions.Builder().setBarcodeFormats( val analysisUseCase = ImageAnalysis.Builder().build()
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()
analysisUseCase.setAnalyzer( analysisUseCase.setAnalyzer(
Executors.newSingleThreadExecutor() Executors.newSingleThreadExecutor()
) { imageProxy -> ) { imageProxy ->
processImageProxy(scanner, imageProxy, cb) processImageProxy(imageProxy, cb)
} }
return analysisUseCase return analysisUseCase
} }
} }

View File

@ -25,6 +25,7 @@
android:contentDescription="@string/manual" /> android:contentDescription="@string/manual" />
<com.google.android.material.progressindicator.CircularProgressIndicator <com.google.android.material.progressindicator.CircularProgressIndicator
android:id="@+id/ScanActive"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"

View File

@ -4,4 +4,5 @@ plugins {
id 'com.android.application' version '8.3.1' apply false id 'com.android.application' version '8.3.1' apply false
id 'com.android.library' version '8.3.1' apply false id 'com.android.library' version '8.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.9.23' apply false id 'org.jetbrains.kotlin.android' version '1.9.23' apply false
id 'com.autonomousapps.dependency-analysis' version '1.30.0' apply true
} }

View File

@ -14,5 +14,6 @@ dependencyResolutionManagement {
maven { url 'https://jitpack.io' } maven { url 'https://jitpack.io' }
} }
} }
rootProject.name = "Fidelity" rootProject.name = "Fidelity"
include ':app' include ':app'