Advertisement
PowerSlaveAlfons

Untitled

Jan 4th, 2019
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // QR-Code
  2.   var track;
  3.  
  4.   function readQR() {
  5.     removeQRCodeLink();
  6.     var video = document.getElementById('webcamVideo');
  7.     setupVideo(video);
  8.     //Scanner
  9.     let scanner = new Instascan.Scanner({
  10.       video
  11.     });
  12.  
  13.     //Listens for valid QRCode read by webcam
  14.     scanner.addListener('scan', function(content) {
  15.       qrCode_content = content; // content returns QRCode
  16.       $.fn.getXMLData("qrCode");
  17.       //stop scanning
  18.       stopScan(scanner);
  19.       addQRCodeLink();
  20.     });
  21.  
  22.     //Again, ask for camera access, check if camera was found ...
  23.     Instascan.Camera.getCameras().then(function(cameras) {
  24.       if (cameras.length > 0) {
  25.         scanner.start(cameras[0]); //Start scanning on first webcam
  26.       } else {
  27.         alert("No cameras found.");
  28.       }
  29.     }).catch(function(e) {
  30.       alert("Error opening camera");
  31.     });
  32.   }
  33.  
  34.   function setupVideo(video) {
  35.     //Access Camera to get Stream to play video
  36.     navigator.mediaDevices.getUserMedia({
  37.       video: true,
  38.       audio: false
  39.     })
  40.     .then(function(stream) {
  41.       track = stream;
  42.       video.srcObject = track;
  43.       video.play();
  44.     })
  45.     .catch(function(err) {
  46.       console.log("An error occured! " + err);
  47.     });
  48. //video dimensions
  49.     var width = 400;
  50.     var height = 0;
  51.  
  52.     height = video.videoHeight / (video.videoWidth / width); //height ist abhängig von stream width(hor oder ver)
  53.  
  54.     video.setAttribute('width', width);
  55.     video.setAttribute('height', height);
  56.   }
  57.  
  58.   function stopScan(scanner) {
  59.     scanner.stop();
  60.     var video = document.getElementById('webcamVideo');
  61.     stopStreamedVideo(video);
  62.   }
  63.  
  64.   function removeQRCodeLink() {
  65.     /var qrcodeSection = document.getElementById('qrVideoDiv');
  66.     qrcodeSection.removeChild(document.getElementById('qrCodeLink'));/
  67.     var qrCodeLink = $("#qrCodeLink");
  68.     qrCodeLink.css("display", "none");
  69.   }
  70.  
  71.   function addQRCodeLink() {
  72.     /var a = document.createElement("a");
  73.     a.setAttribute("id", "qrCodeLink");
  74.     a.setAttribute("href", "javascript:readQR()");
  75.     var img = document.createElement("img");
  76.     img.setAttribute("src", "res/img/qrcode.png");
  77.     img.setAttribute("alt", "Press to Scan QR Code");
  78.     a.appendChild(img);
  79.     var qrCodeSection = document.getElementById('roomsQrCode');
  80.     qrCodeSection.insertAdjacentElement('afterbegin', a);/
  81.     var qrCodeLink = $("#qrCodeLink");
  82.     qrCodeLink.css("display", "inline");
  83.   }
  84.  
  85.   function stopStreamedVideo(videoElem) {
  86.     let stream = videoElem.srcObject;
  87.     let tracks = stream.getTracks();
  88.  
  89.     tracks.forEach(function(track) {
  90.       track.stop();
  91.     });
  92.  
  93.     videoElem.src = null;
  94.  
  95.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement