From 4b3d22ca698eac7d82b5ff5f5bea8f8d1e568eac Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 15 Jul 2014 19:29:28 -0700 Subject: Create a page with a QR scanner for sweeping paper wallets --- public/js/webqr.js | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 public/js/webqr.js (limited to 'public/js/webqr.js') diff --git a/public/js/webqr.js b/public/js/webqr.js new file mode 100644 index 00000000..06f85b18 --- /dev/null +++ b/public/js/webqr.js @@ -0,0 +1,134 @@ +// 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 vidhtml = ''; + +$(function() { + load(); +}); + +function initCanvas(w,h) +{ + gCanvas = document.getElementById("qr-canvas"); + gCanvas.style.width = w + "px"; + gCanvas.style.height = h + "px"; + gCanvas.width = w; + gCanvas.height = h; + gCtx = gCanvas.getContext("2d"); + gCtx.clearRect(0, 0, w, h); +} + + +function captureToCanvas() { + if(stype!=1) + return; + if(gUM) + { + try{ + gCtx.drawImage(v,0,0); + try{ + qrcode.decode(); + } + catch(e){ + console.log(e); + setTimeout(captureToCanvas, 500); + }; + } + catch(e){ + console.log(e); + setTimeout(captureToCanvas, 500); + }; + } +} + +function htmlEntities(str) { + return String(str).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); +} + +function read(a) +{ + var html="
"; + html+=""+htmlEntities(a)+"

"; + document.getElementById("result").innerHTML=html; +} + +function isCanvasSupported(){ + var elem = document.createElement('canvas'); + return !!(elem.getContext && elem.getContext('2d')); +} +function success(stream) { + if(webkit) + v.src = window.webkitURL.createObjectURL(stream); + else + if(moz) + { + v.mozSrcObject = stream; + v.play(); + } + else + v.src = stream; + gUM=true; + setTimeout(captureToCanvas, 500); +} + +function error(error) { + gUM=false; + return; +} + +function load() +{ + if(isCanvasSupported() && window.File && window.FileReader) + { + initCanvas(800, 800); + qrcode.callback = read; + setwebcam(); + } + else + { + document.getElementById("mainbody").style.display="inline"; + document.getElementById("mainbody").innerHTML='

QR code scanner for HTML5 capable browsers


'+ + '

sorry your browser is not supported



'+ + '

try or or

'; + } +} + +function setwebcam() +{ + document.getElementById("result").innerHTML="- scanning -"; + if(stype==1) + { + setTimeout(captureToCanvas, 500); + return; + } + var n=navigator; + document.getElementById("outdiv").innerHTML = vidhtml; + v=document.getElementById("v"); + + if(n.getUserMedia) + n.getUserMedia({video: true, audio: false}, success, error); + else + if(n.webkitGetUserMedia) + { + webkit=true; + n.webkitGetUserMedia({video: true, audio: false}, success, error); + } + else + if(n.mozGetUserMedia) + { + moz=true; + n.mozGetUserMedia({video: true, audio: false}, success, error); + } + + stype=1; + setTimeout(captureToCanvas, 500); +} -- cgit v1.2.3