Skip to content

Commit

Permalink
find camera
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorWinberg committed Aug 19, 2024
1 parent 523dc42 commit 13e6e8b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions client/src/components/QRScanner.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { mapState, mapMutations, mapActions } from "vuex";
import QRScanner from "qr-scanner";
import { QR_SPOT_PANEL } from "@/constants";
import { findCamera } from "@/utils";
// eslint-disable-next-line import/no-webpack-loader-syntax
import QRScannerWorkerPath from "!!file-loader!../../node_modules/qr-scanner/qr-scanner-worker.min.js";
QRScanner.WORKER_PATH = QRScannerWorkerPath;
Expand Down Expand Up @@ -69,8 +71,7 @@ export default Vue.extend({
async initScanner() {
await navigator.mediaDevices.getUserMedia({ audio: false, video: true });
const devices = await navigator.mediaDevices.enumerateDevices();
const cameras = devices.filter(device => device.kind === "videoinput");
const { deviceId } = cameras[(this.user.cameraId || 0) % cameras.length];
const { deviceId } = findCamera(devices);
const { qrscan } = this.$refs;
if (!deviceId || !qrscan) return;
Expand Down
15 changes: 15 additions & 0 deletions client/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,18 @@ export function calculateWalkingTime(distanceInMeters: number) {
const walkingTimeInSeconds = distanceInMeters / walkingSpeed;
return Math.ceil(walkingTimeInSeconds / 60);
}

export function findCamera(devices: MediaDeviceInfo[]) {
const cameras = devices.filter(device => device.kind === "videoinput");

const lenghts = cameras
.map(item => item.label.length)
.sort((a, b) => a - b)
.slice(0, 4);

const mainCameras = cameras.filter(camera =>
lenghts.includes(camera.label.length)
);

return mainCameras[mainCameras.length - 1];
}

0 comments on commit 13e6e8b

Please sign in to comment.