Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring a tad for barcode detection API.
+ 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
1 parent
3985440
commit 21afa9a
Showing
2 changed files
with
34 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}; | ||
}; |