-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathfast_qr_reader_view.dart
454 lines (399 loc) · 12.8 KB
/
fast_qr_reader_view.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
const MethodChannel _channel = const MethodChannel('fast_qr_reader_view');
enum CameraLensDirection { front, back, external }
enum ResolutionPreset { low, medium, high }
enum CodeFormat { codabar, code39, code93, code128, ean8, ean13, itf, upca, upce, aztec, datamatrix, pdf417, qr }
var _availableFormats = {
CodeFormat.codabar: 'codabar', // Android only
CodeFormat.code39: 'code39',
CodeFormat.code93: 'code93',
CodeFormat.code128: 'code128',
CodeFormat.ean8: 'ean8',
CodeFormat.ean13: 'ean13',
CodeFormat.itf: 'itf', // itf-14 on iOS, should be changed to Interleaved2of5?
CodeFormat.upca: 'upca', // Android only
CodeFormat.upce: 'upce',
CodeFormat.aztec: 'aztec',
CodeFormat.datamatrix: 'datamatrix',
CodeFormat.pdf417: 'pdf417',
CodeFormat.qr: 'qr',
};
/// Returns the resolution preset as a String.
String serializeResolutionPreset(ResolutionPreset resolutionPreset) {
switch (resolutionPreset) {
case ResolutionPreset.high:
return 'high';
case ResolutionPreset.medium:
return 'medium';
case ResolutionPreset.low:
return 'low';
}
}
List<String?> serializeCodeFormatsList(List<CodeFormat> formats) {
List<String?> list = [];
for (var i = 0; i < formats.length; i++) {
if (_availableFormats[formats[i]] != null) {
// this format exists in my list of available formats
list.add(_availableFormats[formats[i]]);
}
}
return list;
}
CameraLensDirection _parseCameraLensDirection(String? string) {
switch (string) {
case 'front':
return CameraLensDirection.front;
case 'back':
return CameraLensDirection.back;
case 'external':
return CameraLensDirection.external;
}
throw new ArgumentError('Unknown CameraLensDirection value');
}
/// Completes with a list of available cameras.
///
/// May throw a [QRReaderException].
Future<List<CameraDescription>> availableCameras() async {
try {
await _channel.invokeMethod('init');
final List<dynamic> cameras = await (_channel.invokeMethod('availableCameras'));
return cameras.map((dynamic camera) {
return new CameraDescription(
name: camera['name'],
lensDirection: _parseCameraLensDirection(camera['lensFacing']),
);
}).toList();
} on PlatformException catch (e) {
print(e);
throw new QRReaderException(e.code, e.message);
}
}
/// Checks the current status of the Camera Permission
///
/// returns: [Future<PermissionStatus>] with the status from the check
Future<PermissionStatus> checkCameraPermission() async {
try {
print("checkCameraPermission()");
var permission = await _channel.invokeMethod('checkPermission') as String?;
print("Permission: $permission");
return _getPermissionStatus(permission);
} on PlatformException catch (e) {
print("Error while permissions: $e");
return Future.value(PermissionStatus.unknown);
}
}
/// Requests the camera permission
///
/// returns: [Future<PermissionStatus>] with the status from the request
Future<PermissionStatus> requestCameraPermission() async {
try {
var result = await _channel.invokeMethod('requestPermission');
switch (result) {
case "denied":
return PermissionStatus.denied;
case "dismissedForever":
return PermissionStatus.dismissedForever;
case "granted":
return PermissionStatus.granted;
default:
return PermissionStatus.unknown;
}
} on PlatformException catch (e) {
print("Error while permissions: $e");
return Future.value(PermissionStatus.unknown);
}
}
/// Gets the PermissionStatus from the channel Method
///
/// Given a [String] status from the method channel, it returns a
/// [PermissionStatus]
PermissionStatus _getPermissionStatus(String? status) {
switch (status) {
case "denied":
return PermissionStatus.denied;
case "dismissedForever":
return PermissionStatus.dismissedForever;
case "granted":
return PermissionStatus.granted;
case "restricted":
return PermissionStatus.restricted;
default:
return PermissionStatus.unknown;
}
}
/// Opens the native settings screen
///
/// Opens the native iOS or Android settings screens for the current app,
/// So that the user can give the app permission even if he has denied them
Future<void> openSettings() {
try {
return _channel.invokeMethod('settings');
} on PlatformException catch (e) {
return Future.error(e);
}
}
/// Enum to give us the status of the Permission request/check
enum PermissionStatus {
granted,
/// Permission to access the requested feature is denied by the user.
denied,
/// The feature is disabled (or not available) on the device.
disabled,
/// Permission to access the requested feature is granted by the user.
dismissedForever,
/// The user granted restricted access to the requested feature (only on iOS).
restricted,
/// Permission is in an unknown state
unknown
}
class CameraDescription {
final String? name;
final CameraLensDirection? lensDirection;
CameraDescription({this.name, this.lensDirection});
@override
bool operator ==(Object o) {
return o is CameraDescription && o.name == name && o.lensDirection == lensDirection;
}
@override
int get hashCode {
return hashValues(name, lensDirection);
}
@override
String toString() {
return '$runtimeType($name, $lensDirection)';
}
}
/// This is thrown when the plugin reports an error.
class QRReaderException implements Exception {
String code;
String? description;
QRReaderException(this.code, this.description);
@override
String toString() => '$runtimeType($code, $description)';
}
// Build the UI texture view of the video data with textureId.
class QRReaderPreview extends StatelessWidget {
final QRReaderController controller;
const QRReaderPreview(this.controller);
@override
Widget build(BuildContext context) {
return controller.value.isInitialized! ? new Texture(textureId: controller._textureId!) : new Container();
}
}
/// The state of a [QRReaderController].
class QRReaderValue {
/// True after [QRReaderController.initialize] has completed successfully.
final bool? isInitialized;
/// True when the camera is scanning.
final bool? isScanning;
final String? errorDescription;
/// The size of the preview in pixels.
///
/// Is `null` until [isInitialized] is `true`.
final Size? previewSize;
const QRReaderValue({
this.isInitialized,
this.errorDescription,
this.previewSize,
this.isScanning,
});
const QRReaderValue.uninitialized()
: this(
isInitialized: false,
isScanning: false,
);
/// Convenience getter for `previewSize.height / previewSize.width`.
///
/// Can only be called when [initialize] is done.
double get aspectRatio => previewSize!.height / previewSize!.width;
bool get hasError => errorDescription != null;
QRReaderValue copyWith({
bool? isInitialized,
bool? isScanning,
String? errorDescription,
Size? previewSize,
}) {
return new QRReaderValue(
isInitialized: isInitialized ?? this.isInitialized,
errorDescription: errorDescription,
previewSize: previewSize ?? this.previewSize,
isScanning: isScanning ?? this.isScanning,
);
}
@override
String toString() {
return '$runtimeType('
'isScanning: $isScanning, '
'isInitialized: $isInitialized, '
'errorDescription: $errorDescription, '
'previewSize: $previewSize)';
}
}
/// Controls a QR Reader
///
/// Use [availableCameras] to get a list of available cameras.
///
/// Before using a [QRReaderController] a call to [initialize] must complete.
///
/// To show the camera preview on the screen use a [QRReaderPreview] widget.
class QRReaderController extends ValueNotifier<QRReaderValue> {
final CameraDescription description;
final ResolutionPreset resolutionPreset;
final Function onCodeRead;
final List<CodeFormat> codeFormats;
int? _textureId;
bool _isDisposed = false;
StreamSubscription<dynamic>? _eventSubscription;
Completer<Null>? _creatingCompleter;
QRReaderController(this.description, this.resolutionPreset, this.codeFormats, this.onCodeRead)
: super(const QRReaderValue.uninitialized());
/// Initializes the camera on the device.
///
/// Throws a [QRReaderException] if the initialization fails.
Future<Null> initialize() async {
if (_isDisposed) {
return new Future<Null>.value(null);
}
try {
_channel.setMethodCallHandler(_handleMethod);
_creatingCompleter = new Completer<Null>();
final Map<dynamic, dynamic> reply = await (_channel.invokeMethod(
'initialize',
<String, dynamic>{
'cameraName': description.name,
'resolutionPreset': serializeResolutionPreset(resolutionPreset),
'codeFormats': serializeCodeFormatsList(codeFormats),
},
));
_textureId = reply['textureId'];
value = value.copyWith(
isInitialized: true,
previewSize: new Size(
reply['previewWidth'].toDouble(),
reply['previewHeight'].toDouble(),
),
);
} on PlatformException catch (e) {
throw new QRReaderException(e.code, e.message);
}
_eventSubscription =
new EventChannel('fast_qr_reader_view/cameraEvents$_textureId').receiveBroadcastStream().listen(_listener);
_creatingCompleter!.complete(null);
return _creatingCompleter!.future;
}
/// Listen to events from the native plugins.
///
/// A "cameraClosing" event is sent when the camera is closed automatically by the system (for example when the app go to background). The plugin will try to reopen the camera automatically but any ongoing recording will end.
void _listener(dynamic event) {
final Map<dynamic, dynamic>? map = event;
if (_isDisposed) {
return;
}
switch (map!['eventType']) {
case 'error':
value = value.copyWith(errorDescription: event['errorDescription']);
break;
case 'cameraClosing':
value = value.copyWith(isScanning: false);
break;
}
}
/// Toggle flashlight
Future<Null> toggleFlash() async {
try {
await _channel.invokeMethod(
'toggleFlash',
<String, dynamic>{'textureId': _textureId},
);
} on PlatformException catch (e) {
throw new QRReaderException(e.code, e.message);
}
}
/// Start a QR scan.
///
/// Throws a [QRReaderException] if the capture fails.
Future<Null> startScanning() async {
if (!value.isInitialized! || _isDisposed) {
throw new QRReaderException(
'Uninitialized QRReaderController',
'startScanning was called on uninitialized QRReaderController',
);
}
if (value.isScanning!) {
throw new QRReaderException(
'A scan has already started.',
'startScanning was called when a recording is already started.',
);
}
try {
value = value.copyWith(isScanning: true);
await _channel.invokeMethod(
'startScanning',
<String, dynamic>{'textureId': _textureId},
);
} on PlatformException catch (e) {
throw new QRReaderException(e.code, e.message);
}
}
/// Stop scanning.
Future<Null> stopScanning() async {
if (!value.isInitialized! || _isDisposed) {
throw new QRReaderException(
'Uninitialized QRReaderController',
'stopScanning was called on uninitialized QRReaderController',
);
}
if (!value.isScanning!) {
throw new QRReaderException(
'No scanning is happening',
'stopScanning was called when the scanner was not scanning.',
);
}
try {
value = value.copyWith(isScanning: false);
await _channel.invokeMethod(
'stopScanning',
<String, dynamic>{'textureId': _textureId},
);
} on PlatformException catch (e) {
throw new QRReaderException(e.code, e.message);
}
}
/// Releases the resources of this camera.
@override
Future<Null> dispose() async {
if (_isDisposed) {
return new Future<Null>.value(null);
}
_isDisposed = true;
super.dispose();
if (_creatingCompleter == null) {
return new Future<Null>.value(null);
} else {
return _creatingCompleter!.future.then((_) async {
await _channel.invokeMethod(
'dispose',
<String, dynamic>{'textureId': _textureId},
);
await _eventSubscription?.cancel();
});
}
}
Future<dynamic> _handleMethod(MethodCall call) async {
switch (call.method) {
case "updateCode":
if (value.isScanning!) {
onCodeRead(call.arguments);
value = value.copyWith(isScanning: false);
}
}
}
}
class FastQrReaderView {
static Future<String?> get platformVersion async {
final String? version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
}