Showing
1 changed file
with
0 additions
and
104 deletions
lib/src/web/jsqr.dart
deleted
100644 → 0
| 1 | -@JS() | ||
| 2 | -library jsqr; | ||
| 3 | - | ||
| 4 | -import 'dart:async'; | ||
| 5 | -import 'dart:html'; | ||
| 6 | -import 'dart:typed_data'; | ||
| 7 | - | ||
| 8 | -import 'package:js/js.dart'; | ||
| 9 | -import 'package:mobile_scanner/src/enums/barcode_format.dart'; | ||
| 10 | -import 'package:mobile_scanner/src/enums/camera_facing.dart'; | ||
| 11 | -import 'package:mobile_scanner/src/objects/barcode.dart'; | ||
| 12 | -import 'package:mobile_scanner/src/web/base.dart'; | ||
| 13 | - | ||
| 14 | -@JS('jsQR') | ||
| 15 | -external Code? jsQR(dynamic data, int? width, int? height); | ||
| 16 | - | ||
| 17 | -@JS() | ||
| 18 | -class Code { | ||
| 19 | - external String get data; | ||
| 20 | - | ||
| 21 | - external Uint8ClampedList get binaryData; | ||
| 22 | -} | ||
| 23 | - | ||
| 24 | -/// Barcode reader that uses jsQR library. | ||
| 25 | -/// jsQR supports only QR codes format. | ||
| 26 | -class JsQrCodeReader extends WebBarcodeReaderBase | ||
| 27 | - with InternalStreamCreation, InternalTorchDetection { | ||
| 28 | - JsQrCodeReader({required super.videoContainer}); | ||
| 29 | - | ||
| 30 | - @override | ||
| 31 | - bool get isStarted => localMediaStream != null; | ||
| 32 | - | ||
| 33 | - @override | ||
| 34 | - Future<void> start({ | ||
| 35 | - required CameraFacing cameraFacing, | ||
| 36 | - List<BarcodeFormat>? formats, | ||
| 37 | - Duration? detectionTimeout, | ||
| 38 | - }) async { | ||
| 39 | - videoContainer.children = [video]; | ||
| 40 | - | ||
| 41 | - if (detectionTimeout != null) { | ||
| 42 | - frameInterval = detectionTimeout; | ||
| 43 | - } | ||
| 44 | - | ||
| 45 | - final stream = await initMediaStream(cameraFacing); | ||
| 46 | - | ||
| 47 | - prepareVideoElement(video); | ||
| 48 | - if (stream != null) { | ||
| 49 | - await attachStreamToVideo(stream, video); | ||
| 50 | - } | ||
| 51 | - } | ||
| 52 | - | ||
| 53 | - @override | ||
| 54 | - void prepareVideoElement(VideoElement videoSource) { | ||
| 55 | - // required to tell iOS safari we don't want fullscreen | ||
| 56 | - videoSource.setAttribute('playsinline', 'true'); | ||
| 57 | - } | ||
| 58 | - | ||
| 59 | - @override | ||
| 60 | - Future<void> attachStreamToVideo( | ||
| 61 | - MediaStream stream, | ||
| 62 | - VideoElement videoSource, | ||
| 63 | - ) async { | ||
| 64 | - localMediaStream = stream; | ||
| 65 | - videoSource.srcObject = stream; | ||
| 66 | - await videoSource.play(); | ||
| 67 | - } | ||
| 68 | - | ||
| 69 | - @override | ||
| 70 | - Stream<Barcode?> detectBarcodeContinuously() async* { | ||
| 71 | - yield* Stream.periodic(frameInterval, (_) { | ||
| 72 | - return _captureFrame(video); | ||
| 73 | - }).asyncMap((event) async { | ||
| 74 | - final code = await event; | ||
| 75 | - if (code == null) { | ||
| 76 | - return null; | ||
| 77 | - } | ||
| 78 | - return Barcode( | ||
| 79 | - rawValue: code.data, | ||
| 80 | - rawBytes: Uint8List.fromList(code.binaryData), | ||
| 81 | - format: BarcodeFormat.qrCode, | ||
| 82 | - ); | ||
| 83 | - }); | ||
| 84 | - } | ||
| 85 | - | ||
| 86 | - @override | ||
| 87 | - Future<void> stopDetectBarcodeContinuously() async { | ||
| 88 | - return; | ||
| 89 | - } | ||
| 90 | - | ||
| 91 | - /// Captures a frame and analyzes it for QR codes | ||
| 92 | - Future<Code?> _captureFrame(VideoElement video) async { | ||
| 93 | - if (localMediaStream == null) return null; | ||
| 94 | - final canvas = | ||
| 95 | - CanvasElement(width: video.videoWidth, height: video.videoHeight); | ||
| 96 | - final ctx = canvas.context2D; | ||
| 97 | - | ||
| 98 | - ctx.drawImage(video, 0, 0); | ||
| 99 | - final imgData = ctx.getImageData(0, 0, canvas.width!, canvas.height!); | ||
| 100 | - | ||
| 101 | - final code = jsQR(imgData.data, canvas.width, canvas.height); | ||
| 102 | - return code; | ||
| 103 | - } | ||
| 104 | -} |
-
Please register or login to post a comment