mirror of
https://github.com/serratus/quaggaJS.git
synced 2026-08-12 21:21:42 +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 = {};
|
var events = {};
|
||||||
|
|
||||||
function getEvent(eventName) {
|
function getEvent(eventName) {
|
||||||
@@ -79,4 +79,4 @@ export default (function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})();
|
};
|
||||||
|
|||||||
@@ -24,9 +24,10 @@ const mat2 = {
|
|||||||
copy: require('gl-mat2/copy'),
|
copy: require('gl-mat2/copy'),
|
||||||
create: require('gl-mat2/create'),
|
create: require('gl-mat2/create'),
|
||||||
invert: require('gl-mat2/invert')
|
invert: require('gl-mat2/invert')
|
||||||
}
|
};
|
||||||
|
|
||||||
var _config,
|
export default function createLocator(inputImageWrapper, config) {
|
||||||
|
var _config = config,
|
||||||
_currentImageWrapper,
|
_currentImageWrapper,
|
||||||
_skelImageWrapper,
|
_skelImageWrapper,
|
||||||
_subImageWrapper,
|
_subImageWrapper,
|
||||||
@@ -45,9 +46,12 @@ var _config,
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
_numPatches = {x: 0, y: 0},
|
_numPatches = {x: 0, y: 0},
|
||||||
_inputImageWrapper,
|
_inputImageWrapper = inputImageWrapper,
|
||||||
_skeletonizer;
|
_skeletonizer;
|
||||||
|
|
||||||
|
initBuffers();
|
||||||
|
initCanvas();
|
||||||
|
|
||||||
function initBuffers() {
|
function initBuffers() {
|
||||||
var skeletonImageData;
|
var skeletonImageData;
|
||||||
|
|
||||||
@@ -525,15 +529,7 @@ function rasterizeAngularSimilarity(patchesFound) {
|
|||||||
return label;
|
return label;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
return {
|
||||||
init: function(inputImageWrapper, config) {
|
|
||||||
_config = config;
|
|
||||||
_inputImageWrapper = inputImageWrapper;
|
|
||||||
|
|
||||||
initBuffers();
|
|
||||||
initCanvas();
|
|
||||||
},
|
|
||||||
|
|
||||||
locate: function() {
|
locate: function() {
|
||||||
var patchesFound,
|
var patchesFound,
|
||||||
topLabels,
|
topLabels,
|
||||||
@@ -564,9 +560,10 @@ export default {
|
|||||||
|
|
||||||
boxes = findBoxes(topLabels, maxLabel);
|
boxes = findBoxes(topLabels, maxLabel);
|
||||||
return boxes;
|
return boxes;
|
||||||
},
|
}
|
||||||
|
}
|
||||||
checkImageConstraints: function(inputStream, config) {
|
}
|
||||||
|
export function checkImageConstraints(inputStream, config) {
|
||||||
var patchSize,
|
var patchSize,
|
||||||
width = inputStream.getWidth(),
|
width = inputStream.getWidth(),
|
||||||
height = inputStream.getHeight(),
|
height = inputStream.getHeight(),
|
||||||
@@ -604,4 +601,3 @@ export default {
|
|||||||
width + " )and height (" + height +
|
width + " )and height (" + height +
|
||||||
") must a multiple of " + patchSize.x);
|
") 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 WebrtcAdapter from 'webrtc-adapter'; // eslint-disable-line no-unused-vars
|
||||||
import createScanner from './scanner';
|
import createScanner from './scanner';
|
||||||
import ImageWrapper from './common/image_wrapper';
|
import ImageWrapper from './common/image_wrapper';
|
||||||
import Events from './common/events';
|
|
||||||
import ImageDebug from './common/image_debug';
|
import ImageDebug from './common/image_debug';
|
||||||
import ResultCollector from './analytics/result_collector';
|
import ResultCollector from './analytics/result_collector';
|
||||||
import Config from './config/config';
|
import Config from './config/config';
|
||||||
@@ -31,29 +30,19 @@ function fromImage(config, imageSrc, imageConfig) {
|
|||||||
const scanner = createScanner(config);
|
const scanner = createScanner(config);
|
||||||
return {
|
return {
|
||||||
addEventListener: (eventType, cb) => {
|
addEventListener: (eventType, cb) => {
|
||||||
scanner.init(config, () => {
|
scanner.decodeSingle(config, cb);
|
||||||
Events.once(eventType, (result) => {
|
|
||||||
scanner.stop();
|
|
||||||
cb(result);
|
|
||||||
}, true);
|
|
||||||
scanner.start();
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
removeEventListener(cb) {
|
removeEventListener(cb) {
|
||||||
console.log("Remove listener");
|
console.log("Remove listener");
|
||||||
},
|
},
|
||||||
toPromise() {
|
toPromise() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
scanner.init(config, () => {
|
scanner.decodeSingle(config, (result) => {
|
||||||
Events.once('processed', (result) => {
|
|
||||||
scanner.stop();
|
|
||||||
if (result.codeResult && result.codeResult.code) {
|
if (result.codeResult && result.codeResult.code) {
|
||||||
return resolve(result);
|
return resolve(result);
|
||||||
}
|
}
|
||||||
return reject(result);
|
return reject(result);
|
||||||
});
|
});
|
||||||
scanner.start();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -117,9 +106,6 @@ function createApi(configuration = Config) {
|
|||||||
config(conf) {
|
config(conf) {
|
||||||
return createApi(merge({}, configuration, conf));
|
return createApi(merge({}, configuration, conf));
|
||||||
},
|
},
|
||||||
init: function(config, cb, imageWrapper) {
|
|
||||||
defaultScanner.init(config, cb, imageWrapper);
|
|
||||||
},
|
|
||||||
start: function() {
|
start: function() {
|
||||||
defaultScanner.start();
|
defaultScanner.start();
|
||||||
},
|
},
|
||||||
@@ -129,24 +115,9 @@ function createApi(configuration = Config) {
|
|||||||
pause: function() {
|
pause: function() {
|
||||||
defaultScanner.pause();
|
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) {
|
registerResultCollector: function(resultCollector) {
|
||||||
defaultScanner.registerResultCollector(resultCollector);
|
defaultScanner.registerResultCollector(resultCollector);
|
||||||
},
|
},
|
||||||
decodeSingle: function(config, resultCallback) {
|
|
||||||
defaultScanner.decodeSingle(config, resultCallback);
|
|
||||||
},
|
|
||||||
ImageWrapper: ImageWrapper,
|
ImageWrapper: ImageWrapper,
|
||||||
ImageDebug: ImageDebug,
|
ImageDebug: ImageDebug,
|
||||||
ResultCollector: ResultCollector,
|
ResultCollector: ResultCollector,
|
||||||
|
|||||||
+15
-13
@@ -1,7 +1,7 @@
|
|||||||
import ImageWrapper from './common/image_wrapper';
|
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 BarcodeDecoder from './decoder/barcode_decoder';
|
||||||
import Events from './common/events';
|
import createEventedElement from './common/events';
|
||||||
import CameraAccess from './input/camera_access';
|
import CameraAccess from './input/camera_access';
|
||||||
import ImageDebug from './common/image_debug';
|
import ImageDebug from './common/image_debug';
|
||||||
import ResultCollector from './analytics/result_collector';
|
import ResultCollector from './analytics/result_collector';
|
||||||
@@ -34,7 +34,9 @@ function createScanner() {
|
|||||||
_workerPool = [],
|
_workerPool = [],
|
||||||
_onUIThread = true,
|
_onUIThread = true,
|
||||||
_resultCollector,
|
_resultCollector,
|
||||||
_config = {};
|
_config = {},
|
||||||
|
_events = createEventedElement(),
|
||||||
|
_locator;
|
||||||
|
|
||||||
function initializeData(imageWrapper) {
|
function initializeData(imageWrapper) {
|
||||||
initBuffers(imageWrapper);
|
initBuffers(imageWrapper);
|
||||||
@@ -85,7 +87,7 @@ function createScanner() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function canRecord(cb) {
|
function canRecord(cb) {
|
||||||
BarcodeLocator.checkImageConstraints(_inputStream, _config.locator);
|
checkImageConstraints(_inputStream, _config.locator);
|
||||||
initCanvas(_config);
|
initCanvas(_config);
|
||||||
_framegrabber = FrameGrabber.create(_inputStream, _canvasContainer.dom.image);
|
_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, _inputImageWrapper.size.y]),
|
||||||
vec2.clone([_inputImageWrapper.size.x, 0])
|
vec2.clone([_inputImageWrapper.size.x, 0])
|
||||||
];
|
];
|
||||||
BarcodeLocator.init(_inputImageWrapper, _config.locator);
|
_locator = createLocator(_inputImageWrapper, _config.locator);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getBoundingBoxes() {
|
function getBoundingBoxes() {
|
||||||
if (_config.locate) {
|
if (_config.locate) {
|
||||||
return BarcodeLocator.locate();
|
return _locator.locate();
|
||||||
} else {
|
} else {
|
||||||
return [[
|
return [[
|
||||||
vec2.clone(_boxSize[0]),
|
vec2.clone(_boxSize[0]),
|
||||||
@@ -245,9 +247,9 @@ function createScanner() {
|
|||||||
resultToPublish = result.barcodes || result;
|
resultToPublish = result.barcodes || result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Events.publish("processed", resultToPublish);
|
_events.publish("processed", resultToPublish);
|
||||||
if (hasCodeResult(result)) {
|
if (hasCodeResult(result)) {
|
||||||
Events.publish("detected", resultToPublish);
|
_events.publish("detected", resultToPublish);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -498,16 +500,16 @@ function createScanner() {
|
|||||||
_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);
|
||||||
},
|
},
|
||||||
registerResultCollector: function(resultCollector) {
|
registerResultCollector: function(resultCollector) {
|
||||||
if (resultCollector && typeof resultCollector.addResult === 'function') {
|
if (resultCollector && typeof resultCollector.addResult === 'function') {
|
||||||
@@ -516,7 +518,7 @@ function createScanner() {
|
|||||||
},
|
},
|
||||||
decodeSingle: function(config, resultCallback) {
|
decodeSingle: function(config, resultCallback) {
|
||||||
this.init(config, () => {
|
this.init(config, () => {
|
||||||
Events.once("processed", (result) => {
|
_events.once("processed", (result) => {
|
||||||
this.stop();
|
this.stop();
|
||||||
resultCallback.call(null, result);
|
resultCallback.call(null, result);
|
||||||
}, true);
|
}, true);
|
||||||
|
|||||||
Reference in New Issue
Block a user