Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a webworker and requestAnimationFrame #38

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ module.exports = function(grunt) {
"src/jsqrcode/src/findpat.js",
"src/jsqrcode/src/alignpat.js",
"src/jsqrcode/src/databr.js",
"src/qrcode_decodeImageData.js",
"src/html5-qrcode_webworker.js",
],
'lib/html5-qrcode.min.js': ['src/html5-qrcode.js']
},
Expand Down
2 changes: 1 addition & 1 deletion lib/html5-qrcode.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/jsqrcode-combined.min.js

Large diffs are not rendered by default.

46 changes: 27 additions & 19 deletions src/html5-qrcode.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
(function($) {
(function($) {
jQuery.fn.extend({
html5_qrcode: function(qrcodeSuccess, qrcodeError, videoError) {
return this.each(function() {
var currentElem = $(this);


var worker = $.data(currentElem[0], "worker") || new Worker('jsqrcode-combined.min.js');
$.data(currentElem[0], "worker", worker);

var height = currentElem.height();
var width = currentElem.width();

Expand All @@ -26,30 +29,28 @@
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));

worker.postMessage(context.getImageData(0, 0, width, height));
} else {
$.data(currentElem[0], "timeout", setTimeout(scan, 500));
requestAnimationFrame(scan);
}
};//end snapshot function

window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;

window.requestAnimationFrame =
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 300);
};
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));
requestAnimationFrame(scan);
};

// Call the getUserMedia method with our callback functions
Expand All @@ -61,10 +62,17 @@
console.log('Native web camera streaming (getUserMedia) not supported in this browser.');
// Display a friendly "sorry" message to the user
}

qrcode.callback = function (result) {
qrcodeSuccess(result, localMediaStream);
};

worker.addEventListener('message', function(e) {
var data = e.data;
if(data.indexOf("data:") === 0){
qrcodeSuccess(data.substring(5), localMediaStream);
}
else if (data.indexOf("error:") === 0){
qrcodeError(data.substring(6), localMediaStream);
}
requestAnimationFrame(scan);
}, false);
}); // end of html5_qrcode
},
html5_qrcode_stop: function() {
Expand Down
14 changes: 14 additions & 0 deletions src/html5-qrcode_webworker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(function($) {
qrcode.callback = function (result) {
self.postMessage("data:" + result);
};
self.addEventListener('message', function(e) {
var data = e.data;
var result = "";
try {
qrcode.decodeImageData(data);
} catch (e) {
self.postMessage("error:" + e);
}
});
})();
9 changes: 9 additions & 0 deletions src/qrcode_decodeImageData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
qrcode.decodeImageData = function(data){
qrcode.width = data.width;
qrcode.height = data.height;
qrcode.imagedata = data;
qrcode.result = qrcode.process();
if(qrcode.callback!=null)
qrcode.callback(qrcode.result);
return qrcode.result;
};