Showing
1 changed file
with
0 additions
and
199 deletions
lib/src/web/base.dart
deleted
100644 → 0
| 1 | -import 'dart:html' as html; | ||
| 2 | - | ||
| 3 | -import 'package:flutter/material.dart'; | ||
| 4 | -import 'package:js/js.dart'; | ||
| 5 | -import 'package:js/js_util.dart'; | ||
| 6 | -import 'package:mobile_scanner/src/enums/barcode_format.dart'; | ||
| 7 | -import 'package:mobile_scanner/src/enums/camera_facing.dart'; | ||
| 8 | -import 'package:mobile_scanner/src/objects/barcode.dart'; | ||
| 9 | -import 'package:mobile_scanner/src/web/media.dart'; | ||
| 10 | - | ||
| 11 | -class JsLibrary { | ||
| 12 | - /// The name of global variable where library is stored. | ||
| 13 | - /// Used to properly import the library if [usesRequireJs] flag is true | ||
| 14 | - final String contextName; | ||
| 15 | - final String url; | ||
| 16 | - | ||
| 17 | - /// If js code checks for 'define' variable. | ||
| 18 | - /// E.g. if at the beginning you see code like | ||
| 19 | - /// if (typeof define === "function" && define.amd) | ||
| 20 | - final bool usesRequireJs; | ||
| 21 | - | ||
| 22 | - const JsLibrary({ | ||
| 23 | - required this.contextName, | ||
| 24 | - required this.url, | ||
| 25 | - required this.usesRequireJs, | ||
| 26 | - }); | ||
| 27 | -} | ||
| 28 | - | ||
| 29 | -abstract class WebBarcodeReaderBase { | ||
| 30 | - /// Timer used to capture frames to be analyzed | ||
| 31 | - Duration frameInterval = const Duration(milliseconds: 200); | ||
| 32 | - final html.DivElement videoContainer; | ||
| 33 | - | ||
| 34 | - WebBarcodeReaderBase({ | ||
| 35 | - required this.videoContainer, | ||
| 36 | - }); | ||
| 37 | - | ||
| 38 | - bool get isStarted; | ||
| 39 | - | ||
| 40 | - int get videoWidth; | ||
| 41 | - int get videoHeight; | ||
| 42 | - | ||
| 43 | - /// Starts streaming video | ||
| 44 | - Future<void> start({ | ||
| 45 | - required CameraFacing cameraFacing, | ||
| 46 | - List<BarcodeFormat>? formats, | ||
| 47 | - Duration? detectionTimeout, | ||
| 48 | - }); | ||
| 49 | - | ||
| 50 | - /// Starts scanning QR codes or barcodes | ||
| 51 | - Stream<Barcode?> detectBarcodeContinuously(); | ||
| 52 | - | ||
| 53 | - /// Stops scanning QR codes or barcodes | ||
| 54 | - Future<void> stopDetectBarcodeContinuously(); | ||
| 55 | - | ||
| 56 | - /// Stops streaming video | ||
| 57 | - Future<void> stop(); | ||
| 58 | - | ||
| 59 | - /// Can enable or disable the flash if available | ||
| 60 | - Future<void> toggleTorch({required bool enabled}); | ||
| 61 | - | ||
| 62 | - /// Determine whether device has flash | ||
| 63 | - Future<bool> hasTorch(); | ||
| 64 | -} | ||
| 65 | - | ||
| 66 | -mixin InternalStreamCreation on WebBarcodeReaderBase { | ||
| 67 | - /// The video stream. | ||
| 68 | - /// Will be initialized later to see which camera needs to be used. | ||
| 69 | - html.MediaStream? localMediaStream; | ||
| 70 | - final html.VideoElement video = html.VideoElement(); | ||
| 71 | - | ||
| 72 | - @override | ||
| 73 | - int get videoWidth => video.videoWidth; | ||
| 74 | - @override | ||
| 75 | - int get videoHeight => video.videoHeight; | ||
| 76 | - | ||
| 77 | - Future<html.MediaStream?> initMediaStream(CameraFacing cameraFacing) async { | ||
| 78 | - // Check if browser supports multiple camera's and set if supported | ||
| 79 | - final Map? capabilities = | ||
| 80 | - html.window.navigator.mediaDevices?.getSupportedConstraints(); | ||
| 81 | - final Map<String, dynamic> constraints; | ||
| 82 | - if (capabilities != null && capabilities['facingMode'] as bool) { | ||
| 83 | - constraints = { | ||
| 84 | - 'video': VideoOptions( | ||
| 85 | - facingMode: | ||
| 86 | - cameraFacing == CameraFacing.front ? 'user' : 'environment', | ||
| 87 | - ), | ||
| 88 | - }; | ||
| 89 | - } else { | ||
| 90 | - constraints = {'video': true}; | ||
| 91 | - } | ||
| 92 | - final stream = | ||
| 93 | - await html.window.navigator.mediaDevices?.getUserMedia(constraints); | ||
| 94 | - return stream; | ||
| 95 | - } | ||
| 96 | - | ||
| 97 | - void prepareVideoElement(html.VideoElement videoSource); | ||
| 98 | - | ||
| 99 | - Future<void> attachStreamToVideo( | ||
| 100 | - html.MediaStream stream, | ||
| 101 | - html.VideoElement videoSource, | ||
| 102 | - ); | ||
| 103 | - | ||
| 104 | - @override | ||
| 105 | - Future<void> stop() async { | ||
| 106 | - try { | ||
| 107 | - // Stop the camera stream | ||
| 108 | - localMediaStream?.getTracks().forEach((track) { | ||
| 109 | - if (track.readyState == 'live') { | ||
| 110 | - track.stop(); | ||
| 111 | - } | ||
| 112 | - }); | ||
| 113 | - } catch (e) { | ||
| 114 | - debugPrint('Failed to stop stream: $e'); | ||
| 115 | - } | ||
| 116 | - video.srcObject = null; | ||
| 117 | - localMediaStream = null; | ||
| 118 | - videoContainer.children = []; | ||
| 119 | - } | ||
| 120 | -} | ||
| 121 | - | ||
| 122 | -/// Mixin for libraries that don't have built-in torch support | ||
| 123 | -mixin InternalTorchDetection on InternalStreamCreation { | ||
| 124 | - Future<List<String>> getSupportedTorchStates() async { | ||
| 125 | - try { | ||
| 126 | - final track = localMediaStream?.getVideoTracks(); | ||
| 127 | - if (track != null) { | ||
| 128 | - final imageCapture = ImageCapture(track.first); | ||
| 129 | - final photoCapabilities = await promiseToFuture<PhotoCapabilities>( | ||
| 130 | - imageCapture.getPhotoCapabilities(), | ||
| 131 | - ); | ||
| 132 | - | ||
| 133 | - return photoCapabilities.fillLightMode; | ||
| 134 | - } | ||
| 135 | - } catch (e) { | ||
| 136 | - // ImageCapture is not supported by some browsers: | ||
| 137 | - // https://developer.mozilla.org/en-US/docs/Web/API/ImageCapture#browser_compatibility | ||
| 138 | - } | ||
| 139 | - return []; | ||
| 140 | - } | ||
| 141 | - | ||
| 142 | - @override | ||
| 143 | - Future<bool> hasTorch() async { | ||
| 144 | - return (await getSupportedTorchStates()).isNotEmpty; | ||
| 145 | - } | ||
| 146 | - | ||
| 147 | - @override | ||
| 148 | - Future<void> toggleTorch({required bool enabled}) async { | ||
| 149 | - final hasTorch = await this.hasTorch(); | ||
| 150 | - if (hasTorch) { | ||
| 151 | - final track = localMediaStream?.getVideoTracks(); | ||
| 152 | - await track?.first.applyConstraints({ | ||
| 153 | - 'advanced': [ | ||
| 154 | - {'torch': enabled}, | ||
| 155 | - ], | ||
| 156 | - }); | ||
| 157 | - } | ||
| 158 | - } | ||
| 159 | -} | ||
| 160 | - | ||
| 161 | -@JS('Promise') | ||
| 162 | -@staticInterop | ||
| 163 | -class Promise<T> {} | ||
| 164 | - | ||
| 165 | -@JS() | ||
| 166 | -@anonymous | ||
| 167 | -@staticInterop | ||
| 168 | -class PhotoCapabilities {} | ||
| 169 | - | ||
| 170 | -extension PhotoCapabilitiesExtension on PhotoCapabilities { | ||
| 171 | - @JS('fillLightMode') | ||
| 172 | - external List<dynamic>? get _fillLightMode; | ||
| 173 | - | ||
| 174 | - /// Returns an array of available fill light options. Options include auto, off, or flash. | ||
| 175 | - List<String> get fillLightMode => | ||
| 176 | - _fillLightMode?.cast<String>() ?? <String>[]; | ||
| 177 | -} | ||
| 178 | - | ||
| 179 | -@JS('ImageCapture') | ||
| 180 | -@staticInterop | ||
| 181 | -class ImageCapture { | ||
| 182 | - /// MediaStreamTrack | ||
| 183 | - external factory ImageCapture(dynamic track); | ||
| 184 | -} | ||
| 185 | - | ||
| 186 | -extension ImageCaptureExt on ImageCapture { | ||
| 187 | - external Promise<PhotoCapabilities> getPhotoCapabilities(); | ||
| 188 | -} | ||
| 189 | - | ||
| 190 | -@JS('Map') | ||
| 191 | -@staticInterop | ||
| 192 | -class JsMap { | ||
| 193 | - external factory JsMap(); | ||
| 194 | -} | ||
| 195 | - | ||
| 196 | -extension JsMapExt on JsMap { | ||
| 197 | - external void set(dynamic key, dynamic value); | ||
| 198 | - external dynamic get(dynamic key); | ||
| 199 | -} |
-
Please register or login to post a comment