mobile_scanner_web_plugin.dart
5.75 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
import 'dart:async';
import 'dart:html' as html;
import 'dart:ui_web' as ui;
import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:mobile_scanner/mobile_scanner_web.dart';
import 'package:mobile_scanner/src/enums/barcode_format.dart';
import 'package:mobile_scanner/src/enums/camera_facing.dart';
/// This plugin is the web implementation of mobile_scanner.
/// It only supports QR codes.
class MobileScannerWebPlugin {
static void registerWith(Registrar registrar) {
final PluginEventChannel event = PluginEventChannel(
'dev.steenbakker.mobile_scanner/scanner/event',
const StandardMethodCodec(),
registrar,
);
final MethodChannel channel = MethodChannel(
'dev.steenbakker.mobile_scanner/scanner/method',
const StandardMethodCodec(),
registrar,
);
final MobileScannerWebPlugin instance = MobileScannerWebPlugin();
channel.setMethodCallHandler(instance.handleMethodCall);
event.setController(instance.controller);
}
// Controller to send events back to the framework
StreamController controller = StreamController.broadcast();
// ID of the video feed
String viewID = 'WebScanner-${DateTime.now().millisecondsSinceEpoch}';
static final html.DivElement vidDiv = html.DivElement();
/// Represents barcode reader library.
/// Change this property if you want to use a custom implementation.
///
/// Example of using the jsQR library:
/// void main() {
/// if (kIsWeb) {
/// MobileScannerWebPlugin.barCodeReader =
/// JsQrCodeReader(videoContainer: MobileScannerWebPlugin.vidDiv);
/// }
/// runApp(const MaterialApp(home: MyHome()));
/// }
static WebBarcodeReaderBase barCodeReader =
ZXingBarcodeReader(videoContainer: vidDiv);
StreamSubscription? _barCodeStreamSubscription;
/// Handle incomming messages
Future<dynamic> handleMethodCall(MethodCall call) async {
switch (call.method) {
case 'start':
return _start(call.arguments as Map);
case 'torch':
return _torch(call.arguments);
case 'stop':
return cancel();
case 'updateScanWindow':
return Future<void>.value();
default:
throw PlatformException(
code: 'Unimplemented',
details: "The mobile_scanner plugin for web doesn't implement "
"the method '${call.method}'",
);
}
}
/// Can enable or disable the flash if available
Future<void> _torch(arguments) async {
barCodeReader.toggleTorch(enabled: arguments == 1);
}
/// Starts the video stream and the scanner
Future<Map> _start(Map arguments) async {
var cameraFacing = CameraFacing.front;
if (arguments.containsKey('facing')) {
cameraFacing = CameraFacing.values[arguments['facing'] as int];
}
ui.platformViewRegistry.registerViewFactory(
viewID,
(int id) {
return vidDiv
..style.width = '100%'
..style.height = '100%';
},
);
// Check if stream is running
if (barCodeReader.isStarted) {
final hasTorch = await barCodeReader.hasTorch();
return {
'ViewID': viewID,
'videoWidth': barCodeReader.videoWidth,
'videoHeight': barCodeReader.videoHeight,
'torchable': hasTorch,
};
}
try {
List<BarcodeFormat>? formats;
if (arguments.containsKey('formats')) {
formats = (arguments['formats'] as List)
.cast<int>()
.map(BarcodeFormat.fromRawValue)
.toList();
}
final Duration? detectionTimeout;
if (arguments.containsKey('timeout')) {
detectionTimeout = Duration(milliseconds: arguments['timeout'] as int);
} else {
detectionTimeout = null;
}
await barCodeReader.start(
cameraFacing: cameraFacing,
formats: formats,
detectionTimeout: detectionTimeout,
);
_barCodeStreamSubscription =
barCodeReader.detectBarcodeContinuously().listen((code) {
if (code != null) {
controller.add({
'name': 'barcodeWeb',
'data': {
'rawValue': code.rawValue,
'rawBytes': code.rawBytes,
'format': code.format.rawValue,
'displayValue': code.displayValue,
'type': code.type.rawValue,
if (code.corners.isNotEmpty)
'corners': code.corners
.map(
(Offset c) => <Object?, Object?>{'x': c.dx, 'y': c.dy},
)
.toList(),
},
});
}
});
final hasTorch = await barCodeReader.hasTorch();
final bool? enableTorch = arguments['torch'] as bool?;
if (hasTorch && enableTorch != null) {
await barCodeReader.toggleTorch(enabled: enableTorch);
}
return {
'ViewID': viewID,
'videoWidth': barCodeReader.videoWidth,
'videoHeight': barCodeReader.videoHeight,
'torchable': hasTorch,
};
} catch (e, stackTrace) {
throw PlatformException(
code: 'MobileScannerWeb',
message: '$e',
details: stackTrace.toString(),
);
}
}
/// Check if any camera's are available
static Future<bool> cameraAvailable() async {
final sources =
await html.window.navigator.mediaDevices!.enumerateDevices();
for (final e in sources) {
// TODO:
// ignore: avoid_dynamic_calls
if (e.kind == 'videoinput') {
return true;
}
}
return false;
}
/// Stops the video feed and analyzer
Future<void> cancel() async {
await barCodeReader.stop();
await barCodeReader.stopDetectBarcodeContinuously();
await _barCodeStreamSubscription?.cancel();
_barCodeStreamSubscription = null;
}
}