Moved remaining code to instances

feature/109
Christoph Oberhofer 9 years ago 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,9 +46,12 @@ var _config,
}
},
_numPatches = {x: 0, y: 0},
_inputImageWrapper,
_inputImageWrapper = inputImageWrapper,
_skeletonizer;
initBuffers();
initCanvas();
function initBuffers() {
var skeletonImageData;
@ -525,15 +529,7 @@ 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(),
@ -604,4 +601,3 @@ export default {
width + " )and height (" + height +
") must a multiple of " + patchSize.x);
}
};

@ -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,

@ -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);

Loading…
Cancel
Save