Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple barcodes to be decoded #90

Merged
merged 8 commits into from
Feb 15, 2016
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"ecmaFeatures": {
"blockBindings": true,
"forOf": true,
"blockBindings": true,
"defaultParams": true,
"globalReturn": false,
"modules": true,
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ options within the `decoder` are for debugging/visualization purposes only.
showPattern: false,
readers: [
'code_128_reader'
]
],
multiple: false
}
```

Expand All @@ -380,6 +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 remaining properties `drawBoundingBox`, `showFrequency`, `drawScanline` and
`showPattern` are mostly of interest during debugging and visualization.

Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"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",
"grunt-karma": "^0.12.1",
"isparta-loader": "^1.0.0",
Expand All @@ -21,9 +23,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",
Expand All @@ -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",
Expand All @@ -63,7 +65,7 @@
],
"author": "Christoph Oberhofer <[email protected]>",
"license": "MIT",
"engines":{
"engines": {
"node": ">= 4.0"
},
"dependencies": {
Expand Down
15 changes: 13 additions & 2 deletions src/barcode_decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 26 additions & 5 deletions src/quagga.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The detected event is not fired any more when multiple is activated. Maybe you could expand the condition to include something like: result.barcodes && result.barcodes.length > 0 . And since you only add the results to this array if the decoding was successful, this should be sufficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must have overlooked the last part of that method the first time through, good catch. I just pushed a fix for it which also future-proofs the check if we decide to return an array instead of an object. I'm making the assumption that some of the barcodes might not have a codeResult instead of just checking barcodes.length. If you agree that's the way we should go I will commit the array result change.

Expand Down