-
-
Notifications
You must be signed in to change notification settings - Fork 535
/
Copy pathmobile_scanner_method_channel.dart
344 lines (288 loc) · 10.2 KB
/
mobile_scanner_method_channel.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:mobile_scanner/src/enums/barcode_format.dart';
import 'package:mobile_scanner/src/enums/mobile_scanner_authorization_state.dart';
import 'package:mobile_scanner/src/enums/mobile_scanner_error_code.dart';
import 'package:mobile_scanner/src/enums/torch_state.dart';
import 'package:mobile_scanner/src/mobile_scanner_exception.dart';
import 'package:mobile_scanner/src/mobile_scanner_platform_interface.dart';
import 'package:mobile_scanner/src/mobile_scanner_view_attributes.dart';
import 'package:mobile_scanner/src/objects/barcode.dart';
import 'package:mobile_scanner/src/objects/barcode_capture.dart';
import 'package:mobile_scanner/src/objects/start_options.dart';
/// An implementation of [MobileScannerPlatform] that uses method channels.
class MethodChannelMobileScanner extends MobileScannerPlatform {
/// The name of the barcode event that is sent when a barcode is scanned.
@visibleForTesting
static const String kBarcodeEventName = 'barcode';
/// The name of the error event that is sent when a barcode scan error occurs.
@visibleForTesting
static const String kBarcodeErrorEventName = 'MOBILE_SCANNER_BARCODE_ERROR';
/// The method channel used to interact with the native platform.
@visibleForTesting
final methodChannel = const MethodChannel(
'dev.steenbakker.mobile_scanner/scanner/method',
);
/// The event channel that sends back scanned barcode events.
@visibleForTesting
final eventChannel = const EventChannel(
'dev.steenbakker.mobile_scanner/scanner/event',
);
Stream<Map<Object?, Object?>>? _eventsStream;
Stream<Map<Object?, Object?>> get eventsStream {
_eventsStream ??=
eventChannel.receiveBroadcastStream().cast<Map<Object?, Object?>>();
return _eventsStream!;
}
int? _textureId;
bool _pausing = false;
/// Parse a [BarcodeCapture] from the given [event].
BarcodeCapture? _parseBarcode(Map<Object?, Object?>? event) {
if (event == null) {
return null;
}
final Object? data = event['data'];
if (data == null || data is! List<Object?>) {
return null;
}
final List<Map<Object?, Object?>> barcodes =
data.cast<Map<Object?, Object?>>();
if (defaultTargetPlatform == TargetPlatform.android ||
defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.macOS) {
final Map<Object?, Object?>? imageData =
event['image'] as Map<Object?, Object?>?;
final Uint8List? image = imageData?['bytes'] as Uint8List?;
final double? width = imageData?['width'] as double?;
final double? height = imageData?['height'] as double?;
return BarcodeCapture(
raw: event,
barcodes: barcodes.map(Barcode.fromNative).toList(),
image: image,
size: width == null || height == null ? Size.zero : Size(width, height),
);
}
throw const MobileScannerException(
errorCode: MobileScannerErrorCode.genericError,
errorDetails: MobileScannerErrorDetails(
message: 'Only Android, iOS and macOS are supported.',
),
);
}
/// Parse a [MobileScannerBarcodeException] from the given [error] and [stackTrace], and throw it.
///
/// If the error is not a [PlatformException],
/// with [kBarcodeErrorEventName] as [PlatformException.code], the error is rethrown as-is.
Never _parseBarcodeError(Object error, StackTrace stackTrace) {
if (error case PlatformException(:final String code, :final String? message)
when code == kBarcodeErrorEventName) {
throw MobileScannerBarcodeException(message);
}
Error.throwWithStackTrace(error, stackTrace);
}
/// Request permission to access the camera.
///
/// Throws a [MobileScannerException] if the permission is not granted.
Future<void> _requestCameraPermission() async {
try {
final MobileScannerAuthorizationState authorizationState =
MobileScannerAuthorizationState.fromRawValue(
await methodChannel.invokeMethod<int>('state') ?? 0,
);
switch (authorizationState) {
// Authorization was already granted, no need to request it again.
case MobileScannerAuthorizationState.authorized:
return;
// Android does not have an undetermined authorization state.
// So if the permission was denied, request it again.
case MobileScannerAuthorizationState.denied:
case MobileScannerAuthorizationState.undetermined:
final bool permissionGranted =
await methodChannel.invokeMethod<bool>('request') ?? false;
if (!permissionGranted) {
throw const MobileScannerException(
errorCode: MobileScannerErrorCode.permissionDenied,
);
}
}
} on PlatformException catch (error) {
// If the permission state is invalid, that is an error.
throw MobileScannerException(
errorCode: MobileScannerErrorCode.genericError,
errorDetails: MobileScannerErrorDetails(
code: error.code,
details: error.details as Object?,
message: error.message,
),
);
}
}
@override
Stream<BarcodeCapture?> get barcodesStream {
// Handle incoming barcode events.
// The error events are transformed to `MobileScannerBarcodeException` where possible.
return eventsStream
.where((e) => e['name'] == kBarcodeEventName)
.map((event) => _parseBarcode(event))
.handleError(_parseBarcodeError);
}
@override
Stream<TorchState> get torchStateStream {
return eventsStream
.where((event) => event['name'] == 'torchState')
.map((event) => TorchState.fromRawValue(event['data'] as int? ?? 0));
}
@override
Stream<double> get zoomScaleStateStream {
return eventsStream
.where((event) => event['name'] == 'zoomScaleState')
.map((event) => event['data'] as double? ?? 0.0);
}
@override
Future<BarcodeCapture?> analyzeImage(
String path, {
List<BarcodeFormat> formats = const <BarcodeFormat>[],
}) async {
try {
final Map<Object?, Object?>? result =
await methodChannel.invokeMapMethod<Object?, Object?>(
'analyzeImage',
{
'filePath': path,
'formats': formats.isEmpty
? null
: [
for (final BarcodeFormat format in formats)
if (format != BarcodeFormat.unknown) format.rawValue,
],
},
);
return _parseBarcode(result);
} on PlatformException catch (error) {
// Handle any errors from analyze image requests.
if (error.code == kBarcodeErrorEventName) {
throw MobileScannerBarcodeException(error.message);
}
return null;
}
}
@override
Widget buildCameraView() {
if (_textureId == null) {
return const SizedBox();
}
return Texture(textureId: _textureId!);
}
@override
Future<void> resetZoomScale() async {
await methodChannel.invokeMethod<void>('resetScale');
}
@override
Future<void> setZoomScale(double zoomScale) async {
await methodChannel.invokeMethod<void>('setScale', zoomScale);
}
@override
Future<MobileScannerViewAttributes> start(StartOptions startOptions) async {
if (!_pausing && _textureId != null) {
throw const MobileScannerException(
errorCode: MobileScannerErrorCode.controllerAlreadyInitialized,
errorDetails: MobileScannerErrorDetails(
message: 'The scanner was already started.',
),
);
}
await _requestCameraPermission();
Map<String, Object?>? startResult;
try {
startResult = await methodChannel.invokeMapMethod<String, Object?>(
'start',
startOptions.toMap(),
);
} on PlatformException catch (error) {
throw MobileScannerException(
errorCode: MobileScannerErrorCode.fromPlatformException(error),
errorDetails: MobileScannerErrorDetails(
code: error.code,
details: error.details as Object?,
message: error.message,
),
);
}
if (startResult == null) {
throw const MobileScannerException(
errorCode: MobileScannerErrorCode.genericError,
errorDetails: MobileScannerErrorDetails(
message: 'The start method did not return a view configuration.',
),
);
}
final int? textureId = startResult['textureId'] as int?;
if (textureId == null) {
throw const MobileScannerException(
errorCode: MobileScannerErrorCode.genericError,
errorDetails: MobileScannerErrorDetails(
message: 'The start method did not return a texture id.',
),
);
}
_textureId = textureId;
final int? numberOfCameras = startResult['numberOfCameras'] as int?;
final TorchState currentTorchState = TorchState.fromRawValue(
startResult['currentTorchState'] as int? ?? -1,
);
final Size size;
if (startResult['size']
case {'width': final double width, 'height': final double height}) {
size = Size(width, height);
} else {
size = Size.zero;
}
_pausing = false;
return MobileScannerViewAttributes(
currentTorchMode: currentTorchState,
numberOfCameras: numberOfCameras,
size: size,
);
}
@override
Future<void> stop() async {
if (_textureId == null) {
return;
}
_textureId = null;
_pausing = false;
await methodChannel.invokeMethod<void>('stop');
}
@override
Future<void> pause() async {
if (_pausing) {
return;
}
_pausing = true;
await methodChannel.invokeMethod<void>('pause');
}
@override
Future<void> toggleTorch() async {
await methodChannel.invokeMethod<void>('toggleTorch');
}
@override
Future<void> updateScanWindow(Rect? window) async {
if (_textureId == null) {
return;
}
List<double>? points;
if (window != null) {
points = [window.left, window.top, window.right, window.bottom];
}
await methodChannel.invokeMethod<void>(
'updateScanWindow',
{'rect': points},
);
}
@override
Future<void> dispose() async {
await stop();
}
}