Skip to content

Barcode errorcheck #104

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 32 additions & 2 deletions BarcodeScanning.Native.Maui/Platform/Android/BarcodeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BarcodeResult> _barcodeResults;
private readonly CameraManager _cameraManager;
private TaskCompletionSource<BarcodeResult> barcodeCompletionsource= new TaskCompletionSource<BarcodeResult>();

internal BarcodeAnalyzer(CameraManager cameraManager)
{
Expand Down Expand Up @@ -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<BarcodeResult>();
lastDetectedBarcode = barcode.DisplayValue;
consecutiveCount = 1;
}
}
}
}
}

Expand All @@ -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<BarcodeResult>(){recognizedbarcodeResult});
});
}
}
catch (Exception)
Expand Down Expand Up @@ -153,4 +183,4 @@ protected override void Dispose(bool disposing)

base.Dispose(disposing);
}
}
}
35 changes: 32 additions & 3 deletions BarcodeScanning.Native.Maui/Platform/MaciOS/BarcodeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BarcodeResult> barcodeCompletionSource= new TaskCompletionSource<BarcodeResult>();
private string lastDetectedBarcode = "";
internal BarcodeAnalyzer(CameraManager cameraManager)
{
_barcodeResults = [];
Expand Down Expand Up @@ -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<BarcodeResult>();
lastDetectedBarcode = barcode.DisplayValue;
consecutiveCount = 1;
}
}
}
}
}

Expand All @@ -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<BarcodeResult>(){recognizedbarcodeResult});
});
}
catch (Exception ex)
{
Expand All @@ -100,4 +129,4 @@ protected override void Dispose(bool disposing)

base.Dispose(disposing);
}
}
}