diff --git a/BarcodeScanning.Native.Maui/Platform/Android/BarcodeAnalyzer.cs b/BarcodeScanning.Native.Maui/Platform/Android/BarcodeAnalyzer.cs index 26f1338..8721f01 100644 --- a/BarcodeScanning.Native.Maui/Platform/Android/BarcodeAnalyzer.cs +++ b/BarcodeScanning.Native.Maui/Platform/Android/BarcodeAnalyzer.cs @@ -20,9 +20,12 @@ internal class BarcodeAnalyzer : Java.Lang.Object, ImageAnalysis.IAnalyzer, IOnS private CoordinateTransform _coordinateTransform; private bool _processInverted; private IImageProxy _proxy; + private String lastDetectedBarcode = ""; + private int consecutiveCount = 0; private readonly HashSet _barcodeResults; private readonly CameraManager _cameraManager; + private TaskCompletionSource barcodeCompletionsource= new TaskCompletionSource(); internal BarcodeAnalyzer(CameraManager cameraManager) { @@ -87,6 +90,29 @@ public void OnSuccess(Java.Lang.Object result) { if (!barcode.PreviewBoundingBox.Contains(previewCenter)) _barcodeResults.Remove(barcode); + else + { + if (barcode != null) + { + //Iterates atleast 3 times to check it the code from subsequent frame are same - Streaming camera source -keeps getting incorrect results + if (barcode.DisplayValue.Equals(lastDetectedBarcode)) + { + consecutiveCount++; + if (consecutiveCount >= 3) + { + Console.WriteLine("bar code recognized"); + barcodeCompletionSource.SetResult(barcode); + // Call your .NET MAUI backend with the barcode value + } + } + else + { + barcodeCompletionSource = new TaskCompletionSource(); + lastDetectedBarcode = barcode.DisplayValue; + consecutiveCount = 1; + } + } + } } } @@ -101,7 +127,11 @@ public void OnSuccess(Java.Lang.Object result) } } - _cameraManager?.CameraView?.DetectionFinished(_barcodeResults); + MainThread.BeginInvokeOnMainThread( async()=>{ + var recognizedbarcodeResult = await barcodeCompletionSource.Task; + if(!string.IsNullOrEmpty(recognizedbarcodeResult?.DisplayValue)) + _cameraManager?.CameraView?.DetectionFinished(new HashSet(){recognizedbarcodeResult}); + }); } } catch (Exception) @@ -153,4 +183,4 @@ protected override void Dispose(bool disposing) base.Dispose(disposing); } -} \ No newline at end of file +} diff --git a/BarcodeScanning.Native.Maui/Platform/MaciOS/BarcodeAnalyzer.cs b/BarcodeScanning.Native.Maui/Platform/MaciOS/BarcodeAnalyzer.cs index 48c4d7a..a40271a 100644 --- a/BarcodeScanning.Native.Maui/Platform/MaciOS/BarcodeAnalyzer.cs +++ b/BarcodeScanning.Native.Maui/Platform/MaciOS/BarcodeAnalyzer.cs @@ -14,7 +14,9 @@ internal class BarcodeAnalyzer : AVCaptureVideoDataOutputSampleBufferDelegate private readonly CameraManager _cameraManager; private readonly VNDetectBarcodesRequest _detectBarcodesRequest; private readonly VNSequenceRequestHandler _sequenceRequestHandler; - + private int consecutiveCount =0; + private TaskCompletionSource barcodeCompletionSource= new TaskCompletionSource(); + private string lastDetectedBarcode = ""; internal BarcodeAnalyzer(CameraManager cameraManager) { _barcodeResults = []; @@ -59,6 +61,29 @@ public override void DidOutputSampleBuffer(AVCaptureOutput captureOutput, CMSamp { if (!barcode.PreviewBoundingBox.Contains(previewCenter)) _barcodeResults.Remove(barcode); + else + { + if (barcode != null) + { + if (barcode.DisplayValue.Equals(lastDetectedBarcode)) + { + consecutiveCount++; + //Checksum added to keep the barcode more error free- especially in streaming source + if (consecutiveCount >= 3) + { + Console.WriteLine("bar code recognized"); + barcodeCompletionSource.SetResult(barcode); + // Call your .NET MAUI backend with the barcode value + } + } + else + { + barcodeCompletionSource = new TaskCompletionSource(); + lastDetectedBarcode = barcode.DisplayValue; + consecutiveCount = 1; + } + } + } } } @@ -73,7 +98,11 @@ public override void DidOutputSampleBuffer(AVCaptureOutput captureOutput, CMSamp } } - _cameraManager.CameraView.DetectionFinished(_barcodeResults); + MainThread.BeginInvokeOnMainThread( async()=>{ + var recognizedbarcodeResult = await barcodeCompletionSource.Task; + if(!string.IsNullOrEmpty(recognizedbarcodeResult?.DisplayValue)) + _cameraManager?.CameraView?.DetectionFinished(new HashSet(){recognizedbarcodeResult}); + }); } catch (Exception ex) { @@ -100,4 +129,4 @@ protected override void Dispose(bool disposing) base.Dispose(disposing); } -} \ No newline at end of file +}