Skip to content

Commit

Permalink
Refactoring a tad for barcode detection API.
Browse files Browse the repository at this point in the history
 + moves the canvas closer to the worker.
 + in future can move canvas to offscreen canvas and save pulling out all the imagedata on ui thread.
  • Loading branch information
PaulKinlan committed Dec 3, 2016
1 parent 3985440 commit 21afa9a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
13 changes: 5 additions & 8 deletions app/scripts/main.js
Expand Up @@ -27,11 +27,11 @@
var qrCodeManager = new QRCodeManager('qrcode');
var processingFrame = false;

cameraManager.onframe = function(imageData) {
cameraManager.onframe = function(canvas) {
// There is a frame in the camera, what should we do with it?
if(processingFrame == false) {
processingFrame = true;
var detectedQRCode = qrCodeManager.detectQRCode(imageData, function(url) {
var detectedQRCode = qrCodeManager.detectQRCode(canvas, function(url) {
if(url !== undefined) {
if(ga) { ga('send', 'event', 'urlfound'); }

Expand Down Expand Up @@ -119,10 +119,10 @@
qrcodeShare.classList.remove('hidden');
}

this.detectQRCode = function(imageData, callback) {
this.detectQRCode = function(canvas, callback) {
callback = callback || function() {};

client.decode(imageData, function(result) {
client.decode(canvas, function(result) {
if(result !== undefined) {
self.currentUrl = result;
}
Expand Down Expand Up @@ -445,10 +445,7 @@

drawOverlay(wWidth, wHeight);

// A frame has been captured.
var imageData = canvas.getImageData(0, 0, dWidth, dHeight);

if(self.onframe) self.onframe(imageData);
if(self.onframe) self.onframe(canvas);

coordinatesHaveChanged = false;
};
Expand Down
41 changes: 29 additions & 12 deletions app/scripts/qrclient.js
@@ -1,15 +1,32 @@
var QRClient = function() {
var worker = new Worker('/scripts/jsqrcode/qrworker.js');
var currentCallback;

this.decode = function(imageData, callback) {
worker.postMessage(imageData);
currentCallback = callback;
};
var worker = new Worker('/scripts/jsqrcode/qrworker.js');
var barcodeDetector;
if(BarcodeDetector)
barcodeDetector = new BarcodeDetector();

var currentCallback;

worker.onmessage = function(e) {
if(currentCallback) {
currentCallback(e.data);
}
};
this.decode = function(context, callback) {
// Temporary hack because
if(barcodeDetector) {
barcodeDetector.detect(context.canvas)
.then(barcodes => {
// return the first barcode.
callback(barcodes[0].rawValue);
})
.catch(err => console.log(err));
}
else {
// A frame has been captured.
var imageData = context.getImageData(0, 0, dWidth, dHeight);
worker.postMessage(imageData);
currentCallback = callback;
}
};

worker.onmessage = function(e) {
if(currentCallback) {
currentCallback(e.data);
}
};
};

0 comments on commit 21afa9a

Please sign in to comment.