9
9
using Xamarin . Google . MLKit . Vision . BarCode ;
10
10
using Xamarin . Google . MLKit . Vision . Common ;
11
11
12
- using MLKitBarcodeScanning = Xamarin . Google . MLKit . Vision . BarCode . BarcodeScanning ;
13
12
using Point = Microsoft . Maui . Graphics . Point ;
14
13
using Rect = Microsoft . Maui . Graphics . Rect ;
14
+ using Scanner = Xamarin . Google . MLKit . Vision . BarCode . BarcodeScanning ;
15
15
using Size = Android . Util . Size ;
16
16
17
17
namespace BarcodeScanning ;
18
18
19
- internal class BarcodeAnalyzer : Java . Lang . Object , ImageAnalysis . IAnalyzer , IOnSuccessListener , IOnCompleteListener
19
+ internal class BarcodeAnalyzer : Java . Lang . Object , ImageAnalysis . IAnalyzer
20
20
{
21
21
public Size DefaultTargetResolution => Methods . TargetResolution ( null ) ;
22
22
public int TargetCoordinateSystem => ImageAnalysis . CoordinateSystemViewReferenced ;
@@ -27,9 +27,7 @@ internal class BarcodeAnalyzer : Java.Lang.Object, ImageAnalysis.IAnalyzer, IOnS
27
27
28
28
private IBarcodeScanner ? _barcodeScanner ;
29
29
private CoordinateTransform ? _coordinateTransform ;
30
- private IImageProxy ? _proxy ;
31
30
32
- private bool _processInverted = false ;
33
31
private bool _updateCoordinateTransform = false ;
34
32
private int _transformDegrees = 0 ;
35
33
private Point _previewViewCenter = new ( ) ;
@@ -43,50 +41,39 @@ internal BarcodeAnalyzer(CameraManager cameraManager)
43
41
44
42
_previewViewRect . X = 0 ;
45
43
_previewViewRect . Y = 0 ;
46
-
47
- UpdateSymbologies ( ) ;
48
44
}
49
45
50
46
internal void UpdateSymbologies ( )
51
47
{
52
- if ( _cameraManager ? . CameraView is not null )
53
- {
54
- _barcodeScanner ? . Dispose ( ) ;
55
- _barcodeScanner = MLKitBarcodeScanning . GetClient ( new BarcodeScannerOptions . Builder ( )
56
- . SetBarcodeFormats ( Methods . ConvertBarcodeFormats ( _cameraManager . CameraView . BarcodeSymbologies ) )
57
- . Build ( ) ) ;
58
- }
48
+ _barcodeScanner ? . Dispose ( ) ;
49
+ _barcodeScanner = Scanner . GetClient ( new BarcodeScannerOptions . Builder ( )
50
+ . SetBarcodeFormats ( Methods . ConvertBarcodeFormats ( _cameraManager ? . CameraView ? . BarcodeSymbologies ?? BarcodeFormats . All ) )
51
+ . Build ( ) ) ;
59
52
}
60
53
61
54
public void Analyze ( IImageProxy proxy )
62
55
{
63
- _proxy = proxy ;
64
-
65
56
try
66
57
{
67
- ArgumentNullException . ThrowIfNull ( _proxy ? . Image ) ;
58
+ ArgumentNullException . ThrowIfNull ( proxy ? . Image ) ;
68
59
ArgumentNullException . ThrowIfNull ( _cameraManager ? . CameraView ) ;
69
60
ArgumentNullException . ThrowIfNull ( _barcodeScanner ) ;
70
61
71
- _processInverted = _cameraManager . CameraView . ForceInverted ;
72
-
73
62
if ( _cameraManager . CameraView . PauseScanning )
74
- {
75
- CloseProxy ( ) ;
76
63
return ;
77
- }
78
64
79
65
if ( _cameraManager . CameraView . CaptureNextFrame )
80
66
{
81
67
_cameraManager . CameraView . CaptureNextFrame = false ;
82
- var image = new PlatformImage ( _proxy . ToBitmap ( ) ) ;
68
+ var image = new PlatformImage ( proxy . ToBitmap ( ) ) ;
83
69
_cameraManager . CameraView . TriggerOnImageCaptured ( image ) ;
84
70
}
85
71
86
- if ( _updateCoordinateTransform || _transformDegrees != _proxy . ImageInfo . RotationDegrees )
72
+ if ( _updateCoordinateTransform || _transformDegrees != proxy . ImageInfo . RotationDegrees )
87
73
{
88
- _coordinateTransform = _cameraManager . GetCoordinateTransform ( _proxy ) ;
89
- _transformDegrees = _proxy . ImageInfo . RotationDegrees ;
74
+ _coordinateTransform ? . Dispose ( ) ;
75
+ _coordinateTransform = _cameraManager . GetCoordinateTransform ( proxy ) ;
76
+ _transformDegrees = proxy . ImageInfo . RotationDegrees ;
90
77
91
78
_previewViewCenter . X = _cameraManager . PreviewView . Width / 2 ;
92
79
_previewViewCenter . Y = _cameraManager . PreviewView . Height / 2 ;
@@ -96,92 +83,69 @@ public void Analyze(IImageProxy proxy)
96
83
_updateCoordinateTransform = false ;
97
84
}
98
85
99
- using var inputImage = InputImage . FromMediaImage ( _proxy . Image , _proxy . ImageInfo . RotationDegrees ) ;
100
- _barcodeScanner . Process ( inputImage ) . AddOnSuccessListener ( this ) . AddOnCompleteListener ( this ) ;
101
- }
102
- catch ( Exception ex )
103
- {
104
- Debug . WriteLine ( ex ) ;
105
- CloseProxy ( ) ;
106
- }
107
- }
86
+ using var inputImage = InputImage . FromMediaImage ( proxy . Image , proxy . ImageInfo . RotationDegrees ) ;
87
+ using var task = _barcodeScanner . Process ( inputImage ) ;
88
+ var result = TasksClass . Await ( task ) ;
89
+
90
+ Java . Lang . Object ? invertedResult = null ;
91
+ if ( _cameraManager . CameraView . ForceInverted )
92
+ {
93
+ Methods . InvertLuminance ( proxy . Image ) ;
94
+ using var invertedImage = InputImage . FromMediaImage ( proxy . Image , proxy . ImageInfo . RotationDegrees ) ;
95
+ using var invertedTask = _barcodeScanner . Process ( invertedImage ) ;
96
+ invertedResult = TasksClass . Await ( invertedTask ) ;
97
+ }
108
98
109
- public void OnSuccess ( Java . Lang . Object ? result )
110
- {
111
- try
112
- {
113
99
lock ( _resultsLock )
114
100
{
115
- if ( _processInverted == _cameraManager ? . CameraView ? . ForceInverted )
116
- _barcodeResults . Clear ( ) ;
117
-
118
- if ( result is not JavaList javaList )
119
- return ;
120
-
121
- foreach ( Barcode barcode in javaList )
122
- {
123
- if ( barcode is null )
124
- continue ;
125
- if ( string . IsNullOrEmpty ( barcode . DisplayValue ) && string . IsNullOrEmpty ( barcode . RawValue ) )
126
- continue ;
127
-
128
- var barcodeResult = barcode . AsBarcodeResult ( _coordinateTransform ) ;
129
-
130
- if ( ( _cameraManager ? . CameraView ? . AimMode ?? false ) && ! barcodeResult . PreviewBoundingBox . Contains ( _previewViewCenter ) )
131
- continue ;
132
- if ( ( _cameraManager ? . CameraView ? . ViewfinderMode ?? false ) && ! _previewViewRect . Contains ( barcodeResult . PreviewBoundingBox ) )
133
- continue ;
134
-
135
- _barcodeResults . Add ( barcodeResult ) ;
136
- }
101
+ _barcodeResults . Clear ( ) ;
102
+ AddResultToSet ( result ) ;
103
+ AddResultToSet ( invertedResult ) ;
104
+
105
+ result ? . Dispose ( ) ;
106
+ invertedResult ? . Dispose ( ) ;
137
107
}
138
108
139
- if ( ! _processInverted )
140
- _cameraManager ? . CameraView ? . DetectionFinished ( _barcodeResults , _resultsLock ) ;
109
+ _cameraManager ? . CameraView ? . DetectionFinished ( _barcodeResults , _resultsLock ) ;
141
110
}
142
111
catch ( Exception ex )
143
112
{
144
113
Debug . WriteLine ( ex ) ;
145
114
}
146
- }
147
-
148
- public void OnComplete ( Android . Gms . Tasks . Task task )
149
- {
150
- if ( _processInverted )
115
+ finally
151
116
{
152
117
try
153
118
{
154
- _processInverted = false ;
155
-
156
- ArgumentNullException . ThrowIfNull ( _proxy ? . Image ) ;
157
- ArgumentNullException . ThrowIfNull ( _barcodeScanner ) ;
158
-
159
- Methods . InvertLuminance ( _proxy . Image ) ;
160
- using var inputImage = InputImage . FromMediaImage ( _proxy . Image , _proxy . ImageInfo . RotationDegrees ) ;
161
- _barcodeScanner . Process ( inputImage ) . AddOnSuccessListener ( this ) . AddOnCompleteListener ( this ) ;
119
+ proxy ? . Close ( ) ;
162
120
}
163
121
catch ( Exception ex )
164
122
{
165
123
Debug . WriteLine ( ex ) ;
166
- CloseProxy ( ) ;
124
+ MainThread . BeginInvokeOnMainThread ( ( ) => _cameraManager ? . Start ( ) ) ;
167
125
}
168
126
}
169
- else
170
- {
171
- CloseProxy ( ) ;
172
- }
173
127
}
174
128
175
- private void CloseProxy ( )
129
+ private void AddResultToSet ( Java . Lang . Object ? result )
176
130
{
177
- try
178
- {
179
- _proxy ? . Close ( ) ;
180
- }
181
- catch ( Exception ex )
131
+ if ( result is not JavaList javaList )
132
+ return ;
133
+
134
+ foreach ( Barcode barcode in javaList )
182
135
{
183
- Debug . WriteLine ( ex ) ;
184
- MainThread . BeginInvokeOnMainThread ( ( ) => _cameraManager ? . Start ( ) ) ;
136
+ if ( barcode is null )
137
+ continue ;
138
+ if ( string . IsNullOrEmpty ( barcode . DisplayValue ) && string . IsNullOrEmpty ( barcode . RawValue ) )
139
+ continue ;
140
+
141
+ var barcodeResult = barcode . AsBarcodeResult ( _coordinateTransform ) ;
142
+
143
+ if ( ( _cameraManager ? . CameraView ? . AimMode ?? false ) && ! barcodeResult . PreviewBoundingBox . Contains ( _previewViewCenter ) )
144
+ continue ;
145
+ if ( ( _cameraManager ? . CameraView ? . ViewfinderMode ?? false ) && ! _previewViewRect . Contains ( barcodeResult . PreviewBoundingBox ) )
146
+ continue ;
147
+
148
+ _barcodeResults . Add ( barcodeResult ) ;
185
149
}
186
150
}
187
151
0 commit comments