-
Notifications
You must be signed in to change notification settings - Fork 1
/
qr-scanner.js
153 lines (136 loc) · 3.82 KB
/
qr-scanner.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
Meteor.qrScanner = new function() {
var context = null;
var video = null;
var intervalID = null;
var isReady = false;
var camera = null;
var videoStream = null;
var options = {
"width": 1280,
"height": 720,
"done": function(result) {
console.log(result);
},
"fail": function(ignore) {return;}
};
var initContext = function(canvas) {
canvas.width = options.width;
canvas.height = options.height;
context = canvas.getContext('2d');
}
var initCamera = function() {
window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
navigator.mediaDevices = navigator.mediaDevices || (navigator.getUserMedia? {
getUserMedia: function(cfg) {
return new Promise(function(resolve, reject) {
navigator.getUserMedia.call(navigator, cfg, resolve, reject);
});
},
enumerateDevices: function() {
return new Promise(function(resolve, reject) {
MediaStreamTrack.getSources.call(navigator, resolve, reject);
});
}
} : undefined);
camera = chooseCamera();
}
var chooseCamera = function() {
navigator.mediaDevices.enumerateDevices().then(function(sources) {
for (var i = 0; sources.length; ++i) {
if (sources[i].kind == "videoinput" || (sources[i].kind == "video" && (sources[i].facing == "" || sources[i].facing == "environment"))) {
return sources[i].id;
}
}
});
}
var errorCallback = function(error) {
console.error('An error occurred: [CODE ' + error.code + ']');
console.log(error);
}
var successCallback = function(stream) {
if (video.mozSrcObject !== undefined) {
video.mozSrcObject = stream;
} else {
video.src = (window.URL && window.URL.createObjectURL(stream)) || stream;
}
videoStream = stream;
video.onloadedmetadata = function() {
video.play();
}
intervalID = setInterval(captureToCanvas, 500);
}
var captureToCanvas = function() {
context.drawImage(video, 0, 0, options.width, options.height);
try {
var result = qrcode.decode({"canvas":context.canvas});
Meteor.qrScanner.stop();
options.done(result);
} catch(error) {
options.fail(error);
}
}
var setOptions = function(_options) {
for (var key in _options) {
options[key] = _options[key];
}
}
this.init = function(_options) {
var canvas = document.createElement('canvas');
initCamera();
if (!navigator.mediaDevices) {
document.getElementById('stremingIsNotSupported').style.display = 'block';
console.error('Native web camera streaming (getUserMedia) not supported in this browser.');
return;
}
else if (!canvas.getContext || !canvas.getContext('2d')) {
document.getElementById('canvasIsNotSupported').style.display = 'block';
console.error("Canvas is not supported");
return;
}
if (_options) {
setOptions(_options);
}
video = document.getElementById('qr-scanner-video');
initContext(canvas);
isReady = true;
}
this.scan = function() {
if (isReady) {
navigator.mediaDevices.getUserMedia({
"video": {
"mandatory": {
"maxWidth": options.width,
"maxHeight": options.height,
},
"optional": [{
"sourceId": camera
}]
},
"audio": false
}).then(successCallback).catch(errorCallback);
}
}
this.stop = function() {
clearInterval(intervalID);
video.pause();
if (video.mozSrcObject !== undefined) {
video.mozSrcObject = null;
} else {
video.src = null;
}
if (videoStream) {
var tracks = videoStream.getTracks();
for (var i = 0; i < tracks.length; ++i) {
tracks[i].stop();
}
}
context.clearRect(0, 0, options.width, options.height);
}
}
Template.qrScanner.rendered = function() {
Meteor.qrScanner.init();
}
Template.qrScanner.destroyed = function() {
Meteor.qrScanner.stop();
}