Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // QR-Code
- var track;
- function readQR() {
- removeQRCodeLink();
- var video = document.getElementById('webcamVideo');
- setupVideo(video);
- //Scanner
- let scanner = new Instascan.Scanner({
- video
- });
- //Listens for valid QRCode read by webcam
- scanner.addListener('scan', function(content) {
- qrCode_content = content; // content returns QRCode
- $.fn.getXMLData("qrCode");
- //stop scanning
- stopScan(scanner);
- addQRCodeLink();
- });
- //Again, ask for camera access, check if camera was found ...
- Instascan.Camera.getCameras().then(function(cameras) {
- if (cameras.length > 0) {
- scanner.start(cameras[0]); //Start scanning on first webcam
- } else {
- alert("No cameras found.");
- }
- }).catch(function(e) {
- alert("Error opening camera");
- });
- }
- function setupVideo(video) {
- //Access Camera to get Stream to play video
- navigator.mediaDevices.getUserMedia({
- video: true,
- audio: false
- })
- .then(function(stream) {
- track = stream;
- video.srcObject = track;
- video.play();
- })
- .catch(function(err) {
- console.log("An error occured! " + err);
- });
- //video dimensions
- var width = 400;
- var height = 0;
- height = video.videoHeight / (video.videoWidth / width); //height ist abhängig von stream width(hor oder ver)
- video.setAttribute('width', width);
- video.setAttribute('height', height);
- }
- function stopScan(scanner) {
- scanner.stop();
- var video = document.getElementById('webcamVideo');
- stopStreamedVideo(video);
- }
- function removeQRCodeLink() {
- /var qrcodeSection = document.getElementById('qrVideoDiv');
- qrcodeSection.removeChild(document.getElementById('qrCodeLink'));/
- var qrCodeLink = $("#qrCodeLink");
- qrCodeLink.css("display", "none");
- }
- function addQRCodeLink() {
- /var a = document.createElement("a");
- a.setAttribute("id", "qrCodeLink");
- a.setAttribute("href", "javascript:readQR()");
- var img = document.createElement("img");
- img.setAttribute("src", "res/img/qrcode.png");
- img.setAttribute("alt", "Press to Scan QR Code");
- a.appendChild(img);
- var qrCodeSection = document.getElementById('roomsQrCode');
- qrCodeSection.insertAdjacentElement('afterbegin', a);/
- var qrCodeLink = $("#qrCodeLink");
- qrCodeLink.css("display", "inline");
- }
- function stopStreamedVideo(videoElem) {
- let stream = videoElem.srcObject;
- let tracks = stream.getTracks();
- tracks.forEach(function(track) {
- track.stop();
- });
- videoElem.src = null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement