mobile_scanner_controller.dart
9.36 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
303
304
305
306
307
308
309
310
311
import 'dart:async';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:mobile_scanner/src/barcode_utility.dart';
import 'package:mobile_scanner/src/mobile_scanner_exception.dart';
/// The [MobileScannerController] holds all the logic of this plugin,
/// where as the [MobileScanner] class is the frontend of this plugin.
class MobileScannerController {
MobileScannerController({
this.facing = CameraFacing.back,
this.detectionSpeed = DetectionSpeed.normal,
this.detectionTimeoutMs = 250,
this.torchEnabled = false,
this.formats,
this.returnImage = false,
this.onPermissionSet,
}) {
// In case a new instance is created before calling dispose()
if (controllerHashcode != null) {
stop();
}
controllerHashcode = hashCode;
events = _eventChannel
.receiveBroadcastStream()
.listen((data) => _handleEvent(data as Map));
}
//Must be static to keep the same value on new instances
static int? controllerHashcode;
/// Select which camera should be used.
///
/// Default: CameraFacing.back
final CameraFacing facing;
/// Enable or disable the torch (Flash) on start
///
/// Default: disabled
final bool torchEnabled;
/// Set to true if you want to return the image buffer with the Barcode event
///
/// Only supported on iOS and Android
final bool returnImage;
/// If provided, the scanner will only detect those specific formats
final List<BarcodeFormat>? formats;
/// Sets the speed of detections.
///
/// WARNING: DetectionSpeed.unrestricted can cause memory issues on some devices
final DetectionSpeed detectionSpeed;
final int detectionTimeoutMs;
/// Sets the barcode stream
final StreamController<BarcodeCapture> _barcodesController =
StreamController.broadcast();
Stream<BarcodeCapture> get barcodes => _barcodesController.stream;
static const MethodChannel _methodChannel =
MethodChannel('dev.steenbakker.mobile_scanner/scanner/method');
static const EventChannel _eventChannel =
EventChannel('dev.steenbakker.mobile_scanner/scanner/event');
Function(bool permissionGranted)? onPermissionSet;
/// Listen to events from the platform specific code
late StreamSubscription events;
/// A notifier that provides several arguments about the MobileScanner
final ValueNotifier<MobileScannerArguments?> startArguments =
ValueNotifier(null);
/// A notifier that provides the state of the Torch (Flash)
final ValueNotifier<TorchState> torchState = ValueNotifier(TorchState.off);
/// A notifier that provides the state of which camera is being used
late final ValueNotifier<CameraFacing> cameraFacingState =
ValueNotifier(facing);
bool isStarting = false;
bool? _hasTorch;
/// Set the starting arguments for the camera
Map<String, dynamic> _argumentsToMap({CameraFacing? cameraFacingOverride}) {
final Map<String, dynamic> arguments = {};
cameraFacingState.value = cameraFacingOverride ?? facing;
arguments['facing'] = cameraFacingState.value.index;
arguments['torch'] = torchEnabled;
arguments['speed'] = detectionSpeed.index;
arguments['timeout'] = detectionTimeoutMs;
if (formats != null) {
if (Platform.isAndroid) {
arguments['formats'] = formats!.map((e) => e.index).toList();
} else if (Platform.isIOS || Platform.isMacOS) {
arguments['formats'] = formats!.map((e) => e.rawValue).toList();
}
}
arguments['returnImage'] = true;
return arguments;
}
/// Start barcode scanning. This will first check if the required permissions
/// are set.
Future<MobileScannerArguments?> start({
CameraFacing? cameraFacingOverride,
}) async {
debugPrint('Hashcode controller: $hashCode');
if (isStarting) {
debugPrint("Called start() while starting.");
}
isStarting = true;
// Check authorization status
if (!kIsWeb) {
final MobileScannerState state = MobileScannerState
.values[await _methodChannel.invokeMethod('state') as int? ?? 0];
switch (state) {
case MobileScannerState.undetermined:
final bool result =
await _methodChannel.invokeMethod('request') as bool? ?? false;
if (!result) {
isStarting = false;
onPermissionSet?.call(result);
throw MobileScannerException('User declined camera permission.');
}
break;
case MobileScannerState.denied:
isStarting = false;
onPermissionSet?.call(false);
throw MobileScannerException('User declined camera permission.');
case MobileScannerState.authorized:
onPermissionSet?.call(true);
break;
}
}
// Start the camera with arguments
Map<String, dynamic>? startResult = {};
try {
startResult = await _methodChannel.invokeMapMethod<String, dynamic>(
'start',
_argumentsToMap(cameraFacingOverride: cameraFacingOverride),
);
} on PlatformException catch (error) {
debugPrint('${error.code}: ${error.message}');
isStarting = false;
if (error.code == "MobileScannerWeb") {
onPermissionSet?.call(false);
}
return null;
}
if (startResult == null) {
isStarting = false;
throw MobileScannerException(
'Failed to start mobileScanner, no response from platform side',
);
}
_hasTorch = startResult['torchable'] as bool? ?? false;
if (_hasTorch! && torchEnabled) {
torchState.value = TorchState.on;
}
if (kIsWeb) {
onPermissionSet?.call(
true,
); // If we reach this line, it means camera permission has been granted
startArguments.value = MobileScannerArguments(
webId: startResult['ViewID'] as String?,
size: Size(
startResult['videoWidth'] as double? ?? 0,
startResult['videoHeight'] as double? ?? 0,
),
hasTorch: _hasTorch!,
);
} else {
startArguments.value = MobileScannerArguments(
textureId: startResult['textureId'] as int?,
size: toSize(startResult['size'] as Map? ?? {}),
hasTorch: _hasTorch!,
);
}
isStarting = false;
return startArguments.value!;
}
/// Stops the camera, but does not dispose this controller.
Future<void> stop() async {
try {
await _methodChannel.invokeMethod('stop');
} catch (e) {
debugPrint('$e');
}
}
/// Switches the torch on or off.
///
/// Only works if torch is available.
Future<void> toggleTorch() async {
if (_hasTorch == null) {
throw MobileScannerException(
'Cannot toggle torch if start() has never been called',
);
} else if (!_hasTorch!) {
throw MobileScannerException('Device has no torch');
}
torchState.value =
torchState.value == TorchState.off ? TorchState.on : TorchState.off;
await _methodChannel.invokeMethod('torch', torchState.value.index);
}
/// Switches the torch on or off.
///
/// Only works if torch is available.
Future<void> switchCamera() async {
await _methodChannel.invokeMethod('stop');
final CameraFacing facingToUse =
cameraFacingState.value == CameraFacing.back
? CameraFacing.front
: CameraFacing.back;
await start(cameraFacingOverride: facingToUse);
}
/// Handles a local image file.
/// Returns true if a barcode or QR code is found.
/// Returns false if nothing is found.
///
/// [path] The path of the image on the devices
Future<bool> analyzeImage(String path) async {
return _methodChannel
.invokeMethod<bool>('analyzeImage', path)
.then<bool>((bool? value) => value ?? false);
}
/// Disposes the MobileScannerController and closes all listeners.
///
/// If you call this, you cannot use this controller object anymore.
void dispose() {
stop();
events.cancel();
_barcodesController.close();
if (hashCode == controllerHashcode) {
controllerHashcode = null;
onPermissionSet = null;
}
}
/// Handles a returning event from the platform side
void _handleEvent(Map event) {
final name = event['name'];
final data = event['data'];
switch (name) {
case 'torchState':
final state = TorchState.values[data as int? ?? 0];
torchState.value = state;
break;
case 'barcode':
if (data == null) return;
final parsed = (data as List)
.map((value) => Barcode.fromNative(value as Map))
.toList();
_barcodesController.add(
BarcodeCapture(
barcodes: parsed,
image: event['image'] as Uint8List?,
),
);
break;
case 'barcodeMac':
_barcodesController.add(
BarcodeCapture(
barcodes: [
Barcode(
rawValue: (data as Map)['payload'] as String?,
)
],
),
);
break;
case 'barcodeWeb':
_barcodesController.add(
BarcodeCapture(
barcodes: [
Barcode(
rawValue: data as String?,
)
],
),
);
break;
case 'error':
throw MobileScannerException(data as String);
default:
throw UnimplementedError(name as String?);
}
}
}