Skip to content

Commit b475922

Browse files
committed
feat(QrcodeStream): add torch prop
Adds `torch` prop for turning a devices flashlight on/off. Initially suggested by @magyarb
1 parent 1ef93db commit b475922

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/components/QrcodeStream.vue

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export default {
4747
}
4848
},
4949
50+
torch: {
51+
type: Boolean,
52+
default: false
53+
},
54+
5055
track: {
5156
type: [Function, Boolean],
5257
default: true
@@ -146,6 +151,10 @@ export default {
146151
}
147152
},
148153
154+
torch() {
155+
this.$emit("init", this.init());
156+
},
157+
149158
constraints() {
150159
this.$emit("init", this.init());
151160
}
@@ -168,7 +177,9 @@ export default {
168177
if (this.constraints === undefined) {
169178
this.cameraInstance = null;
170179
} else {
171-
this.cameraInstance = await Camera(this.constraints, this.$refs.video);
180+
this.cameraInstance = await Camera(this.constraints, this.$refs.video, {
181+
torch: this.torch
182+
});
172183
173184
// if the component is destroyed before `cameraInstance` resolves a
174185
// `beforeDestroy` hook has no chance to clear the remaining camera

src/misc/camera.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const STREAM_API_NOT_SUPPORTED = !(
2828

2929
let streamApiShimApplied = false;
3030

31-
export default async function(constraints, videoEl) {
31+
export default async function(constraints, videoEl, advancedConstraints) {
3232
// At least in Chrome `navigator.mediaDevices` is undefined when the page is
3333
// loaded using HTTP rather than HTTPS. Thus `STREAM_API_NOT_SUPPORTED` is
3434
// initialized with `false` although the API might actually be supported.
@@ -64,5 +64,15 @@ export default async function(constraints, videoEl) {
6464

6565
await eventOn(videoEl, "loadeddata");
6666

67+
if (advancedConstraints.torch) {
68+
const [track] = stream.getVideoTracks();
69+
70+
try {
71+
await track.applyConstraints({ advanced: [{ torch: true }] });
72+
} catch (error) {
73+
console.warn("device does not support torch capability");
74+
}
75+
}
76+
6777
return new Camera(videoEl, stream);
6878
}

0 commit comments

Comments
 (0)