zxing_barcode_reader.dart
6.04 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
import 'dart:async';
import 'dart:js_interop';
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:mobile_scanner/src/enums/barcode_format.dart';
import 'package:mobile_scanner/src/mobile_scanner_exception.dart';
import 'package:mobile_scanner/src/objects/barcode_capture.dart';
import 'package:mobile_scanner/src/objects/start_options.dart';
import 'package:mobile_scanner/src/web/barcode_reader.dart';
import 'package:mobile_scanner/src/web/javascript_map.dart';
import 'package:mobile_scanner/src/web/media_track_constraints_delegate.dart';
import 'package:mobile_scanner/src/web/zxing/result.dart';
import 'package:mobile_scanner/src/web/zxing/zxing_browser_multi_format_reader.dart';
import 'package:mobile_scanner/src/web/zxing/zxing_exception.dart';
import 'package:web/web.dart' as web;
/// A barcode reader implementation that uses the ZXing library.
final class ZXingBarcodeReader extends BarcodeReader {
ZXingBarcodeReader();
/// ZXing reports an error with this message if the code could not be detected.
@visibleForTesting
static const String kNoCodeDetectedErrorMessage =
'No MultiFormat Readers were able to detect the code.';
/// The listener for media track settings changes.
void Function(web.MediaTrackSettings)? _onMediaTrackSettingsChanged;
/// The internal media stream track constraints delegate.
final MediaTrackConstraintsDelegate _mediaTrackConstraintsDelegate =
const MediaTrackConstraintsDelegate();
/// The internal barcode reader.
ZXingBrowserMultiFormatReader? _reader;
@override
bool get isScanning => _reader?.stream != null;
@override
Size get videoSize {
final web.HTMLVideoElement? videoElement = _reader?.videoElement;
if (videoElement == null) {
return Size.zero;
}
return Size(
videoElement.videoWidth.toDouble(),
videoElement.videoHeight.toDouble(),
);
}
@override
String get scriptUrl => 'https://unpkg.com/@zxing/library@0.19.1';
JSMap? _createReaderHints(List<BarcodeFormat> formats) {
if (formats.isEmpty || formats.contains(BarcodeFormat.all)) {
return null;
}
final JSMap hints = JSMap();
// Set the formats hint.
// See https://github.com/zxing-js/library/blob/master/src/core/DecodeHintType.ts#L45
hints.set(
2.toJS,
[
for (final BarcodeFormat format in formats) format.toJS,
].toJS,
);
return hints;
}
/// Prepare the video element for the barcode reader.
///
/// The given [videoElement] is assumed to already be attached to the DOM at this point.
/// The camera video output is then attached to both the barcode reader (to detect barcodes),
/// and the video element (to display the camera output).
Future<void> _prepareVideoElement(
web.HTMLVideoElement videoElement,
web.MediaStream videoStream,
) async {
final JSPromise? result = _reader?.attachStreamToVideo.callAsFunction(
_reader,
videoStream,
videoElement,
) as JSPromise?;
await result?.toDart;
final web.MediaTrackSettings? settings =
_mediaTrackConstraintsDelegate.getSettings(videoStream);
if (settings != null) {
_onMediaTrackSettingsChanged?.call(settings);
}
}
@override
Stream<BarcodeCapture> detectBarcodes() {
final controller = StreamController<BarcodeCapture>();
controller.onListen = () {
_reader?.decodeContinuously.callAsFunction(
_reader,
_reader?.videoElement,
(Result? result, ZXingException? error) {
if (controller.isClosed) {
return;
}
// Skip the event if no code was detected.
if (error != null && error.message != kNoCodeDetectedErrorMessage) {
controller.addError(MobileScannerBarcodeException(error.message));
return;
}
if (result != null) {
controller.add(
BarcodeCapture(
barcodes: [result.toBarcode],
),
);
}
}.toJS,
);
};
// The onCancel() method of the controller is called
// when the stream subscription returned by this method is cancelled in `MobileScannerWeb.stop()`.
// This avoids both leaving the barcode scanner running and a memory leak for the stream subscription.
controller.onCancel = () async {
_reader?.stopContinuousDecode.callAsFunction(_reader);
_reader?.reset.callAsFunction(_reader);
await controller.close();
};
return controller.stream;
}
@override
void setMediaTrackSettingsListener(
void Function(web.MediaTrackSettings) listener,
) {
_onMediaTrackSettingsChanged ??= listener;
}
@override
Future<void> start(
StartOptions options, {
required web.HTMLVideoElement videoElement,
required web.MediaStream videoStream,
}) async {
final int detectionTimeoutMs = options.detectionTimeoutMs;
final List<BarcodeFormat> formats = [
for (final BarcodeFormat format in options.formats)
if (format != BarcodeFormat.unknown) format,
];
_reader = ZXingBrowserMultiFormatReader(
_createReaderHints(formats),
detectionTimeoutMs,
);
await _prepareVideoElement(videoElement, videoStream);
}
@override
Future<void> stop() async {
_onMediaTrackSettingsChanged = null;
_reader?.stopContinuousDecode.callAsFunction(_reader);
_reader?.reset.callAsFunction(_reader);
_reader = null;
}
}
extension on BarcodeFormat {
/// Get the barcode format from the ZXing library.
JSNumber get toJS {
final int zxingFormat = switch (this) {
BarcodeFormat.aztec => 0,
BarcodeFormat.codabar => 1,
BarcodeFormat.code39 => 2,
BarcodeFormat.code93 => 3,
BarcodeFormat.code128 => 4,
BarcodeFormat.dataMatrix => 5,
BarcodeFormat.ean8 => 6,
BarcodeFormat.ean13 => 7,
BarcodeFormat.itf => 8,
BarcodeFormat.pdf417 => 10,
BarcodeFormat.qrCode => 11,
BarcodeFormat.upcA => 14,
BarcodeFormat.upcE => 15,
BarcodeFormat.unknown || BarcodeFormat.all || _ => -1,
};
return zxingFormat.toJS;
}
}