Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(barcode-scanning): vote system #129

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading