-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhtml5-qrcode.js
66 lines (52 loc) · 2.19 KB
/
html5-qrcode.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
(function( $ ){
jQuery.fn.extend({
html5_qrcode: function(qrcodeSuccess, qrcodeError, videoError) {
return this.each(function() {
var currentElem = $( this );
var height = currentElem.height();
var width = currentElem.width();
if (height == null) {
height = 250;
}
if (width == null) {
width = 300;
}
var vidElem = $('<video width="' + width + 'px" height="' + height + 'px"></video>').appendTo(currentElem);
var canvasElem = $('<canvas id="qr-canvas" width="' + (width - 2) + 'px" height="' + (height - 2) + 'px" style="display:none;"></canvas>').appendTo(currentElem);
var video = vidElem[0];
var canvas = canvasElem[0];
var context = canvas.getContext('2d');
var localMediaStream;
var scan = function() {
if (localMediaStream) {
context.drawImage(video, 0, 0, 307,250);
try {
qrcode.decode();
} catch(e) {
qrcodeError(e);
}
setTimeout(scan, 500);
} else {
setTimeout(scan,500);
}
}//end snapshot function
window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
var successCallback = function(stream) {
video.src = (window.URL && window.URL.createObjectURL(stream)) || stream;
localMediaStream = stream;
video.play();
setTimeout(scan,1000);
}
// Call the getUserMedia method with our callback functions
if (navigator.getUserMedia) {
navigator.getUserMedia({video: true}, successCallback, videoError);
} else {
console.log('Native web camera streaming (getUserMedia) not supported in this browser.');
// Display a friendly "sorry" message to the user
}
qrcode.callback = qrcodeSuccess;
}); // end of html5_qrcode
}
});
})( jQuery );