mirror of
https://github.com/serratus/quaggaJS.git
synced 2026-08-13 05:31:37 +08:00
Moved remaining code to instances
This commit is contained in:
committed by
Christoph Oberhofer
parent
b733c2b7fd
commit
633eabe86c
@@ -1,4 +1,4 @@
|
||||
export default (function() {
|
||||
export default function createEventedElement() {
|
||||
var events = {};
|
||||
|
||||
function getEvent(eventName) {
|
||||
@@ -79,4 +79,4 @@ export default (function() {
|
||||
}
|
||||
}
|
||||
};
|
||||
})();
|
||||
};
|
||||
|
||||
@@ -24,9 +24,10 @@ const mat2 = {
|
||||
copy: require('gl-mat2/copy'),
|
||||
create: require('gl-mat2/create'),
|
||||
invert: require('gl-mat2/invert')
|
||||
}
|
||||
};
|
||||
|
||||
var _config,
|
||||
export default function createLocator(inputImageWrapper, config) {
|
||||
var _config = config,
|
||||
_currentImageWrapper,
|
||||
_skelImageWrapper,
|
||||
_subImageWrapper,
|
||||
@@ -45,10 +46,13 @@ var _config,
|
||||
}
|
||||
},
|
||||
_numPatches = {x: 0, y: 0},
|
||||
_inputImageWrapper,
|
||||
_inputImageWrapper = inputImageWrapper,
|
||||
_skeletonizer;
|
||||
|
||||
function initBuffers() {
|
||||
initBuffers();
|
||||
initCanvas();
|
||||
|
||||
function initBuffers() {
|
||||
var skeletonImageData;
|
||||
|
||||
if (_config.halfSample) {
|
||||
@@ -85,9 +89,9 @@ function initBuffers() {
|
||||
}, undefined, Array, true);
|
||||
_patchGrid = new ImageWrapper(_imageToPatchGrid.size, undefined, undefined, true);
|
||||
_patchLabelGrid = new ImageWrapper(_imageToPatchGrid.size, undefined, Int32Array, true);
|
||||
}
|
||||
}
|
||||
|
||||
function initCanvas() {
|
||||
function initCanvas() {
|
||||
if (_config.useWorker || typeof document === 'undefined') {
|
||||
return;
|
||||
}
|
||||
@@ -99,13 +103,13 @@ function initCanvas() {
|
||||
_canvasContainer.ctx.binary = _canvasContainer.dom.binary.getContext("2d");
|
||||
_canvasContainer.dom.binary.width = _binaryImageWrapper.size.x;
|
||||
_canvasContainer.dom.binary.height = _binaryImageWrapper.size.y;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Creates a bounding box which encloses all the given patches
|
||||
* @returns {Array} The minimal bounding box
|
||||
*/
|
||||
function boxFromPatches(patches) {
|
||||
function boxFromPatches(patches) {
|
||||
var overAvg,
|
||||
i,
|
||||
j,
|
||||
@@ -191,24 +195,24 @@ function boxFromPatches(patches) {
|
||||
}
|
||||
|
||||
return box;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Creates a binary image of the current image
|
||||
*/
|
||||
function binarizeImage() {
|
||||
function binarizeImage() {
|
||||
otsuThreshold(_currentImageWrapper, _binaryImageWrapper);
|
||||
_binaryImageWrapper.zeroBorder();
|
||||
if (ENV.development && _config.debug.showCanvas) {
|
||||
_binaryImageWrapper.show(_canvasContainer.dom.binary, 255);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Iterate over the entire image
|
||||
* extract patches
|
||||
*/
|
||||
function findPatches() {
|
||||
function findPatches() {
|
||||
var i,
|
||||
j,
|
||||
x,
|
||||
@@ -254,14 +258,14 @@ function findPatches() {
|
||||
}
|
||||
|
||||
return patchesFound;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Finds those connected areas which contain at least 6 patches
|
||||
* and returns them ordered DESC by the number of contained patches
|
||||
* @param {Number} maxLabel
|
||||
*/
|
||||
function findBiggestConnectedAreas(maxLabel){
|
||||
function findBiggestConnectedAreas(maxLabel){
|
||||
var i,
|
||||
sum,
|
||||
labelHist = [],
|
||||
@@ -294,12 +298,12 @@ function findBiggestConnectedAreas(maxLabel){
|
||||
});
|
||||
|
||||
return topLabels;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
*
|
||||
*/
|
||||
function findBoxes(topLabels, maxLabel) {
|
||||
function findBoxes(topLabels, maxLabel) {
|
||||
var i,
|
||||
j,
|
||||
sum,
|
||||
@@ -336,13 +340,13 @@ function findBoxes(topLabels, maxLabel) {
|
||||
}
|
||||
}
|
||||
return boxes;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Find similar moments (via cluster)
|
||||
* @param {Object} moments
|
||||
*/
|
||||
function similarMoments(moments) {
|
||||
function similarMoments(moments) {
|
||||
var clusters = cluster(moments, 0.90);
|
||||
var topCluster = topGeneric(clusters, 1, function(e) {
|
||||
return e.getPoints().length;
|
||||
@@ -355,9 +359,9 @@ function similarMoments(moments) {
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
function skeletonize(x, y) {
|
||||
function skeletonize(x, y) {
|
||||
_binaryImageWrapper.subImageAsCopy(_subImageWrapper, imageRef(x, y));
|
||||
_skeletonizer.skeletonize();
|
||||
|
||||
@@ -365,9 +369,9 @@ function skeletonize(x, y) {
|
||||
if (ENV.development && _config.debug.showSkeleton) {
|
||||
_skelImageWrapper.overlay(_canvasContainer.dom.binary, 360, imageRef(x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Extracts and describes those patches which seem to contain a barcode pattern
|
||||
* @param {Array} moments
|
||||
* @param {Object} patchPos,
|
||||
@@ -375,7 +379,7 @@ function skeletonize(x, y) {
|
||||
* @param {Number} y
|
||||
* @returns {Array} list of patches
|
||||
*/
|
||||
function describePatch(moments, patchPos, x, y) {
|
||||
function describePatch(moments, patchPos, x, y) {
|
||||
var k,
|
||||
avg,
|
||||
eligibleMoments = [],
|
||||
@@ -428,13 +432,13 @@ function describePatch(moments, patchPos, x, y) {
|
||||
}
|
||||
}
|
||||
return patchesFound;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* finds patches which are connected and share the same orientation
|
||||
* @param {Object} patchesFound
|
||||
*/
|
||||
function rasterizeAngularSimilarity(patchesFound) {
|
||||
function rasterizeAngularSimilarity(patchesFound) {
|
||||
var label = 0,
|
||||
threshold = 0.95,
|
||||
currIdx = 0,
|
||||
@@ -523,17 +527,9 @@ function rasterizeAngularSimilarity(patchesFound) {
|
||||
}
|
||||
|
||||
return label;
|
||||
}
|
||||
|
||||
export default {
|
||||
init: function(inputImageWrapper, config) {
|
||||
_config = config;
|
||||
_inputImageWrapper = inputImageWrapper;
|
||||
|
||||
initBuffers();
|
||||
initCanvas();
|
||||
},
|
||||
}
|
||||
|
||||
return {
|
||||
locate: function() {
|
||||
var patchesFound,
|
||||
topLabels,
|
||||
@@ -564,9 +560,10 @@ export default {
|
||||
|
||||
boxes = findBoxes(topLabels, maxLabel);
|
||||
return boxes;
|
||||
},
|
||||
|
||||
checkImageConstraints: function(inputStream, config) {
|
||||
}
|
||||
}
|
||||
}
|
||||
export function checkImageConstraints(inputStream, config) {
|
||||
var patchSize,
|
||||
width = inputStream.getWidth(),
|
||||
height = inputStream.getHeight(),
|
||||
@@ -603,5 +600,4 @@ export default {
|
||||
throw new Error("Image dimensions do not comply with the current settings: Width (" +
|
||||
width + " )and height (" + height +
|
||||
") must a multiple of " + patchSize.x);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
+2
-31
@@ -2,7 +2,6 @@ import TypeDefs from './common/typedefs'; // eslint-disable-line no-unused-vars
|
||||
import WebrtcAdapter from 'webrtc-adapter'; // eslint-disable-line no-unused-vars
|
||||
import createScanner from './scanner';
|
||||
import ImageWrapper from './common/image_wrapper';
|
||||
import Events from './common/events';
|
||||
import ImageDebug from './common/image_debug';
|
||||
import ResultCollector from './analytics/result_collector';
|
||||
import Config from './config/config';
|
||||
@@ -31,29 +30,19 @@ function fromImage(config, imageSrc, imageConfig) {
|
||||
const scanner = createScanner(config);
|
||||
return {
|
||||
addEventListener: (eventType, cb) => {
|
||||
scanner.init(config, () => {
|
||||
Events.once(eventType, (result) => {
|
||||
scanner.stop();
|
||||
cb(result);
|
||||
}, true);
|
||||
scanner.start();
|
||||
});
|
||||
scanner.decodeSingle(config, cb);
|
||||
},
|
||||
removeEventListener(cb) {
|
||||
console.log("Remove listener");
|
||||
},
|
||||
toPromise() {
|
||||
return new Promise((resolve, reject) => {
|
||||
scanner.init(config, () => {
|
||||
Events.once('processed', (result) => {
|
||||
scanner.stop();
|
||||
scanner.decodeSingle(config, (result) => {
|
||||
if (result.codeResult && result.codeResult.code) {
|
||||
return resolve(result);
|
||||
}
|
||||
return reject(result);
|
||||
});
|
||||
scanner.start();
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -117,9 +106,6 @@ function createApi(configuration = Config) {
|
||||
config(conf) {
|
||||
return createApi(merge({}, configuration, conf));
|
||||
},
|
||||
init: function(config, cb, imageWrapper) {
|
||||
defaultScanner.init(config, cb, imageWrapper);
|
||||
},
|
||||
start: function() {
|
||||
defaultScanner.start();
|
||||
},
|
||||
@@ -129,24 +115,9 @@ function createApi(configuration = Config) {
|
||||
pause: function() {
|
||||
defaultScanner.pause();
|
||||
},
|
||||
onDetected: function(callback) {
|
||||
defaultScanner.onDetected(callback);
|
||||
},
|
||||
offDetected: function(callback) {
|
||||
defaultScanner.offDetected(callback);
|
||||
},
|
||||
onProcessed: function(callback) {
|
||||
defaultScanner.onProcessed(callback);
|
||||
},
|
||||
offProcessed: function(callback) {
|
||||
defaultScanner.offProcessed(callback);
|
||||
},
|
||||
registerResultCollector: function(resultCollector) {
|
||||
defaultScanner.registerResultCollector(resultCollector);
|
||||
},
|
||||
decodeSingle: function(config, resultCallback) {
|
||||
defaultScanner.decodeSingle(config, resultCallback);
|
||||
},
|
||||
ImageWrapper: ImageWrapper,
|
||||
ImageDebug: ImageDebug,
|
||||
ResultCollector: ResultCollector,
|
||||
|
||||
+15
-13
@@ -1,7 +1,7 @@
|
||||
import ImageWrapper from './common/image_wrapper';
|
||||
import BarcodeLocator from './locator/barcode_locator';
|
||||
import createLocator, {checkImageConstraints} from './locator/barcode_locator';
|
||||
import BarcodeDecoder from './decoder/barcode_decoder';
|
||||
import Events from './common/events';
|
||||
import createEventedElement from './common/events';
|
||||
import CameraAccess from './input/camera_access';
|
||||
import ImageDebug from './common/image_debug';
|
||||
import ResultCollector from './analytics/result_collector';
|
||||
@@ -34,7 +34,9 @@ function createScanner() {
|
||||
_workerPool = [],
|
||||
_onUIThread = true,
|
||||
_resultCollector,
|
||||
_config = {};
|
||||
_config = {},
|
||||
_events = createEventedElement(),
|
||||
_locator;
|
||||
|
||||
function initializeData(imageWrapper) {
|
||||
initBuffers(imageWrapper);
|
||||
@@ -85,7 +87,7 @@ function createScanner() {
|
||||
}
|
||||
|
||||
function canRecord(cb) {
|
||||
BarcodeLocator.checkImageConstraints(_inputStream, _config.locator);
|
||||
checkImageConstraints(_inputStream, _config.locator);
|
||||
initCanvas(_config);
|
||||
_framegrabber = FrameGrabber.create(_inputStream, _canvasContainer.dom.image);
|
||||
|
||||
@@ -155,12 +157,12 @@ function createScanner() {
|
||||
vec2.clone([_inputImageWrapper.size.x, _inputImageWrapper.size.y]),
|
||||
vec2.clone([_inputImageWrapper.size.x, 0])
|
||||
];
|
||||
BarcodeLocator.init(_inputImageWrapper, _config.locator);
|
||||
_locator = createLocator(_inputImageWrapper, _config.locator);
|
||||
}
|
||||
|
||||
function getBoundingBoxes() {
|
||||
if (_config.locate) {
|
||||
return BarcodeLocator.locate();
|
||||
return _locator.locate();
|
||||
} else {
|
||||
return [[
|
||||
vec2.clone(_boxSize[0]),
|
||||
@@ -245,9 +247,9 @@ function createScanner() {
|
||||
resultToPublish = result.barcodes || result;
|
||||
}
|
||||
|
||||
Events.publish("processed", resultToPublish);
|
||||
_events.publish("processed", resultToPublish);
|
||||
if (hasCodeResult(result)) {
|
||||
Events.publish("detected", resultToPublish);
|
||||
_events.publish("detected", resultToPublish);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -498,16 +500,16 @@ function createScanner() {
|
||||
_stopped = true;
|
||||
},
|
||||
onDetected: function(callback) {
|
||||
Events.subscribe("detected", callback);
|
||||
_events.subscribe("detected", callback);
|
||||
},
|
||||
offDetected: function(callback) {
|
||||
Events.unsubscribe("detected", callback);
|
||||
_events.unsubscribe("detected", callback);
|
||||
},
|
||||
onProcessed: function(callback) {
|
||||
Events.subscribe("processed", callback);
|
||||
_events.subscribe("processed", callback);
|
||||
},
|
||||
offProcessed: function(callback) {
|
||||
Events.unsubscribe("processed", callback);
|
||||
_events.unsubscribe("processed", callback);
|
||||
},
|
||||
registerResultCollector: function(resultCollector) {
|
||||
if (resultCollector && typeof resultCollector.addResult === 'function') {
|
||||
@@ -516,7 +518,7 @@ function createScanner() {
|
||||
},
|
||||
decodeSingle: function(config, resultCallback) {
|
||||
this.init(config, () => {
|
||||
Events.once("processed", (result) => {
|
||||
_events.once("processed", (result) => {
|
||||
this.stop();
|
||||
resultCallback.call(null, result);
|
||||
}, true);
|
||||
|
||||
Reference in New Issue
Block a user