Skip to content

Commit 2c14a48

Browse files
committed
feat: return multiple as array instead of object
1 parent 9a9b8de commit 2c14a48

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,11 @@ more possible clashes, or false-positives. One should take care of the order
381381
the readers are given, since some might return a value even though it is not
382382
the correct type (EAN-13 vs. UPC-A).
383383

384-
The `multiple` property tells the decoder if it should stop after finding a
385-
single result. If multiple is set to `true`, the `result` object will have a
386-
`barcodes` array, each of which is a `result` object with it's own `codeResult`,
387-
`box`, `line`, etc.
384+
The `multiple` property tells the decoder if it should continue decoding after
385+
finding a valid barcode. If multiple is set to `true`, the results will be
386+
returned as an array of result objects. Each object in the array will have a
387+
`box`, and may have a `codeResult` depending on the success of decoding the
388+
individual box.
388389

389390
The remaining properties `drawBoundingBox`, `showFrequency`, `drawScanline` and
390391
`showPattern` are mostly of interest during debugging and visualization.

src/barcode_decoder.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,21 +267,23 @@ export default {
267267
return decodeFromBoundingBox(box);
268268
},
269269
decodeFromBoundingBoxes: function(boxes) {
270-
var i, result, barcodes = [];
271-
for ( i = 0; i < boxes.length; i++) {
272-
result = decodeFromBoundingBox(boxes[i]);
273-
if (result && result.codeResult) {
274-
result.box = boxes[i];
270+
var i, result,
271+
barcodes = [],
272+
multiple = config.multiple;
275273

276-
if (!config.multiple) {
277-
return result;
278-
}
274+
for ( i = 0; i < boxes.length; i++) {
275+
const box = boxes[i];
276+
result = decodeFromBoundingBox(box) || {};
277+
result.box = box;
279278

279+
if (multiple) {
280280
barcodes.push(result);
281+
} else if (result.codeResult) {
282+
return result;
281283
}
282284
}
283285

284-
if (config.multiple) {
286+
if (multiple) {
285287
return {
286288
barcodes
287289
};

src/quagga.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ function transformResult(result) {
178178
moveLine(result.line);
179179
}
180180

181+
if (result.box) {
182+
moveBox(result.box);
183+
}
184+
181185
if (result.boxes && result.boxes.length > 0) {
182186
for (i = 0; i < result.boxes.length; i++) {
183187
moveBox(result.boxes[i]);
@@ -202,39 +206,35 @@ function transformResult(result) {
202206
}
203207

204208
function addResult (result, imageData) {
205-
var i;
206-
207209
if (!imageData || !_resultCollector) {
208210
return;
209211
}
210212

211213
if (result.barcodes) {
212-
for (i = 0; i < result.barcodes.length; i++) {
213-
addResult(result.barcodes[i], imageData);
214-
}
215-
return;
216-
}
217-
218-
if (result.codeResult) {
214+
result.barcodes.filter(barcode => barcode.codeResult)
215+
.forEach(barcode => addResult(barcode, imageData));
216+
} else if (result.codeResult) {
219217
_resultCollector.addResult(imageData, _inputStream.getCanvasSize(), result.codeResult);
220218
}
221219
}
222220

223221
function hasCodeResult (result) {
224-
return result && result.barcodes ?
222+
return result && (result.barcodes ?
225223
result.barcodes.some(barcode => barcode.codeResult) :
226-
result.codeResult;
224+
result.codeResult);
227225
}
228226

229227
function publishResult(result, imageData) {
228+
const resultToPublish = result && (result.barcodes || result);
229+
230230
if (result && _onUIThread) {
231231
transformResult(result);
232232
addResult(result, imageData);
233233
}
234234

235-
Events.publish("processed", result);
235+
Events.publish("processed", resultToPublish);
236236
if (hasCodeResult(result)) {
237-
Events.publish("detected", result);
237+
Events.publish("detected", resultToPublish);
238238
}
239239
}
240240

0 commit comments

Comments
 (0)