-
Notifications
You must be signed in to change notification settings - Fork 11
/
camcode_web.dart
315 lines (276 loc) · 8.79 KB
/
camcode_web.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
import 'dart:async';
/// In order to *not* need this ignore, consider extracting the "web" version
/// of your plugin as a separate package, instead of inlining it in the same
/// package as the core of your plugin.
/// ignore: avoid_web_libraries_in_flutter
import 'dart:html';
import 'dart:js';
import 'package:camcode/barcode_results.dart';
import 'package:camcode/dart_ui_stub/dart_ui.dart' as ui;
import 'package:camcode/barcode.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
/// A web implementation of the Camcode plugin.
class CamcodeWeb {
/// VideoElement used to display the camera image
late VideoElement _webcamVideoElement;
/// ImageElement used to display taken pictures
late ImageElement imageElement;
/// timer shceduling the pictures treatment process
late Timer _timer;
/// used to transmit result to the Widget via MethodChannel
late Completer<String> completer;
/// bacode results container
final BarcodeResults _barcodeResults = BarcodeResults();
/// Canvas element used to draw the barcode result
late CanvasElement _canvasElement;
/// list of every cameras
final Map<String, String> _cameraDevices = {};
/// ID of the selected device
String? _selectedDeviceId;
/// Completer to get enumerateDevices result
late Completer<List<String>> _enumerateDevicesCompleter;
int frameSize = 1;
int counter = 0;
int maxAttempt = 4;
/// Registering method
static void registerWith(Registrar registrar) {
final channel = MethodChannel(
'camcode',
const StandardMethodCodec(),
// ignore: unnecessary_cast
registrar as BinaryMessenger,
);
final pluginInstance = CamcodeWeb();
channel.setMethodCallHandler(pluginInstance.handleMethodCall);
}
/// handle channel calls
Future<dynamic> handleMethodCall(MethodCall call) async {
switch (call.method) {
case 'initialize':
final List arguments = call.arguments;
return initialize(
arguments[0],
arguments[1],
arguments[2],
arguments[3],
);
case 'releaseResources':
return releaseResources();
case 'fetchResult':
return fetchResult();
case 'fetchDeviceList':
return fetchDevices();
case 'selectDevice':
return _selectDevice(call.arguments);
default:
throw PlatformException(
code: 'Unimplemented',
details: 'camcode for web doesn\'t implement \'${call.method}\'',
);
}
}
/// wait for the result to be completed
Future<String> fetchResult() {
return completer.future;
}
/// wait for the list of devices to be completed
Future<List<String>> fetchDevices() {
_enumerateVideoDevices();
return _enumerateDevicesCompleter.future;
}
/// Initialize the scanner :
/// - request user permission
/// - request camera stream
/// - initialize video
/// - start video streaming
/// - start picture snapshot timer scheduling
int initialize(
double width,
double height,
int refreshDelayMillis,
int minimalResultCount,
) {
completer = Completer<String>();
_enumerateDevicesCompleter = Completer<List<String>>();
_barcodeResults.singleShot = minimalResultCount == 1;
_barcodeResults.minimalResultCount = minimalResultCount;
_barcodeResults.clear();
// Create a video element which will be provided with stream source
_webcamVideoElement = VideoElement()
..width = 1920
..height = 1080
..style.width = '100%'
..style.height = '100%'
..style.objectFit = 'contain'
..autoplay = true
..muted = true;
_webcamVideoElement.setAttribute('playsinline', 'true');
imageElement = ImageElement()
..width = 1920
..height = 1080
..style.width = '100%'
..style.height = '100%';
_canvasElement = CanvasElement(
width: 1920,
height: 1080,
);
final time = DateTime.now().microsecondsSinceEpoch;
// ignore: undefined_prefixed_name
ui.platformViewRegistry.registerViewFactory(
'webcamVideoElement$time',
(int viewId) => _webcamVideoElement,
);
// ignore: undefined_prefixed_name
ui.platformViewRegistry.registerViewFactory(
'imageElement',
(int viewId) => imageElement,
);
// Access the webcam stream
_setupMediaStream(width, height);
Future.delayed(Duration(seconds: 1), () {
_scan(refreshDelayMillis);
});
return time;
}
/// Fetch media device stream and affect it to the video element
void _setupMediaStream(double width, double height) {
if (window.location.protocol.contains('https')) {
var options = _configureOptions(width, height);
window.navigator.mediaDevices
?.getUserMedia(options)
.then((MediaStream stream) {
_webcamVideoElement.srcObject = stream;
});
} else {
window.navigator.getUserMedia(video: true).then((MediaStream stream) {
_webcamVideoElement.srcObject = stream;
});
}
}
/// configure constraint options to fetch media device stream
dynamic _configureOptions(double width, double height) {
var options;
if (window.navigator.userAgent.contains('Mobi')) {
options = {
'audio': false,
'video': {
'deviceId':
_selectedDeviceId != null ? {'exact': _selectedDeviceId} : null,
'facingMode': {'exact': 'environment'},
}
};
} else {
options = {
'audio': false,
'video': {
'deviceId':
_selectedDeviceId != null ? {'exact': _selectedDeviceId} : null,
}
};
}
return options;
}
/// Selects a device with the given label
void _selectDevice(String? deviceLabel) {
_selectedDeviceId = _cameraDevices[deviceLabel];
_setupMediaStream(
_webcamVideoElement.width.toDouble(),
_webcamVideoElement.height.toDouble(),
);
}
/// Enumerate all video devices
void _enumerateVideoDevices() {
window.navigator.mediaDevices?.enumerateDevices().then((devices) {
for (final device in devices) {
if (device.kind == 'videoinput') {
_cameraDevices[device.label] = device.deviceId;
}
}
_enumerateDevicesCompleter.complete(_cameraDevices.keys.toList());
});
}
/// Scan loop
Future<void> _scan(int refreshDelayMillis) async {
counter = 0;
_timer = Timer.periodic(
Duration(
milliseconds: refreshDelayMillis,
),
(timer) {
_takePicture();
},
);
}
/// Takes a picture of the current camera image
/// and process it for barcode identification
Future<void> _takePicture() async {
final context = _canvasElement.context2D;
// context.filter = 'grayscale(1)';
switch (frameSize) {
case 1:
context.drawImageScaledFromSource(
_webcamVideoElement,
_webcamVideoElement.videoWidth * .2,
_webcamVideoElement.videoHeight * .4,
_webcamVideoElement.videoWidth * .6,
_webcamVideoElement.videoHeight * .2,
0,
0,
_webcamVideoElement.width,
_webcamVideoElement.height,
);
break;
default:
context.drawImageScaledFromSource(
_webcamVideoElement,
0,
_webcamVideoElement.videoHeight * .4,
_webcamVideoElement.videoWidth,
_webcamVideoElement.videoHeight * .2,
0,
0,
_webcamVideoElement.width,
_webcamVideoElement.height,
);
break;
}
debugPrint(
'Webcam taille : ${_webcamVideoElement.height} & ${_webcamVideoElement.width}');
debugPrint(
'Webcam video taille : ${_webcamVideoElement.videoHeight} & ${_webcamVideoElement.videoWidth}');
final dataUrl = _canvasElement.toDataUrl('image/jpeg', 1.0);
imageElement.src = dataUrl;
detectBarcode(dataUrl, allowInterop((result) => onBarcodeResult(result)));
counter++;
if (counter >= maxAttempt) {
counter = 0;
frameSize = frameSize == 0 ? 1 : 0;
}
}
/// Method called on barcode result to finish the process and send result
Future<void> onBarcodeResult(String _barcode) async {
_barcodeResults.add(_barcode);
if (_barcodeResults.gotResult) {
releaseResources();
if (!completer.isCompleted) {
completer.complete(_barcodeResults.mostFrequentBarcode);
_barcodeResults.clear();
}
}
}
/// Release resources to avoid leaks
void releaseResources() {
_timer.cancel();
_webcamVideoElement.pause();
_webcamVideoElement.srcObject?.getTracks().forEach((track) {
track.stop();
track.enabled = false;
});
_webcamVideoElement.srcObject = null;
_canvasElement.remove();
_webcamVideoElement.remove();
imageElement.remove();
}
}