forked from mebjas/html5-qrcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html5-qrcode.js
153 lines (129 loc) · 6.09 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
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
(function($) {
var cameraIds = [];
/**
* Code to initialise the cameraIds field
*/
function gotSources(sourceInfos) {
for (var i = 0; i !== sourceInfos.length; ++i) {
var sourceInfo = sourceInfos[i];
if (sourceInfo.kind === 'video') {
cameraIds.push(sourceInfo.id);
}
}
}
if (typeof MediaStreamTrack === 'undefined' ||
typeof MediaStreamTrack.getSources === 'undefined') {
console.log('This browser does not support MediaStreamTrack.\n\nTry Chrome.');
} else {
MediaStreamTrack.getSources(gotSources);
}
jQuery.fn.extend({
/**
* jQuery method,
* @param: qrcodeSuccess (function) - callback on success
* @param: qrcodeError (function) - callback on qr error
* @param: videoError (function) - callback on video error
* @param: camera (int) - which camera to use
*/
html5_qrcode: function(qrcodeSuccess, qrcodeError, videoError, camera) {
return this.each(function() {
var currentElem = $(this);
$.data(currentElem[0], "qrcodeSuccess", qrcodeSuccess);
$.data(currentElem[0], "qrcodeError", qrcodeError);
$.data(currentElem[0], "videoError", videoError);
if (typeof camera != 'undefined' && typeof cameraIds[camera] != 'undefined')
$.data(currentElem[0], "sourceId", camera);
else $.data(currentElem[0], "sourceId", 0);
if (typeof cameraIds[currentElem.data('sourceId')] != 'undefined')
$.data(currentElem[0], "cameraId", cameraIds[currentElem.data('sourceId')] );
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, localMediaStream);
}
$.data(currentElem[0], "timeout", setTimeout(scan, 500));
} else {
$.data(currentElem[0], "timeout", 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;
$.data(currentElem[0], "stream", stream);
video.play();
$.data(currentElem[0], "timeout", setTimeout(scan, 1000));
};
// Call the getUserMedia method with our callback functions
if (navigator.getUserMedia) {
var config = {video: true};
if (typeof currentElem.data("cameraId") != 'undefined') {
config = {
video: {
optional: [{
sourceId: currentElem.data("cameraId")
}]
}
};
}
navigator.getUserMedia(config, successCallback, function(error) {
if (typeof videoError == 'function') videoError(error, localMediaStream);
else console.log('Error callback is undefined or not a function.');
});
} else {
console.log('Native web camera streaming (getUserMedia) not supported in this browser.');
// Display a friendly "sorry" message to the user
}
qrcode.callback = function (result) {
if (typeof qrcodeSuccess == 'function')
qrcodeSuccess(result, localMediaStream);
else console.log('Success callback is undefined or not a function.');
};
}); // end of html5_qrcode
},
html5_qrcode_stop: function() {
return this.each(function() {
//stop the stream and cancel timeouts
$(this).data('stream').getVideoTracks().forEach(function(videoTrack) {
videoTrack.stop();
});
$(this).children('video').remove();
$(this).children('canvas').remove();
clearTimeout($(this).data('timeout'));
});
},
html5_qrcode_changeCamera: function() {
return this.each(function() {
//stop the stream and cancel timeouts
$(this).html5_qrcode_stop();
$(this).html5_qrcode(
$(this).data('qrcodeSuccess'),
$(this).data('qrcodeError'),
$(this).data('videoError'),
($(this).data('sourceId') + 1) % cameraIds.length
);
});
},
html5_qrcode_cameraCount: function() {
return cameraIds.length;
}
});
})(jQuery);