1
- using AndroidX . Camera . Core ;
1
+ using Android . Gms . Tasks ;
2
+ using AndroidX . Camera . Core ;
3
+ using AndroidX . Camera . View . Transform ;
4
+ using Microsoft . Maui . Graphics . Platform ;
2
5
using System . Diagnostics ;
6
+ using Xamarin . Google . MLKit . Vision . Common ;
3
7
4
8
using Size = Android . Util . Size ;
5
9
6
10
namespace BarcodeScanning ;
7
11
8
- internal class BarcodeAnalyzer : Java . Lang . Object , ImageAnalysis . IAnalyzer
12
+ internal class BarcodeAnalyzer : Java . Lang . Object , ImageAnalysis . IAnalyzer , IOnSuccessListener , IOnCompleteListener
9
13
{
10
14
public Size DefaultTargetResolution => Methods . TargetResolution ( CaptureQuality . Medium ) ;
11
15
public int TargetCoordinateSystem => ImageAnalysis . CoordinateSystemOriginal ;
12
16
17
+ private CoordinateTransform _coordinateTransform ;
18
+ private bool _processInverted ;
19
+ private IImageProxy _proxy ;
20
+
21
+ private readonly HashSet < BarcodeResult > _barcodeResults ;
13
22
private readonly CameraManager _cameraManager ;
14
23
15
24
internal BarcodeAnalyzer ( CameraManager cameraManager )
16
25
{
26
+ _barcodeResults = [ ] ;
17
27
_cameraManager = cameraManager ;
28
+ _processInverted = false ;
18
29
}
19
30
20
31
public void Analyze ( IImageProxy proxy )
21
32
{
22
33
try
23
34
{
24
- _cameraManager ? . AnalyzeFrame ( proxy ) ;
35
+ _proxy = proxy ;
36
+ _barcodeResults . Clear ( ) ;
37
+
38
+ if ( _cameraManager . CameraView . CaptureNextFrame )
39
+ {
40
+ _cameraManager . CameraView . CaptureNextFrame = false ;
41
+ var image = new PlatformImage ( _proxy . ToBitmap ( ) ) ;
42
+ _cameraManager . CameraView . TriggerOnImageCaptured ( image ) ;
43
+ }
44
+
45
+ if ( _cameraManager . RecalculateCoordinateTransform || _coordinateTransform is null )
46
+ _coordinateTransform = _cameraManager . GetCoordinateTransform ( _proxy ) ;
47
+
48
+ _processInverted = _cameraManager . CameraView . ForceInverted ;
49
+ using var inputImage = InputImage . FromMediaImage ( _proxy . Image , _proxy . ImageInfo . RotationDegrees ) ;
50
+ _cameraManager . BarcodeScanner . Process ( inputImage ) . AddOnSuccessListener ( this ) . AddOnCompleteListener ( this ) ;
25
51
}
26
- catch ( Exception ex )
52
+ catch ( Exception )
27
53
{
28
- Debug . WriteLine ( ex ) ;
54
+ CloseProxy ( ) ;
55
+ }
56
+ }
57
+
58
+ public void OnSuccess ( Java . Lang . Object result )
59
+ {
60
+ try
61
+ {
62
+ Methods . ProcessBarcodeResult ( result , _barcodeResults , _coordinateTransform ) ;
63
+
64
+ if ( ! _processInverted )
65
+ {
66
+ if ( _cameraManager . CameraView . AimMode )
67
+ {
68
+ var previewCenter = new Point ( _cameraManager . PreviewView . Width / 2 , _cameraManager . PreviewView . Height / 2 ) ;
69
+
70
+ foreach ( var barcode in _barcodeResults )
71
+ {
72
+ if ( ! barcode . PreviewBoundingBox . Contains ( previewCenter ) )
73
+ _barcodeResults . Remove ( barcode ) ;
74
+ }
75
+ }
76
+
77
+ if ( _cameraManager . CameraView . ViewfinderMode )
78
+ {
79
+ var previewRect = new RectF ( 0 , 0 , _cameraManager . PreviewView . Width , _cameraManager . PreviewView . Height ) ;
80
+
81
+ foreach ( var barcode in _barcodeResults )
82
+ {
83
+ if ( ! previewRect . Contains ( barcode . PreviewBoundingBox ) )
84
+ _barcodeResults . Remove ( barcode ) ;
85
+ }
86
+ }
87
+
88
+ _cameraManager ? . CameraView ? . DetectionFinished ( _barcodeResults ) ;
89
+ }
29
90
}
30
- finally
91
+ catch ( Exception )
92
+ {
93
+ }
94
+ }
95
+
96
+ public void OnComplete ( Android . Gms . Tasks . Task task )
97
+ {
98
+ if ( _processInverted )
31
99
{
32
100
try
33
101
{
34
- proxy ? . Close ( ) ;
102
+ Methods . InvertLuminance ( _proxy . Image ) ;
103
+
104
+ _processInverted = false ;
105
+ using var inputImage = InputImage . FromMediaImage ( _proxy . Image , _proxy . ImageInfo . RotationDegrees ) ;
106
+ _cameraManager . BarcodeScanner . Process ( inputImage ) . AddOnSuccessListener ( this ) . AddOnCompleteListener ( this ) ;
35
107
}
36
- catch ( Exception ex )
108
+ catch ( Exception )
37
109
{
38
- Debug . WriteLine ( ex ) ;
39
- MainThread . BeginInvokeOnMainThread ( ( ) => _cameraManager ? . Start ( ) ) ;
110
+ CloseProxy ( ) ;
40
111
}
41
112
}
113
+ else
114
+ {
115
+ CloseProxy ( ) ;
116
+ }
117
+
118
+ }
119
+
120
+ private void CloseProxy ( )
121
+ {
122
+ try
123
+ {
124
+ _proxy ? . Close ( ) ;
125
+ }
126
+ catch ( Exception ex )
127
+ {
128
+ Debug . WriteLine ( ex ) ;
129
+ MainThread . BeginInvokeOnMainThread ( ( ) => _cameraManager ? . Start ( ) ) ;
130
+ }
131
+ }
132
+
133
+ protected override void Dispose ( bool disposing )
134
+ {
135
+ _coordinateTransform ? . Dispose ( ) ;
136
+
137
+ base . Dispose ( disposing ) ;
42
138
}
43
139
}
0 commit comments