-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
309 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 106 additions & 10 deletions
116
BarcodeScanning.Native.Maui/Platform/Android/BarcodeAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,139 @@ | ||
using AndroidX.Camera.Core; | ||
using Android.Gms.Tasks; | ||
using AndroidX.Camera.Core; | ||
using AndroidX.Camera.View.Transform; | ||
using Microsoft.Maui.Graphics.Platform; | ||
using System.Diagnostics; | ||
using Xamarin.Google.MLKit.Vision.Common; | ||
|
||
using Size = Android.Util.Size; | ||
|
||
namespace BarcodeScanning; | ||
|
||
internal class BarcodeAnalyzer : Java.Lang.Object, ImageAnalysis.IAnalyzer | ||
internal class BarcodeAnalyzer : Java.Lang.Object, ImageAnalysis.IAnalyzer, IOnSuccessListener, IOnCompleteListener | ||
{ | ||
public Size DefaultTargetResolution => Methods.TargetResolution(CaptureQuality.Medium); | ||
public int TargetCoordinateSystem => ImageAnalysis.CoordinateSystemOriginal; | ||
|
||
private CoordinateTransform _coordinateTransform; | ||
private bool _processInverted; | ||
private IImageProxy _proxy; | ||
|
||
private readonly HashSet<BarcodeResult> _barcodeResults; | ||
private readonly CameraManager _cameraManager; | ||
|
||
internal BarcodeAnalyzer(CameraManager cameraManager) | ||
{ | ||
_barcodeResults = []; | ||
_cameraManager = cameraManager; | ||
_processInverted = false; | ||
} | ||
|
||
public void Analyze(IImageProxy proxy) | ||
{ | ||
try | ||
{ | ||
_cameraManager?.AnalyzeFrame(proxy); | ||
_proxy = proxy; | ||
_barcodeResults.Clear(); | ||
|
||
if (_cameraManager.CameraView.CaptureNextFrame) | ||
{ | ||
_cameraManager.CameraView.CaptureNextFrame = false; | ||
var image = new PlatformImage(_proxy.ToBitmap()); | ||
_cameraManager.CameraView.TriggerOnImageCaptured(image); | ||
} | ||
|
||
if (_cameraManager.RecalculateCoordinateTransform || _coordinateTransform is null) | ||
_coordinateTransform = _cameraManager.GetCoordinateTransform(_proxy); | ||
|
||
_processInverted = _cameraManager.CameraView.ForceInverted; | ||
using var inputImage = InputImage.FromMediaImage(_proxy.Image, _proxy.ImageInfo.RotationDegrees); | ||
_cameraManager.BarcodeScanner.Process(inputImage).AddOnSuccessListener(this).AddOnCompleteListener(this); | ||
} | ||
catch (Exception ex) | ||
catch (Exception) | ||
{ | ||
Debug.WriteLine(ex); | ||
CloseProxy(); | ||
} | ||
} | ||
|
||
public void OnSuccess(Java.Lang.Object result) | ||
{ | ||
try | ||
{ | ||
Methods.ProcessBarcodeResult(result, _barcodeResults, _coordinateTransform); | ||
|
||
if (!_processInverted) | ||
{ | ||
if (_cameraManager.CameraView.AimMode) | ||
{ | ||
var previewCenter = new Point(_cameraManager.PreviewView.Width / 2, _cameraManager.PreviewView.Height / 2); | ||
|
||
foreach (var barcode in _barcodeResults) | ||
{ | ||
if (!barcode.PreviewBoundingBox.Contains(previewCenter)) | ||
_barcodeResults.Remove(barcode); | ||
} | ||
} | ||
|
||
if (_cameraManager.CameraView.ViewfinderMode) | ||
{ | ||
var previewRect = new RectF(0, 0, _cameraManager.PreviewView.Width, _cameraManager.PreviewView.Height); | ||
|
||
foreach (var barcode in _barcodeResults) | ||
{ | ||
if (!previewRect.Contains(barcode.PreviewBoundingBox)) | ||
_barcodeResults.Remove(barcode); | ||
} | ||
} | ||
|
||
_cameraManager?.CameraView?.DetectionFinished(_barcodeResults); | ||
} | ||
} | ||
finally | ||
catch (Exception) | ||
{ | ||
} | ||
} | ||
|
||
public void OnComplete(Android.Gms.Tasks.Task task) | ||
{ | ||
if (_processInverted) | ||
{ | ||
try | ||
{ | ||
proxy?.Close(); | ||
Methods.InvertLuminance(_proxy.Image); | ||
|
||
_processInverted = false; | ||
using var inputImage = InputImage.FromMediaImage(_proxy.Image, _proxy.ImageInfo.RotationDegrees); | ||
_cameraManager.BarcodeScanner.Process(inputImage).AddOnSuccessListener(this).AddOnCompleteListener(this); | ||
} | ||
catch (Exception ex) | ||
catch (Exception) | ||
{ | ||
Debug.WriteLine(ex); | ||
MainThread.BeginInvokeOnMainThread(() => _cameraManager?.Start()); | ||
CloseProxy(); | ||
} | ||
} | ||
else | ||
{ | ||
CloseProxy(); | ||
} | ||
|
||
} | ||
|
||
private void CloseProxy() | ||
{ | ||
try | ||
{ | ||
_proxy?.Close(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Debug.WriteLine(ex); | ||
MainThread.BeginInvokeOnMainThread(() => _cameraManager?.Start()); | ||
} | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
_coordinateTransform?.Dispose(); | ||
|
||
base.Dispose(disposing); | ||
} | ||
} |
Oops, something went wrong.