Skip to content

Instantly share code, notes, and snippets.

@ekopradesga
Created May 13, 2022 09:49
Show Gist options
  • Save ekopradesga/c987b7e3a09412c3d97cc19cc38c1aa1 to your computer and use it in GitHub Desktop.
Save ekopradesga/c987b7e3a09412c3d97cc19cc38c1aa1 to your computer and use it in GitHub Desktop.
QR camera
const video = document.createElement("video"),
canvasElement = document.getElementById("qr-canvas"),
canvas = canvasElement.getContext("2d"),
outputData = document.getElementById("docqrcode"),
btnScanQR = document.getElementById("take-camera");
let scanning = !1;
function tick() {
canvasElement.height = video.videoHeight, canvasElement.width = video.videoWidth, canvas.drawImage(video, 0, 0, canvasElement.width, canvasElement.height), scanning && requestAnimationFrame(tick)
}
function scan() {
try {
qrcode.decode()
} catch (e) {
setTimeout(scan, 300)
}
}
qrcode.callback = e => {
if (e) {
document.fullscreenElement && (document.exitFullscreen ? document.exitFullscreen() : document.mozCancelFullScreen ? document.mozCancelFullScreen() : document.webkitCancelFullScreen ? document.webkitCancelFullScreen() : document.msExitFullscreen && document.msExitFullscreen());
const n = e.split("/");
outputData.value = n[n.length - 1], scanning = !1, video.srcObject.getTracks().forEach((e => {
e.stop()
})), canvasElement.hidden = !0, document.getElementById("formvalidation").submit()
}
}, btnScanQR.onclick = () => {
navigator.mediaDevices.getUserMedia({
video: {
facingMode: "environment"
}
}).then((function(e) {
document.querySelector('meta[name="viewport"]').setAttribute("content", "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,"), scanning = !0, canvasElement.hidden = !1, canvasElement.requestFullscreen ? canvasElement.requestFullscreen() : canvasElement.webkitRequestFullscreen ? canvasElement.webkitRequestFullscreen() : canvasElement.msRequestFullscreen && canvasElement.msRequestFullscreen(), video.srcObject = e, video.play(), tick(), scan()
}))
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment