-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr-scanner.html
87 lines (83 loc) · 2.64 KB
/
qr-scanner.html
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>QR-Code Scanner</title>
<!-- Einbinden der jsQR-Bibliothek -->
<script src="https://cdn.jsdelivr.net/npm/jsqr@1.4.0/dist/jsQR.js"></script>
<style>
body, html {
margin: 0;
padding: 0;
overflow: hidden;
}
#video {
width: 100vw;
height: 100vh;
object-fit: cover;
}
#closeButton {
position: absolute;
top: 10px;
right: 15px;
color: white;
font-size: 24px;
cursor: pointer;
z-index: 1001;
}
</style>
</head>
<body>
<video id="video" autoplay playsinline></video>
<div id="closeButton">×</div>
<script>
(function() {
const video = document.getElementById('video');
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
// Funktion zum Schließen des Scanners
document.getElementById('closeButton').addEventListener('click', function() {
stopStream();
// Informiere die übergeordnete Seite, dass der Scanner geschlossen wurde
window.parent.postMessage({ type: 'qr-scan-cancelled' }, '*');
});
// Starten des Video-Streams
navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' } })
.then(function(stream) {
video.srcObject = stream;
video.setAttribute('playsinline', true); // Für iOS
requestAnimationFrame(tick);
})
.catch(function(err) {
console.error('Fehler beim Zugriff auf die Kamera:', err);
// Informiere die übergeordnete Seite über den Fehler
window.parent.postMessage({ type: 'qr-scan-error', message: err.message }, '*');
});
function stopStream() {
if (video.srcObject) {
video.srcObject.getTracks().forEach(function(track) {
track.stop();
});
}
}
function tick() {
if (video.readyState === video.HAVE_ENOUGH_DATA) {
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
context.drawImage(video, 0, 0, canvas.width, canvas.height);
const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
const code = jsQR(imageData.data, imageData.width, imageData.height);
if (code) {
// QR-Code erkannt
stopStream();
// Sende die QR-Code-Daten an die übergeordnete Seite
window.parent.postMessage({ type: 'qr-code-scanned', data: code.data }, '*');
return;
}
}
requestAnimationFrame(tick);
}
})();
</script>
</body>
</html>