forked from dsias/blockchain.info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqr.code.reader.js
179 lines (139 loc) · 5.6 KB
/
qr.code.reader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var gCtx = null;
var gCanvas = null;
var imageData = null;
var c=0;
//Called by the flash QR Scanner
function passLine(stringPixels) {
var coll = stringPixels.split("-");
for(var i=0;i<320;i++) {
var intVal = parseInt(coll[i]);
r = (intVal >> 16) & 0xff;
g = (intVal >> 8) & 0xff;
b = (intVal ) & 0xff;
imageData.data[c+0]=r;
imageData.data[c+1]=g;
imageData.data[c+2]=b;
imageData.data[c+3]=255;
c+=4;
}
if(c>=320*240*4) {
c=0;
gCtx.putImageData(imageData, 0,0);
try{
qrcode.decode();
} catch(e){ };
}
}
function captureToCanvas() {
try {
flash = document.getElementById("embedflash");
if(!flash)
return;
flash.ccCapture();
} catch (e) {
console.log(e);
}
}
var QRCodeReader = {
video:'',
container:'',
canvas:'',
ctx:'',
out:'',
found:false,
timer:'',
_stream : null,
isCanvasSupported : function(){
var elem = document.createElement('canvas');
return !!(elem.getContext && elem.getContext('2d'));
},
loop: function() {
QRCodeReader.captureToCanvas();
},
captureToCanvas: function() {
QRCodeReader.ctx.drawImage(QRCodeReader.video, 0, 0, QRCodeReader.video.videoWidth, QRCodeReader.video.videoHeight, 0, 0, QRCodeReader.canvas.width, QRCodeReader.canvas.height);
qrcode.decode(QRCodeReader.canvas.toDataURL());
},
canvasInit: function() {
QRCodeReader.canvas = document.createElement('canvas');
QRCodeReader.canvas.width = QRCodeReader.video.videoWidth;
QRCodeReader.canvas.height = QRCodeReader.video.videoHeight;
QRCodeReader.ctx = QRCodeReader.canvas.getContext('2d');
QRCodeReader.interval = setInterval(QRCodeReader.loop, 500);
},
stop : function() {
if (QRCodeReader.interval) {
clearInterval(QRCodeReader.interval);
QRCodeReader.interval = null;
}
if (QRCodeReader.reader_container) {
QRCodeReader.reader_container.empty();
QRCodeReader.reader_container = null;
}
try {
if (this._stream) {
this._stream.stop();
this._stream = null;
}
} catch (e) {
console.log(e);
}
},
//Pass in Jquery Obj
init: function(el, success, error) {
if (!QRCodeReader.isCanvasSupported()) {
error('Sorry your browser does not support canvas. Please try Firefox, Chrome or safari.');
return;
}
var hasFlash = false;
try {
var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
if(fo) hasFlash = true;
} catch(e){
if(navigator.mimeTypes ["application/x-shockwave-flash"] != undefined) hasFlash = true;
}
loadScript('wallet/llqrcode', function() {
// Standard and prefixed methods for hooking into stream
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.msGetUserMedia;
window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
QRCodeReader.reader_container = el.find('.qr-code-reader');
if (navigator.getUserMedia) {
//Append the video element
QRCodeReader.reader_container.html('<video style="width:320px;height:240px" autoplay id="sourcevid"></video>');
QRCodeReader.video = QRCodeReader.reader_container.find('video').get(0);
QRCodeReader.flash = null;
navigator.getUserMedia({video: true}, function(stream) {
QRCodeReader._stream = stream;
QRCodeReader.video.src = window.URL.createObjectURL(stream) || stream;
setTimeout(QRCodeReader.canvasInit,250); // Needed to get videoWidth/videoHeight
}, error);
} else if (hasFlash) {
function initCanvas(ww,hh) {
gCanvas = document.getElementById("qr-canvas");
var w = ww;
var h = hh;
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);
imageData = gCtx.getImageData(0,0,320,240);
}
function makeFlash(path) {
return $('<embed style="z-index:10;" allowScriptAccess="always" id="embedflash" src="'+path+'camcanvas.swf" quality="high" width="1" height="1" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" mayscript="true" />');
}
function makeCanvas() {
return $('<canvas style="z-index:-1;width:800px;height:600px; display:none;" id="qr-canvas" width="800" height="600"></canvas>');
}
QRCodeReader.reader_container.html(makeFlash(resource + 'wallet/').width(320).height(240));
QRCodeReader.reader_container.append(makeCanvas());
initCanvas(800,600);
QRCodeReader.interval = setInterval(captureToCanvas, 1000);
} else {
error('Sorry your browser is not supported. Please try Firefox, Chrome or safari.');
}
qrcode.callback = function(data) { if (data) { QRCodeReader.stop(); success(data); } };
});
}
};