Skip to content

Commit

Permalink
feat(barcode-scanning): vote system (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz authored Oct 8, 2024
1 parent 1310898 commit 4f731dc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -70,6 +71,8 @@ public class BarcodeScanner implements ImageAnalysis.Analyzer {
@Nullable
private ModuleInstallProgressListener moduleInstallProgressListener;

private HashMap<String, Integer> barcodeRawValueVotes = new HashMap<String, Integer>();

private boolean isTorchEnabled = false;

public BarcodeScanner(BarcodeScannerPlugin plugin) {
Expand Down Expand Up @@ -135,6 +138,7 @@ public void stopScan() {
camera = null;
barcodeScannerInstance = null;
scanSettings = null;
barcodeRawValueVotes.clear();
}

public void readBarcodesFromImage(String path, ScanSettings scanSettings, ReadBarcodesFromImageResultCallback callback)
Expand Down Expand Up @@ -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);
}
}
}
)
Expand Down Expand Up @@ -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);
}
}
}
19 changes: 18 additions & 1 deletion packages/barcode-scanning/ios/Plugin/BarcodeScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}
}
}
Expand Down

0 comments on commit 4f731dc

Please sign in to comment.