// QRCODE reader Copyright 2011 Lazar Laszlo // http://www.webqr.com var gCtx = null; var gCanvas = null; var c=0; var stype=0; var gUM=false; var webkit=false; var moz=false; var v=null; var qrSize = 0; var margin = 0; var scannedCallback = null; var vidhtml = ''; function handleFiles(f) { var o=[]; for(var i =0;i/g, '>').replace(/"/g, '"'); } function read(a) { if (scannedCallback) scannedCallback(a); } function isCanvasSupported(){ var elem = document.createElement('canvas'); return !!(elem.getContext && elem.getContext('2d')); } function success(stream) { v.srcObject = stream; v.play(); gUM=true; setTimeout(captureToCanvas, 500); } function error(error) { gUM=false; return; } function load(initSize, initMargin, callback) { qrSize = initSize; margin = initMargin; scannedCallback = callback; if(isCanvasSupported() && window.File && window.FileReader) { initCanvas(800, 600); qrcode.callback = read; setwebcam(); } else { console.log("Sorry your browser is not supported"); } } function setwebcam() { var options = true; if(navigator.mediaDevices && navigator.mediaDevices.enumerateDevices) { try{ navigator.mediaDevices.enumerateDevices() .then(function(devices) { devices.forEach(function(device) { if (device.kind === 'videoinput') { if(device.label.toLowerCase().search("back") >-1) options={'deviceId': {'exact':device.deviceId}, 'facingMode':'environment'} ; } console.log(device.kind + ": " + device.label +" id = " + device.deviceId); }); setwebcam2(options); }); } catch(e) { console.log(e); } } else{ console.log("no navigator.mediaDevices.enumerateDevices" ); setwebcam2(options); } } function setwebcam2(options) { console.log(options); if(stype==1) { setTimeout(captureToCanvas, 500); return; } var n=navigator; document.getElementById("outdiv").innerHTML = vidhtml; v=document.getElementById("video-stream"); v.style.width = qrSize + "px"; v.style.marginLeft = margin + "px"; if(n.mediaDevices.getUserMedia) { n.mediaDevices.getUserMedia({video: options, audio: false}). then(function(stream){ success(stream); }).catch(function(error){ error(error) }); } else if(n.getUserMedia) { webkit=true; n.getUserMedia({video: options, audio: false}, success, error); } else if(n.webkitGetUserMedia) { webkit=true; n.webkitGetUserMedia({video:options, audio: false}, success, error); } stype=1; setTimeout(captureToCanvas, 500); }