pull/319/merge
Adam Dobrawy 6 years ago committed by GitHub
commit 8f0db6db71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

754
dist/quagga.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

10421
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -14,7 +14,8 @@ const vec2 = {
clone: require('gl-vec2/clone') clone: require('gl-vec2/clone')
}; };
var _inputStream, exports.default = function() {
var _inputStream,
_framegrabber, _framegrabber,
_stopped, _stopped,
_canvasContainer = { _canvasContainer = {
@ -35,12 +36,12 @@ var _inputStream,
_resultCollector, _resultCollector,
_config = {}; _config = {};
function initializeData(imageWrapper) { function initializeData(imageWrapper) {
initBuffers(imageWrapper); initBuffers(imageWrapper);
_decoder = BarcodeDecoder.create(_config.decoder, _inputImageWrapper); _decoder = BarcodeDecoder.create(_config.decoder, _inputImageWrapper);
} }
function initInputStream(cb) { function initInputStream(cb) {
var video; var video;
if (_config.inputStream.type === "VideoStream") { if (_config.inputStream.type === "VideoStream") {
video = document.createElement("video"); video = document.createElement("video");
@ -68,9 +69,9 @@ function initInputStream(cb) {
_inputStream.setAttribute("preload", "auto"); _inputStream.setAttribute("preload", "auto");
_inputStream.setInputStream(_config.inputStream); _inputStream.setInputStream(_config.inputStream);
_inputStream.addEventListener("canrecord", canRecord.bind(undefined, cb)); _inputStream.addEventListener("canrecord", canRecord.bind(undefined, cb));
} }
function getViewPort() { function getViewPort() {
var target = _config.inputStream.target; var target = _config.inputStream.target;
// Check if target is already a DOM element // Check if target is already a DOM element
if (target && target.nodeName && target.nodeType === 1) { if (target && target.nodeName && target.nodeType === 1) {
@ -80,27 +81,27 @@ function getViewPort() {
var selector = typeof target === 'string' ? target : '#interactive.viewport'; var selector = typeof target === 'string' ? target : '#interactive.viewport';
return document.querySelector(selector); return document.querySelector(selector);
} }
} }
function canRecord(cb) { function canRecord(cb) {
BarcodeLocator.checkImageConstraints(_inputStream, _config.locator); BarcodeLocator.checkImageConstraints(_inputStream, _config.locator);
initCanvas(_config); initCanvas(_config);
_framegrabber = FrameGrabber.create(_inputStream, _canvasContainer.dom.image); _framegrabber = FrameGrabber.create(_inputStream, _canvasContainer.dom.image);
adjustWorkerPool(_config.numOfWorkers, function() { adjustWorkerPool(_config.numOfWorkers, function () {
if (_config.numOfWorkers === 0) { if (_config.numOfWorkers === 0) {
initializeData(); initializeData();
} }
ready(cb); ready(cb);
}); });
} }
function ready(cb){ function ready(cb) {
_inputStream.play(); _inputStream.play();
cb(); cb();
} }
function initCanvas() { function initCanvas() {
if (typeof document !== "undefined") { if (typeof document !== "undefined") {
var $viewport = getViewPort(); var $viewport = getViewPort();
_canvasContainer.dom.image = document.querySelector("canvas.imgBuffer"); _canvasContainer.dom.image = document.querySelector("canvas.imgBuffer");
@ -132,9 +133,9 @@ function initCanvas() {
_canvasContainer.dom.overlay.width = _inputStream.getCanvasSize().x; _canvasContainer.dom.overlay.width = _inputStream.getCanvasSize().x;
_canvasContainer.dom.overlay.height = _inputStream.getCanvasSize().y; _canvasContainer.dom.overlay.height = _inputStream.getCanvasSize().y;
} }
} }
function initBuffers(imageWrapper) { function initBuffers(imageWrapper) {
if (imageWrapper) { if (imageWrapper) {
_inputImageWrapper = imageWrapper; _inputImageWrapper = imageWrapper;
} else { } else {
@ -154,9 +155,9 @@ function initBuffers(imageWrapper) {
vec2.clone([_inputImageWrapper.size.x, 0]) vec2.clone([_inputImageWrapper.size.x, 0])
]; ];
BarcodeLocator.init(_inputImageWrapper, _config.locator); BarcodeLocator.init(_inputImageWrapper, _config.locator);
} }
function getBoundingBoxes() { function getBoundingBoxes() {
if (_config.locate) { if (_config.locate) {
return BarcodeLocator.locate(); return BarcodeLocator.locate();
} else { } else {
@ -166,9 +167,9 @@ function getBoundingBoxes() {
vec2.clone(_boxSize[2]), vec2.clone(_boxSize[2]),
vec2.clone(_boxSize[3])]]; vec2.clone(_boxSize[3])]];
} }
} }
function transformResult(result) { function transformResult(result) {
var topRight = _inputStream.getTopRight(), var topRight = _inputStream.getTopRight(),
xOffset = topRight.x, xOffset = topRight.x,
yOffset = topRight.y, yOffset = topRight.y,
@ -213,9 +214,9 @@ function transformResult(result) {
line[1].x += xOffset; line[1].x += xOffset;
line[1].y += yOffset; line[1].y += yOffset;
} }
} }
function addResult (result, imageData) { function addResult(result, imageData) {
if (!imageData || !_resultCollector) { if (!imageData || !_resultCollector) {
return; return;
} }
@ -226,15 +227,15 @@ function addResult (result, imageData) {
} else if (result.codeResult) { } else if (result.codeResult) {
_resultCollector.addResult(imageData, _inputStream.getCanvasSize(), result.codeResult); _resultCollector.addResult(imageData, _inputStream.getCanvasSize(), result.codeResult);
} }
} }
function hasCodeResult (result) { function hasCodeResult(result) {
return result && (result.barcodes ? return result && (result.barcodes ?
result.barcodes.some(barcode => barcode.codeResult) : result.barcodes.some(barcode => barcode.codeResult) :
result.codeResult); result.codeResult);
} }
function publishResult(result, imageData) { function publishResult(result, imageData) {
let resultToPublish = result; let resultToPublish = result;
if (result && _onUIThread) { if (result && _onUIThread) {
@ -247,9 +248,9 @@ function publishResult(result, imageData) {
if (hasCodeResult(result)) { if (hasCodeResult(result)) {
Events.publish("detected", resultToPublish); Events.publish("detected", resultToPublish);
} }
} }
function locateAndDecode() { function locateAndDecode() {
var result, var result,
boxes; boxes;
@ -262,14 +263,14 @@ function locateAndDecode() {
} else { } else {
publishResult(); publishResult();
} }
} }
function update() { function update() {
var availableWorker; var availableWorker;
if (_onUIThread) { if (_onUIThread) {
if (_workerPool.length > 0) { if (_workerPool.length > 0) {
availableWorker = _workerPool.filter(function(workerThread) { availableWorker = _workerPool.filter(function (workerThread) {
return !workerThread.busy; return !workerThread.busy;
})[0]; })[0];
if (availableWorker) { if (availableWorker) {
@ -294,9 +295,9 @@ function update() {
} else { } else {
locateAndDecode(); locateAndDecode();
} }
} }
function startContinuousUpdate() { function startContinuousUpdate() {
var next = null, var next = null,
delay = 1000 / (_config.frequency || 60); delay = 1000 / (_config.frequency || 60);
@ -311,17 +312,17 @@ function startContinuousUpdate() {
window.requestAnimFrame(frame); window.requestAnimFrame(frame);
} }
}(performance.now())); }(performance.now()));
} }
function start() { function start() {
if (_onUIThread && _config.inputStream.type === "LiveStream") { if (_onUIThread && _config.inputStream.type === "LiveStream") {
startContinuousUpdate(); startContinuousUpdate();
} else { } else {
update(); update();
} }
} }
function initWorker(cb) { function initWorker(cb) {
var blobURL, var blobURL,
workerThread = { workerThread = {
worker: undefined, worker: undefined,
@ -332,7 +333,7 @@ function initWorker(cb) {
blobURL = generateWorkerBlob(); blobURL = generateWorkerBlob();
workerThread.worker = new Worker(blobURL); workerThread.worker = new Worker(blobURL);
workerThread.worker.onmessage = function(e) { workerThread.worker.onmessage = function (e) {
if (e.data.event === 'initialized') { if (e.data.event === 'initialized') {
URL.revokeObjectURL(blobURL); URL.revokeObjectURL(blobURL);
workerThread.busy = false; workerThread.busy = false;
@ -358,9 +359,9 @@ function initWorker(cb) {
imageData: workerThread.imageData, imageData: workerThread.imageData,
config: configForWorker(_config) config: configForWorker(_config)
}, [workerThread.imageData.buffer]); }, [workerThread.imageData.buffer]);
} }
function configForWorker(config) { function configForWorker(config) {
return { return {
...config, ...config,
inputStream: { inputStream: {
@ -368,9 +369,9 @@ function configForWorker(config) {
target: null target: null
} }
}; };
} }
function workerInterface(factory) { function workerInterface(factory) {
/* eslint-disable no-undef*/ /* eslint-disable no-undef*/
if (factory) { if (factory) {
var Quagga = factory().default; var Quagga = factory().default;
@ -381,7 +382,7 @@ function workerInterface(factory) {
} }
var imageWrapper; var imageWrapper;
self.onmessage = function(e) { self.onmessage = function (e) {
if (e.data.cmd === 'init') { if (e.data.cmd === 'init') {
var config = e.data.config; var config = e.data.config;
config.numOfWorkers = 0; config.numOfWorkers = 0;
@ -412,9 +413,9 @@ function workerInterface(factory) {
} }
/* eslint-enable */ /* eslint-enable */
} }
function generateWorkerBlob() { function generateWorkerBlob() {
var blob, var blob,
factorySource; factorySource;
@ -428,26 +429,26 @@ function generateWorkerBlob() {
{type: 'text/javascript'}); {type: 'text/javascript'});
return window.URL.createObjectURL(blob); return window.URL.createObjectURL(blob);
} }
function setReaders(readers) { function setReaders(readers) {
if (_decoder) { if (_decoder) {
_decoder.setReaders(readers); _decoder.setReaders(readers);
} else if (_onUIThread && _workerPool.length > 0) { } else if (_onUIThread && _workerPool.length > 0) {
_workerPool.forEach(function(workerThread) { _workerPool.forEach(function (workerThread) {
workerThread.worker.postMessage({cmd: 'setReaders', readers: readers}); workerThread.worker.postMessage({cmd: 'setReaders', readers: readers});
}); });
} }
} }
function adjustWorkerPool(capacity, cb) { function adjustWorkerPool(capacity, cb) {
const increaseBy = capacity - _workerPool.length; const increaseBy = capacity - _workerPool.length;
if (increaseBy === 0) { if (increaseBy === 0) {
return cb && cb(); return cb && cb();
} }
if (increaseBy < 0) { if (increaseBy < 0) {
const workersToTerminate = _workerPool.slice(increaseBy); const workersToTerminate = _workerPool.slice(increaseBy);
workersToTerminate.forEach(function(workerThread) { workersToTerminate.forEach(function (workerThread) {
workerThread.worker.terminate(); workerThread.worker.terminate();
if (ENV.development) { if (ENV.development) {
console.log("Worker terminated!"); console.log("Worker terminated!");
@ -462,15 +463,15 @@ function adjustWorkerPool(capacity, cb) {
function workerInitialized(workerThread) { function workerInitialized(workerThread) {
_workerPool.push(workerThread); _workerPool.push(workerThread);
if (_workerPool.length >= capacity){ if (_workerPool.length >= capacity) {
cb && cb(); cb && cb();
} }
} }
} }
} }
export default { return {
init: function(config, cb, imageWrapper) { init: function (config, cb, imageWrapper) {
_config = merge({}, Config, config); _config = merge({}, Config, config);
if (imageWrapper) { if (imageWrapper) {
_onUIThread = false; _onUIThread = false;
@ -480,10 +481,10 @@ export default {
initInputStream(cb); initInputStream(cb);
} }
}, },
start: function() { start: function () {
start(); start();
}, },
stop: function() { stop: function () {
_stopped = true; _stopped = true;
adjustWorkerPool(0); adjustWorkerPool(0);
if (_config.inputStream.type === "LiveStream") { if (_config.inputStream.type === "LiveStream") {
@ -491,31 +492,31 @@ export default {
_inputStream.clearEventHandlers(); _inputStream.clearEventHandlers();
} }
}, },
pause: function() { pause: function () {
_stopped = true; _stopped = true;
}, },
onDetected: function(callback) { onDetected: function (callback) {
Events.subscribe("detected", callback); Events.subscribe("detected", callback);
}, },
offDetected: function(callback) { offDetected: function (callback) {
Events.unsubscribe("detected", callback); Events.unsubscribe("detected", callback);
}, },
onProcessed: function(callback) { onProcessed: function (callback) {
Events.subscribe("processed", callback); Events.subscribe("processed", callback);
}, },
offProcessed: function(callback) { offProcessed: function (callback) {
Events.unsubscribe("processed", callback); Events.unsubscribe("processed", callback);
}, },
setReaders: function(readers) { setReaders: function (readers) {
setReaders(readers); setReaders(readers);
}, },
registerResultCollector: function(resultCollector) { registerResultCollector: function (resultCollector) {
if (resultCollector && typeof resultCollector.addResult === 'function') { if (resultCollector && typeof resultCollector.addResult === 'function') {
_resultCollector = resultCollector; _resultCollector = resultCollector;
} }
}, },
canvas: _canvasContainer, canvas: _canvasContainer,
decodeSingle: function(config, resultCallback) { decodeSingle: function (config, resultCallback) {
config = merge({ config = merge({
inputStream: { inputStream: {
type: "ImageStream", type: "ImageStream",
@ -540,4 +541,5 @@ export default {
ImageDebug: ImageDebug, ImageDebug: ImageDebug,
ResultCollector: ResultCollector, ResultCollector: ResultCollector,
CameraAccess: CameraAccess, CameraAccess: CameraAccess,
}; };
}
Loading…
Cancel
Save