mobile_scanner_method_channel.dart
8.71 KB
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
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 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;
/// 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.macOS) {
return BarcodeCapture(
raw: event,
barcodes: barcodes
.map(
(barcode) => Barcode(
rawValue: barcode['payload'] as String?,
format: BarcodeFormat.fromRawValue(
barcode['symbology'] as int? ?? -1,
),
),
)
.toList(),
);
}
if (defaultTargetPlatform == TargetPlatform.android ||
defaultTargetPlatform == TargetPlatform.iOS) {
final double? width = event['width'] as double?;
final double? height = event['height'] as double?;
return BarcodeCapture(
raw: data,
barcodes: barcodes.map(Barcode.fromNative).toList(),
image: event['image'] as Uint8List?,
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.',
),
);
}
/// 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 {
return eventsStream
.where((event) => event['name'] == 'barcode')
.map((event) => _parseBarcode(event));
}
@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) async {
final Map<String, Object?>? result =
await methodChannel.invokeMapMethod<String, Object?>(
'analyzeImage',
path,
);
return _parseBarcode(result);
}
@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 (_textureId != null) {
throw const MobileScannerException(
errorCode: MobileScannerErrorCode.controllerAlreadyInitialized,
errorDetails: MobileScannerErrorDetails(
message:
'The scanner was already started. Call stop() before calling start() again.',
),
);
}
await _requestCameraPermission();
Map<String, Object?>? startResult;
try {
startResult = await methodChannel.invokeMapMethod<String, Object?>(
'start',
startOptions.toMap(),
);
} on PlatformException catch (error) {
throw MobileScannerException(
errorCode: MobileScannerErrorCode.genericError,
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 Map<Object?, Object?>? sizeInfo =
startResult['size'] as Map<Object?, Object?>?;
final double? width = sizeInfo?['width'] as double?;
final double? height = sizeInfo?['height'] as double?;
final Size size;
if (width == null || height == null) {
size = Size.zero;
} else {
size = Size(width, height);
}
return MobileScannerViewAttributes(
currentTorchMode: currentTorchState,
numberOfCameras: numberOfCameras,
size: size,
);
}
@override
Future<void> stop() async {
if (_textureId == null) {
return;
}
_textureId = null;
await methodChannel.invokeMethod<void>('stop');
}
@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();
}
}