From e4cba7f15e16d0a6a2e57955effb0167caad9c9c Mon Sep 17 00:00:00 2001 From: dgreif Date: Mon, 8 Feb 2016 06:43:48 -0600 Subject: [PATCH 1/8] chore: fix dev dependencies --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index d4c6cc0..4b6140f 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "chai": "^3.4.1", "core-js": "^1.2.1", "grunt": "^0.4.5", + "grunt-cli": "^0.1.13", "grunt-contrib-nodeunit": "^0.4.1", "grunt-karma": "^0.12.1", "isparta-loader": "^1.0.0", @@ -21,9 +22,8 @@ "karma-coverage": "^0.5.2", "karma-mocha": "~0.2.0", "karma-phantomjs-launcher": "^0.2.1", - "karma-sinon-chai": "^1.1.0", "karma-sinon": "^1.0.4", - "karma-sinon-chai": "~0.2.0", + "karma-sinon-chai": "^1.1.0", "karma-source-map-support": "^1.1.0", "karma-webpack": "^1.7.0", "mocha": "^2.3.2", @@ -63,7 +63,7 @@ ], "author": "Christoph Oberhofer ", "license": "MIT", - "engines":{ + "engines": { "node": ">= 4.0" }, "dependencies": { From 09e1cd4b1e2dbecda8112145cf108d89cd3e1ef3 Mon Sep 17 00:00:00 2001 From: dgreif Date: Mon, 8 Feb 2016 06:58:32 -0600 Subject: [PATCH 2/8] chore: add lint script --- .eslintrc | 2 -- package.json | 4 +++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.eslintrc b/.eslintrc index d857d30..104faf0 100644 --- a/.eslintrc +++ b/.eslintrc @@ -10,7 +10,6 @@ "ecmaFeatures": { "blockBindings": true, "forOf": true, - "blockBindings": true, "defaultParams": true, "globalReturn": false, "modules": true, @@ -59,7 +58,6 @@ "comma-style": [2, "last"], "consistent-this": [1, "self"], "eol-last": 0, - "new-cap": 0, "new-parens": 2, "no-array-constructor": 2, "no-mixed-spaces-and-tabs": 2, diff --git a/package.json b/package.json index 4b6140f..8438cbf 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "babel-loader": "^5.3.2", "chai": "^3.4.1", "core-js": "^1.2.1", + "eslint": "^1.10.3", "grunt": "^0.4.5", "grunt-cli": "^0.1.13", "grunt-contrib-nodeunit": "^0.4.1", @@ -39,7 +40,8 @@ "test": "grunt test", "integrationtest": "grunt integrationtest", "build": "webpack && webpack --config webpack.config.min.js && grunt uglyasm && webpack --config webpack.node.config.js", - "watch": "webpack --watch" + "watch": "webpack --watch", + "lint": "eslint src" }, "repository": { "type": "git", From afeb37be35fdffd9be48e92b63a3e54be39f3e5a Mon Sep 17 00:00:00 2001 From: dgreif Date: Mon, 8 Feb 2016 09:36:08 -0600 Subject: [PATCH 3/8] feat: allow multiple barcodes to be decoded --- src/barcode_decoder.js | 15 +++++++++++++-- src/quagga.js | 31 ++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/barcode_decoder.js b/src/barcode_decoder.js index 54d1e1f..c927d9e 100644 --- a/src/barcode_decoder.js +++ b/src/barcode_decoder.js @@ -267,14 +267,25 @@ export default { return decodeFromBoundingBox(box); }, decodeFromBoundingBoxes: function(boxes) { - var i, result; + var i, result, barcodes = []; for ( i = 0; i < boxes.length; i++) { result = decodeFromBoundingBox(boxes[i]); if (result && result.codeResult) { result.box = boxes[i]; - return result; + + if (!config.multiple) { + return result; + } + + barcodes.push(result); } } + + if (config.multiple) { + return { + barcodes + }; + } }, setReaders: function(readers) { config.readers = readers; diff --git a/src/quagga.js b/src/quagga.js index 8c90134..042d738 100644 --- a/src/quagga.js +++ b/src/quagga.js @@ -168,10 +168,16 @@ function transformResult(result) { return; } + if (result.barcodes) { + for (i = 0; i < result.barcodes.length; i++) { + transformResult(result.barcodes[i]); + } + } if (result.line && result.line.length === 2) { moveLine(result.line); } + if (result.boxes && result.boxes.length > 0) { for (i = 0; i < result.boxes.length; i++) { moveBox(result.boxes[i]); @@ -195,14 +201,29 @@ function transformResult(result) { } } +function addResult (result, imageData) { + var i; + + if (!imageData || !result || !_resultCollector) { + return; + } + + if (result.barcodes) { + for (i = 0; i < result.barcodes.length; i++) { + addResult(result.barcodes[i], imageData); + } + return; + } + + if (result.codeResult) { + _resultCollector.addResult(imageData, _inputStream.getCanvasSize(), result.codeResult); + } +} + function publishResult(result, imageData) { if (_onUIThread) { transformResult(result); - if (imageData && result && result.codeResult) { - if (_resultCollector) { - _resultCollector.addResult(imageData, _inputStream.getCanvasSize(), result.codeResult); - } - } + addResult(result, imageData); } Events.publish("processed", result); From a1ded4b3e772256e597a6aecca6d7de0624e74ac Mon Sep 17 00:00:00 2001 From: dgreif Date: Mon, 8 Feb 2016 09:41:06 -0600 Subject: [PATCH 4/8] docs: add `multiple` to docs May need to go into more detail since the result object is significantly different. --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9adcb30..66e2e25 100644 --- a/README.md +++ b/README.md @@ -357,7 +357,8 @@ options within the `decoder` are for debugging/visualization purposes only. showPattern: false, readers: [ 'code_128_reader' - ] + ], + multiple: false } ``` @@ -380,6 +381,10 @@ more possible clashes, or false-positives. One should take care of the order the readers are given, since some might return a value even though it is not the correct type (EAN-13 vs. UPC-A). +The `multiple` property tells the decoder if it should stop after finding a single result. If multiple is set to +`true`, the `result` object will have a `barcodes` array, each of which is a `result` object with it's own `codeResult`, +`box`, `line`, etc. + The remaining properties `drawBoundingBox`, `showFrequency`, `drawScanline` and `showPattern` are mostly of interest during debugging and visualization. From 9ebb69ec38068e3ac1825d84942d785db62f4814 Mon Sep 17 00:00:00 2001 From: dgreif Date: Mon, 8 Feb 2016 16:53:58 -0600 Subject: [PATCH 5/8] docs: wrap at 80 characters --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 66e2e25..587006f 100644 --- a/README.md +++ b/README.md @@ -381,8 +381,9 @@ more possible clashes, or false-positives. One should take care of the order the readers are given, since some might return a value even though it is not the correct type (EAN-13 vs. UPC-A). -The `multiple` property tells the decoder if it should stop after finding a single result. If multiple is set to -`true`, the `result` object will have a `barcodes` array, each of which is a `result` object with it's own `codeResult`, +The `multiple` property tells the decoder if it should stop after finding a +single result. If multiple is set to `true`, the `result` object will have a +`barcodes` array, each of which is a `result` object with it's own `codeResult`, `box`, `line`, etc. The remaining properties `drawBoundingBox`, `showFrequency`, `drawScanline` and From 74c4f04a07206dc4d6e216817473d13b513df45f Mon Sep 17 00:00:00 2001 From: dgreif Date: Mon, 8 Feb 2016 21:31:28 -0600 Subject: [PATCH 6/8] refactor: move result check to higher level --- src/quagga.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/quagga.js b/src/quagga.js index 042d738..031c68d 100644 --- a/src/quagga.js +++ b/src/quagga.js @@ -164,7 +164,7 @@ function transformResult(result) { yOffset = topRight.y, i; - if (!result || (xOffset === 0 && yOffset === 0)) { + if (xOffset === 0 && yOffset === 0) { return; } @@ -204,7 +204,7 @@ function transformResult(result) { function addResult (result, imageData) { var i; - if (!imageData || !result || !_resultCollector) { + if (!imageData || !_resultCollector) { return; } @@ -221,7 +221,7 @@ function addResult (result, imageData) { } function publishResult(result, imageData) { - if (_onUIThread) { + if (result && _onUIThread) { transformResult(result); addResult(result, imageData); } From 9a9b8de4bcec901ef806af4555c29c385950f80e Mon Sep 17 00:00:00 2001 From: dgreif Date: Mon, 8 Feb 2016 21:55:40 -0600 Subject: [PATCH 7/8] fix: publish `detected` event for `multiple` --- src/quagga.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/quagga.js b/src/quagga.js index 031c68d..41ef3ab 100644 --- a/src/quagga.js +++ b/src/quagga.js @@ -220,6 +220,12 @@ function addResult (result, imageData) { } } +function hasCodeResult (result) { + return result && result.barcodes ? + result.barcodes.some(barcode => barcode.codeResult) : + result.codeResult; +} + function publishResult(result, imageData) { if (result && _onUIThread) { transformResult(result); @@ -227,7 +233,7 @@ function publishResult(result, imageData) { } Events.publish("processed", result); - if (result && result.codeResult) { + if (hasCodeResult(result)) { Events.publish("detected", result); } } From 2c14a4897d2a86c77eaca019e011807eae9e356e Mon Sep 17 00:00:00 2001 From: dgreif Date: Tue, 9 Feb 2016 07:03:01 -0600 Subject: [PATCH 8/8] feat: return `multiple` as array instead of object --- README.md | 9 +++++---- src/barcode_decoder.js | 20 +++++++++++--------- src/quagga.js | 26 +++++++++++++------------- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 587006f..cdbfc6c 100644 --- a/README.md +++ b/README.md @@ -381,10 +381,11 @@ more possible clashes, or false-positives. One should take care of the order the readers are given, since some might return a value even though it is not the correct type (EAN-13 vs. UPC-A). -The `multiple` property tells the decoder if it should stop after finding a -single result. If multiple is set to `true`, the `result` object will have a -`barcodes` array, each of which is a `result` object with it's own `codeResult`, -`box`, `line`, etc. +The `multiple` property tells the decoder if it should continue decoding after +finding a valid barcode. If multiple is set to `true`, the results will be +returned as an array of result objects. Each object in the array will have a +`box`, and may have a `codeResult` depending on the success of decoding the +individual box. The remaining properties `drawBoundingBox`, `showFrequency`, `drawScanline` and `showPattern` are mostly of interest during debugging and visualization. diff --git a/src/barcode_decoder.js b/src/barcode_decoder.js index c927d9e..4417750 100644 --- a/src/barcode_decoder.js +++ b/src/barcode_decoder.js @@ -267,21 +267,23 @@ export default { return decodeFromBoundingBox(box); }, decodeFromBoundingBoxes: function(boxes) { - var i, result, barcodes = []; - for ( i = 0; i < boxes.length; i++) { - result = decodeFromBoundingBox(boxes[i]); - if (result && result.codeResult) { - result.box = boxes[i]; + var i, result, + barcodes = [], + multiple = config.multiple; - if (!config.multiple) { - return result; - } + for ( i = 0; i < boxes.length; i++) { + const box = boxes[i]; + result = decodeFromBoundingBox(box) || {}; + result.box = box; + if (multiple) { barcodes.push(result); + } else if (result.codeResult) { + return result; } } - if (config.multiple) { + if (multiple) { return { barcodes }; diff --git a/src/quagga.js b/src/quagga.js index 41ef3ab..dcf6e41 100644 --- a/src/quagga.js +++ b/src/quagga.js @@ -178,6 +178,10 @@ function transformResult(result) { moveLine(result.line); } + if (result.box) { + moveBox(result.box); + } + if (result.boxes && result.boxes.length > 0) { for (i = 0; i < result.boxes.length; i++) { moveBox(result.boxes[i]); @@ -202,39 +206,35 @@ function transformResult(result) { } function addResult (result, imageData) { - var i; - if (!imageData || !_resultCollector) { return; } if (result.barcodes) { - for (i = 0; i < result.barcodes.length; i++) { - addResult(result.barcodes[i], imageData); - } - return; - } - - if (result.codeResult) { + result.barcodes.filter(barcode => barcode.codeResult) + .forEach(barcode => addResult(barcode, imageData)); + } else if (result.codeResult) { _resultCollector.addResult(imageData, _inputStream.getCanvasSize(), result.codeResult); } } function hasCodeResult (result) { - return result && result.barcodes ? + return result && (result.barcodes ? result.barcodes.some(barcode => barcode.codeResult) : - result.codeResult; + result.codeResult); } function publishResult(result, imageData) { + const resultToPublish = result && (result.barcodes || result); + if (result && _onUIThread) { transformResult(result); addResult(result, imageData); } - Events.publish("processed", result); + Events.publish("processed", resultToPublish); if (hasCodeResult(result)) { - Events.publish("detected", result); + Events.publish("detected", resultToPublish); } }