-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
47 lines (40 loc) · 1.15 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
window.addEventListener("DOMContentLoaded", () => {
console.log("DOM Content Loaded");
const video = document.querySelector("#video");
// Check if device has a camera
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
// Use video without audio
const constraints = {
audio: false,
video: {
facingMode: "environment",
},
};
// // Start video stream
const startStream = async () => {
const stream = await navigator.mediaDevices.getUserMedia(constraints);
video.srcObject = stream;
};
startStream();
// Create new barcode detector
const barcodeDetector = new BarcodeDetector({ formats: ["qr_code"] });
// Detect code function
const detectCode = () => {
// Start detecting code on the video element
barcodeDetector
.detect(video)
.then((codes) => {
if (codes.length === 0) return;
for (const barcode of codes) {
alert(barcode.rawValue);
}
})
.catch((err) => {
console.error(err);
});
};
setInterval(() => {
detectCode();
}, 1000);
}
});