Skip to content

Commit

Permalink
decodeSingle: run all calls on a new Quagga instance
Browse files Browse the repository at this point in the history
- this appears to actually work correctly, all tests are passing
- move stop functionality into the instance
- init accepts an instance parameter to determine which instance to init
- update bound to instance
- add Quagga::setReaders and Quagga::registerReader
- setupInputStream defaults to LiveStream per the TypeScript def for
  inputStream config
- InputStreamType back to optional in input stream config
  • Loading branch information
ericblade committed Apr 16, 2020
1 parent b9cade6 commit 2bfe9cf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
30 changes: 13 additions & 17 deletions src/quagga.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,27 @@ const instance = new Quagga();
const _context = instance.context;

export default {
init: function (config, cb, imageWrapper) {
_context.config = merge({}, Config, config);
init: function (config, cb, imageWrapper, quaggaInstance = instance) {
quaggaInstance.context.config = merge({}, Config, config);
// TODO: pending restructure in Issue #105, we are temp disabling workers
if (_context.config.numOfWorkers > 0) {
_context.config.numOfWorkers = 0;
if (quaggaInstance.context.config.numOfWorkers > 0) {
quaggaInstance.context.config.numOfWorkers = 0;
}
if (imageWrapper) {
_context.onUIThread = false;
instance.initializeData(imageWrapper);
quaggaInstance.context.onUIThread = false;
quaggaInstance.initializeData(imageWrapper);
if (cb) {
cb();
}
} else {
instance.initInputStream(cb);
quaggaInstance.initInputStream(cb);
}
},
start: function () {
instance.start();
},
stop: function () {
_context.stopped = true;
QWorkers.adjustWorkerPool(0);
if (_context.config.inputStream && _context.config.inputStream.type === 'LiveStream') {
CameraAccess.release();
_context.inputStream.clearEventHandlers();
}
instance.stop();
},
pause: function () {
_context.stopped = true;
Expand Down Expand Up @@ -70,6 +65,7 @@ export default {
},
canvas: _context.canvasContainer,
decodeSingle: function (config, resultCallback) {
const quaggaInstance = new Quagga();
if (this.inDecodeSingle) {
// force multiple calls to decodeSingle to run in serial, because presently
// simultaneous running breaks things.
Expand All @@ -84,7 +80,7 @@ export default {
}
return null;
}
this.inDecodeSingle = true;
// this.inDecodeSingle = true;
config = merge({
inputStream: {
type: 'ImageStream',
Expand Down Expand Up @@ -113,14 +109,14 @@ export default {
this.init(config, () => {
Events.once('processed', (result) => {
this.inDecodeSingle = false;
this.stop();
quaggaInstance.stop();
if (resultCallback) {
resultCallback.call(null, result);
}
resolve(result);
}, true);
instance.start();
});
quaggaInstance.start();
}, null, quaggaInstance);
} catch (err) {
this.inDecodeSingle = false;
reject(err);
Expand Down
27 changes: 25 additions & 2 deletions src/quagga/quagga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import CameraAccess from '../input/camera_access';
import { clone } from 'gl-vec2';
import { BarcodeInfo } from '../reader/barcode_reader';
import { moveLine, moveBox } from './transform';
import { QuaggaJSResultObject } from '../../type-definitions/quagga';
import { QuaggaJSResultObject, QuaggaJSReaderConfig } from '../../type-definitions/quagga';
import Events from '../common/events';

const InputStream = typeof window === 'undefined' ? NodeInputStream : BrowserInputStream;
Expand Down Expand Up @@ -195,7 +195,7 @@ export default class Quagga {
}
}

update() {
update = () => {
if (this.context.onUIThread) {
const workersUpdated = QWorkers.updateWorkers(this.context.framegrabber);
if (!workersUpdated) {
Expand Down Expand Up @@ -241,4 +241,27 @@ export default class Quagga {
}
}

stop() {
this.context.stopped = true;
QWorkers.adjustWorkerPool(0);
if (this.context.config?.inputStream && this.context.config.inputStream.type === 'LiveStream') {
CameraAccess.release();
this.context.inputStream.clearEventHandlers();
}
}

setReaders(readers: Array<QuaggaJSReaderConfig>) {
if (this.context.decoder) {
this.context.decoder.setReaders(readers);
}
QWorkers.setReaders(readers);
}

registerReader(name: string, reader: QuaggaJSReaderConfig) {
BarcodeDecoder.registerReader(name, reader);
if (this.context.decoder) {
this.context.decoder.registerReader(name, reader);
}
QWorkers.registerReader(name, reader);
}
}
2 changes: 1 addition & 1 deletion src/quagga/setupInputStream.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InputStreamType } from "../../type-definitions/quagga";

// TODO: need to create an InputStream typescript interface, so we don't have an "any" in the next line
export default function setupInputStream(type: InputStreamType, viewport: Element | null, InputStream: any) {
export default function setupInputStream(type: InputStreamType = 'LiveStream', viewport: Element | null, InputStream: any) {
switch (type) {
case 'VideoStream': {
const video = document.createElement('video');
Expand Down
2 changes: 1 addition & 1 deletion type-definitions/quagga.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ export interface QuaggaJSConfigObject {
/**
* @default "LiveStream"
*/
type: InputStreamType;
type?: InputStreamType;

target?: Element | string,

Expand Down

0 comments on commit 2bfe9cf

Please sign in to comment.