From 2bfe9cfa3c451c0e378932610c7e864db6e8dcde Mon Sep 17 00:00:00 2001 From: Eric Blade Date: Thu, 16 Apr 2020 12:04:40 -0400 Subject: [PATCH] decodeSingle: run all calls on a new Quagga instance - 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 --- src/quagga.js | 30 +++++++++++++----------------- src/quagga/quagga.ts | 27 +++++++++++++++++++++++++-- src/quagga/setupInputStream.ts | 2 +- type-definitions/quagga.d.ts | 2 +- 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/quagga.js b/src/quagga.js index b4b02989..ef1df8e5 100644 --- a/src/quagga.js +++ b/src/quagga.js @@ -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; @@ -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. @@ -84,7 +80,7 @@ export default { } return null; } - this.inDecodeSingle = true; + // this.inDecodeSingle = true; config = merge({ inputStream: { type: 'ImageStream', @@ -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); diff --git a/src/quagga/quagga.ts b/src/quagga/quagga.ts index f6b34c66..c702198a 100644 --- a/src/quagga/quagga.ts +++ b/src/quagga/quagga.ts @@ -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; @@ -195,7 +195,7 @@ export default class Quagga { } } - update() { + update = () => { if (this.context.onUIThread) { const workersUpdated = QWorkers.updateWorkers(this.context.framegrabber); if (!workersUpdated) { @@ -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) { + 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); + } } diff --git a/src/quagga/setupInputStream.ts b/src/quagga/setupInputStream.ts index 6edc4440..e84d2b36 100644 --- a/src/quagga/setupInputStream.ts +++ b/src/quagga/setupInputStream.ts @@ -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'); diff --git a/type-definitions/quagga.d.ts b/type-definitions/quagga.d.ts index ca076ab7..6edea5bd 100644 --- a/type-definitions/quagga.d.ts +++ b/type-definitions/quagga.d.ts @@ -391,7 +391,7 @@ export interface QuaggaJSConfigObject { /** * @default "LiveStream" */ - type: InputStreamType; + type?: InputStreamType; target?: Element | string,