p-mazhnik

refactor: abstract torch support

... ... @@ -34,9 +34,6 @@ class MobileScannerWebPlugin {
// ID of the video feed
String viewID = 'WebScanner-${DateTime.now().millisecondsSinceEpoch}';
// Determine wether device has flas
bool hasFlash = false;
final html.DivElement vidDiv = html.DivElement();
late final WebBarcodeReaderBase _barCodeReader =
... ... @@ -63,15 +60,7 @@ class MobileScannerWebPlugin {
/// Can enable or disable the flash if available
Future<void> _torch(arguments) async {
// if (hasFlash) {
// final track = _localStream?.getVideoTracks();
// await track!.first.applyConstraints({
// 'advanced': {'torch': arguments == 1}
// });
// } else {
// controller.addError('Device has no flash');
// }
controller.addError('Device has no flash');
_barCodeReader.toggleTorch(enabled: arguments == 1);
}
/// Starts the video stream and the scanner
... ... @@ -97,7 +86,8 @@ class MobileScannerWebPlugin {
return {
'ViewID': viewID,
'videoWidth': _barCodeReader.videoWidth,
'videoHeight': _barCodeReader.videoHeight
'videoHeight': _barCodeReader.videoHeight,
'torchable': _barCodeReader.hasTorch,
};
}
try {
... ... @@ -115,7 +105,7 @@ class MobileScannerWebPlugin {
'ViewID': viewID,
'videoWidth': _barCodeReader.videoWidth,
'videoHeight': _barCodeReader.videoHeight,
'torchable': hasFlash
'torchable': _barCodeReader.hasTorch,
};
} catch (e) {
throw PlatformException(code: 'MobileScannerWeb', message: '$e');
... ...
... ... @@ -29,6 +29,12 @@ abstract class WebBarcodeReaderBase {
/// Stops streaming video
Future<void> stop();
/// Can enable or disable the flash if available
Future<void> toggleTorch({required bool enabled});
/// Determine whether device has flash
bool get hasTorch;
}
mixin InternalStreamCreation on WebBarcodeReaderBase {
... ... @@ -86,3 +92,30 @@ mixin InternalStreamCreation on WebBarcodeReaderBase {
videoContainer.children = [];
}
}
/// Mixin for libraries that don't have built-in torch support
mixin InternalTorchDetection on InternalStreamCreation {
@override
bool get hasTorch {
// TODO: fix flash light. See https://github.com/dart-lang/sdk/issues/48533
// final track = _localStream?.getVideoTracks();
// if (track != null) {
// final imageCapture = html.ImageCapture(track.first);
// final photoCapabilities = await imageCapture.getPhotoCapabilities();
// }
return false;
}
@override
Future<void> toggleTorch({required bool enabled}) async {
if (hasTorch) {
final track = localMediaStream?.getVideoTracks();
await track?.first.applyConstraints({
'advanced': [
{'torch': enabled}
]
});
}
}
}
... ...
... ... @@ -20,7 +20,8 @@ class Code {
}
class JsQrCodeReader extends WebBarcodeReaderBase with InternalStreamCreation {
class JsQrCodeReader extends WebBarcodeReaderBase
with InternalStreamCreation, InternalTorchDetection {
JsQrCodeReader({required super.videoContainer});
@override
... ... @@ -34,13 +35,6 @@ class JsQrCodeReader extends WebBarcodeReaderBase with InternalStreamCreation {
final stream = await initMediaStream(cameraFacing);
// TODO: fix flash light. See https://github.com/dart-lang/sdk/issues/48533
// final track = _localStream?.getVideoTracks();
// if (track != null) {
// final imageCapture = html.ImageCapture(track.first);
// final photoCapabilities = await imageCapture.getPhotoCapabilities();
// }
prepareVideoElement(video);
if (stream != null) {
await attachStreamToVideo(stream, video);
... ...