adjust web barcode scanner constructor to only accept JS types; move hints creat…
…ion to separate method
Showing
2 changed files
with
28 additions
and
15 deletions
| @@ -9,6 +9,7 @@ import 'package:mobile_scanner/src/enums/torch_state.dart'; | @@ -9,6 +9,7 @@ import 'package:mobile_scanner/src/enums/torch_state.dart'; | ||
| 9 | import 'package:mobile_scanner/src/objects/barcode_capture.dart'; | 9 | import 'package:mobile_scanner/src/objects/barcode_capture.dart'; |
| 10 | import 'package:mobile_scanner/src/objects/start_options.dart'; | 10 | import 'package:mobile_scanner/src/objects/start_options.dart'; |
| 11 | import 'package:mobile_scanner/src/web/barcode_reader.dart'; | 11 | import 'package:mobile_scanner/src/web/barcode_reader.dart'; |
| 12 | +import 'package:mobile_scanner/src/web/javascript_map.dart'; | ||
| 12 | import 'package:mobile_scanner/src/web/media_track_constraints_delegate.dart'; | 13 | import 'package:mobile_scanner/src/web/media_track_constraints_delegate.dart'; |
| 13 | import 'package:mobile_scanner/src/web/zxing/result.dart'; | 14 | import 'package:mobile_scanner/src/web/zxing/result.dart'; |
| 14 | import 'package:mobile_scanner/src/web/zxing/zxing_browser_multi_format_reader.dart'; | 15 | import 'package:mobile_scanner/src/web/zxing/zxing_browser_multi_format_reader.dart'; |
| @@ -84,6 +85,26 @@ final class ZXingBarcodeReader extends BarcodeReader { | @@ -84,6 +85,26 @@ final class ZXingBarcodeReader extends BarcodeReader { | ||
| 84 | } | 85 | } |
| 85 | } | 86 | } |
| 86 | 87 | ||
| 88 | + JsMap? _createReaderHints(List<BarcodeFormat> formats) { | ||
| 89 | + if (formats.isEmpty || formats.contains(BarcodeFormat.all)) { | ||
| 90 | + return null; | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + final JsMap hints = JsMap(); | ||
| 94 | + | ||
| 95 | + // Set the formats hint. | ||
| 96 | + // See https://github.com/zxing-js/library/blob/master/src/core/DecodeHintType.ts#L45 | ||
| 97 | + hints.set( | ||
| 98 | + 2.toJS, | ||
| 99 | + [ | ||
| 100 | + for (final BarcodeFormat format in formats) | ||
| 101 | + getZXingBarcodeFormat(format).toJS, | ||
| 102 | + ].toJS, | ||
| 103 | + ); | ||
| 104 | + | ||
| 105 | + return hints; | ||
| 106 | + } | ||
| 107 | + | ||
| 87 | /// Prepare the [web.MediaStream] for the barcode reader video input. | 108 | /// Prepare the [web.MediaStream] for the barcode reader video input. |
| 88 | /// | 109 | /// |
| 89 | /// This method requests permission to use the camera. | 110 | /// This method requests permission to use the camera. |
| @@ -242,19 +263,10 @@ final class ZXingBarcodeReader extends BarcodeReader { | @@ -242,19 +263,10 @@ final class ZXingBarcodeReader extends BarcodeReader { | ||
| 242 | formats.removeWhere((element) => element == BarcodeFormat.unknown); | 263 | formats.removeWhere((element) => element == BarcodeFormat.unknown); |
| 243 | } | 264 | } |
| 244 | 265 | ||
| 245 | - final Map<Object?, Object?>? hints; | ||
| 246 | - | ||
| 247 | - if (formats.isNotEmpty && !formats.contains(BarcodeFormat.all)) { | ||
| 248 | - // Set the formats hint. | ||
| 249 | - // See https://github.com/zxing-js/library/blob/master/src/core/DecodeHintType.ts#L45 | ||
| 250 | - hints = { | ||
| 251 | - 2: formats.map(getZXingBarcodeFormat).toList(), | ||
| 252 | - }; | ||
| 253 | - } else { | ||
| 254 | - hints = null; | ||
| 255 | - } | ||
| 256 | - | ||
| 257 | - _reader = ZXingBrowserMultiFormatReader(hints.jsify(), detectionTimeoutMs); | 266 | + _reader = ZXingBrowserMultiFormatReader( |
| 267 | + _createReaderHints(formats), | ||
| 268 | + detectionTimeoutMs.toJS, | ||
| 269 | + ); | ||
| 258 | 270 | ||
| 259 | final web.HTMLVideoElement videoElement = | 271 | final web.HTMLVideoElement videoElement = |
| 260 | web.document.createElement('video') as web.HTMLVideoElement; | 272 | web.document.createElement('video') as web.HTMLVideoElement; |
| 1 | import 'dart:js_interop'; | 1 | import 'dart:js_interop'; |
| 2 | 2 | ||
| 3 | +import 'package:mobile_scanner/src/web/javascript_map.dart'; | ||
| 3 | import 'package:web/web.dart'; | 4 | import 'package:web/web.dart'; |
| 4 | 5 | ||
| 5 | /// The JS interop class for the ZXing BrowserMultiFormatReader. | 6 | /// The JS interop class for the ZXing BrowserMultiFormatReader. |
| @@ -15,8 +16,8 @@ class ZXingBrowserMultiFormatReader { | @@ -15,8 +16,8 @@ class ZXingBrowserMultiFormatReader { | ||
| 15 | /// | 16 | /// |
| 16 | /// See also: https://github.com/zxing-js/library/blob/master/src/core/DecodeHintType.ts | 17 | /// See also: https://github.com/zxing-js/library/blob/master/src/core/DecodeHintType.ts |
| 17 | external factory ZXingBrowserMultiFormatReader( | 18 | external factory ZXingBrowserMultiFormatReader( |
| 18 | - JSAny? hints, | ||
| 19 | - int? timeBetweenScansMillis, | 19 | + JsMap? hints, |
| 20 | + JSNumber? timeBetweenScansMillis, | ||
| 20 | ); | 21 | ); |
| 21 | } | 22 | } |
| 22 | 23 |
-
Please register or login to post a comment