Skip to content

Commit 417f80b

Browse files
committed
fix: adjust camera selection fix
Issue: #179
1 parent a0be7fa commit 417f80b

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

demo.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
{{ errorMessage }}
3434
</p>
3535

36-
<qrcode-stream @decode="onDecode" @init="onInit"></qrcode-stream>
36+
<qrcode-stream @decode="onDecode" camera="rear" @init="onInit"></qrcode-stream>
3737
</div>
3838
</body>
3939
<script>

src/components/QrcodeStream.vue

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,6 @@ export default {
100100
} else {
101101
return this.track;
102102
}
103-
},
104-
105-
facingMode() {
106-
if (this.camera === "front") return "user";
107-
else if (this.camera === "rear") return "environment";
108-
else return undefined;
109103
}
110104
},
111105
@@ -159,7 +153,7 @@ export default {
159153
};
160154
} else {
161155
this.cameraInstance = await Camera(this.$refs.video, {
162-
facingMode: this.facingMode,
156+
camera: this.camera,
163157
torch: this.torch
164158
});
165159

src/misc/camera.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Camera {
2424
}
2525
}
2626

27-
const narrowDownFacingMode = async facingMode => {
27+
const narrowDownFacingMode = async camera => {
2828
// Modern phones often have multipe front/rear cameras.
2929
// Sometimes special purpose cameras like the wide-angle camera are picked
3030
// by default. Those are not optimal for scanning QR codes but standard
@@ -36,17 +36,32 @@ const narrowDownFacingMode = async facingMode => {
3636
({ kind }) => kind === "videoinput"
3737
);
3838

39-
if (devices.length > 2) {
39+
// if (devices.length > 2) {
40+
if (false) {
4041
const frontCamera = devices[0];
4142
const rearCamera = devices[devices.length - 1];
4243

43-
if (facingMode === "front") {
44-
return { deviceId: { exact: frontCamera } };
45-
} else {
46-
return { deviceId: { exact: rearCamera } };
44+
switch (camera) {
45+
case "auto":
46+
return { deviceId: { exact: rearCamera.deviceId } };
47+
case "rear":
48+
return { deviceId: { exact: rearCamera.deviceId } };
49+
case "front":
50+
return { deviceId: { exact: frontCamera.deviceId } };
51+
default:
52+
return undefined;
4753
}
4854
} else {
49-
return { facingMode };
55+
switch (camera) {
56+
case "auto":
57+
return { facingMode: { ideal: "environment" } };
58+
case "rear":
59+
return { facingMode: { exact: "environment" } };
60+
case "front":
61+
return { facingMode: { exact: "user" } };
62+
default:
63+
return undefined;
64+
}
5065
}
5166
};
5267

@@ -60,7 +75,7 @@ const STREAM_API_NOT_SUPPORTED = !(
6075

6176
let streamApiShimApplied = false;
6277

63-
export default async function(videoEl, { facingMode, torch }) {
78+
export default async function(videoEl, { camera, torch }) {
6479
// At least in Chrome `navigator.mediaDevices` is undefined when the page is
6580
// loaded using HTTP rather than HTTPS. Thus `STREAM_API_NOT_SUPPORTED` is
6681
// initialized with `false` although the API might actually be supported.
@@ -87,7 +102,7 @@ export default async function(videoEl, { facingMode, torch }) {
87102
video: {
88103
width: { min: 360, ideal: 640, max: 1920 },
89104
height: { min: 240, ideal: 480, max: 1080 },
90-
...(await narrowDownFacingMode(facingMode))
105+
...(await narrowDownFacingMode(camera))
91106
}
92107
};
93108

0 commit comments

Comments
 (0)