From a2d545804961bc04ce3cd28f0cb15a12bc08186a Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Fri, 16 Feb 2024 16:30:22 +0100 Subject: [PATCH] feat(barcode-scanning): vote system --- .../mlkit/barcodescanning/BarcodeScanner.java | 23 ++++++++++++++++++- .../ios/Plugin/BarcodeScanner.swift | 19 ++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/packages/barcode-scanning/android/src/main/java/io/capawesome/capacitorjs/plugins/mlkit/barcodescanning/BarcodeScanner.java b/packages/barcode-scanning/android/src/main/java/io/capawesome/capacitorjs/plugins/mlkit/barcodescanning/BarcodeScanner.java index 24786f5..709c42f 100644 --- a/packages/barcode-scanning/android/src/main/java/io/capawesome/capacitorjs/plugins/mlkit/barcodescanning/BarcodeScanner.java +++ b/packages/barcode-scanning/android/src/main/java/io/capawesome/capacitorjs/plugins/mlkit/barcodescanning/BarcodeScanner.java @@ -44,6 +44,7 @@ import io.capawesome.capacitorjs.plugins.mlkit.barcodescanning.classes.results.GetMaxZoomRatioResult; import io.capawesome.capacitorjs.plugins.mlkit.barcodescanning.classes.results.GetMinZoomRatioResult; import io.capawesome.capacitorjs.plugins.mlkit.barcodescanning.classes.results.GetZoomRatioResult; +import java.util.HashMap; public class BarcodeScanner implements ImageAnalysis.Analyzer { @@ -70,6 +71,8 @@ public class BarcodeScanner implements ImageAnalysis.Analyzer { @Nullable private ModuleInstallProgressListener moduleInstallProgressListener; + private HashMap barcodeRawValueVotes = new HashMap(); + private boolean isTorchEnabled = false; public BarcodeScanner(BarcodeScannerPlugin plugin) { @@ -135,6 +138,7 @@ public void stopScan() { camera = null; barcodeScannerInstance = null; scanSettings = null; + barcodeRawValueVotes.clear(); } public void readBarcodesFromImage(String path, ScanSettings scanSettings, ReadBarcodesFromImageResultCallback callback) @@ -347,7 +351,10 @@ public void analyze(@NonNull ImageProxy imageProxy) { return; } for (Barcode barcode : barcodes) { - handleScannedBarcode(barcode, imageSize); + Integer votes = voteForBarcode(barcode); + if (votes >= 10) { + handleScannedBarcode(barcode, imageSize); + } } } ) @@ -418,4 +425,18 @@ private GmsBarcodeScannerOptions buildGmsBarcodeScannerOptions(ScanSettings scan GmsBarcodeScannerOptions options = new GmsBarcodeScannerOptions.Builder().setBarcodeFormats(formats[0], formats).build(); return options; } + + private Integer voteForBarcode(Barcode barcode) { + String rawValue = barcode.getRawValue(); + if (rawValue == null) { + return 1; + } else { + if (barcodeRawValueVotes.containsKey(rawValue)) { + barcodeRawValueVotes.put(rawValue, barcodeRawValueVotes.get(rawValue) + 1); + } else { + barcodeRawValueVotes.put(rawValue, 1); + } + return barcodeRawValueVotes.get(rawValue); + } + } } diff --git a/packages/barcode-scanning/ios/Plugin/BarcodeScanner.swift b/packages/barcode-scanning/ios/Plugin/BarcodeScanner.swift index 9de324c..605f8d6 100644 --- a/packages/barcode-scanning/ios/Plugin/BarcodeScanner.swift +++ b/packages/barcode-scanning/ios/Plugin/BarcodeScanner.swift @@ -15,6 +15,7 @@ typealias MLKitBarcodeScanner = MLKitBarcodeScanning.BarcodeScanner private var cameraView: BarcodeScannerView? private var scanCompletionHandler: (([Barcode]?, AVCaptureVideoOrientation?, String?) -> Void)? + private var barcodeRawValueVotes = [String: Int]() init(plugin: BarcodeScannerPlugin) { self.plugin = plugin @@ -50,6 +51,7 @@ typealias MLKitBarcodeScanner = MLKitBarcodeScanning.BarcodeScanner self.cameraView = nil } self.scanCompletionHandler = nil + self.barcodeRawValueVotes.removeAll() } @objc public func readBarcodesFromImage(imageUrl: URL, settings: ScanSettings, completion: @escaping ([Barcode]?, String?) -> Void) { @@ -257,6 +259,18 @@ typealias MLKitBarcodeScanner = MLKitBarcodeScanning.BarcodeScanner private func handleScannedBarcode(barcode: Barcode, imageSize: CGSize, videoOrientation: AVCaptureVideoOrientation?) { plugin.notifyBarcodeScannedListener(barcode: barcode, imageSize: imageSize, videoOrientation: videoOrientation) } + + private func voteForBarcode(barcode: Barcode) -> Int { + guard let rawValue = barcode.rawValue else { + return 1 + } + if let votes = self.barcodeRawValueVotes[rawValue] { + self.barcodeRawValueVotes[rawValue] = votes + 1 + } else { + self.barcodeRawValueVotes[rawValue] = 1 + } + return self.barcodeRawValueVotes[rawValue] ?? 1 + } } extension BarcodeScanner: BarcodeScannerViewDelegate { @@ -266,7 +280,10 @@ extension BarcodeScanner: BarcodeScannerViewDelegate { self.stopScan() } else { for barcode in barcodes { - self.handleScannedBarcode(barcode: barcode, imageSize: imageSize, videoOrientation: videoOrientation) + let votes = self.voteForBarcode(barcode: barcode) + if votes >= 10 { + self.handleScannedBarcode(barcode: barcode, imageSize: imageSize, videoOrientation: videoOrientation) + } } } }