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