Skip to content

Commit

Permalink
Merge pull request #42 from cozmo/fix-#41-crash-when-fewer-than-3-fin…
Browse files Browse the repository at this point in the history
…der-patterns

Fix #41 crash when fewer than 3 finder patterns
  • Loading branch information
cozmo authored Jan 21, 2018
2 parents 5d6e0f3 + 9b30e76 commit deff667
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dist/jsQR.js
Original file line number Diff line number Diff line change
Expand Up @@ -9905,6 +9905,9 @@ function locate(matrix) {
.filter(function (p, ii) { return i !== ii; })
.map(function (p) { return ({ x: p.x, y: p.y, score: p.score + (Math.pow((p.size - point.size), 2)) / point.size, size: p.size }); })
.sort(function (a, b) { return a.score - b.score; });
if (otherPoints.length < 2) {
return null;
}
var score = point.score + otherPoints[0].score + otherPoints[1].score;
return { points: [point].concat(otherPoints.slice(0, 2)), score: score };
})
Expand Down
3 changes: 3 additions & 0 deletions docs/jsQR.js
Original file line number Diff line number Diff line change
Expand Up @@ -9905,6 +9905,9 @@ function locate(matrix) {
.filter(function (p, ii) { return i !== ii; })
.map(function (p) { return ({ x: p.x, y: p.y, score: p.score + (Math.pow((p.size - point.size), 2)) / point.size, size: p.size }); })
.sort(function (a, b) { return a.score - b.score; });
if (otherPoints.length < 2) {
return null;
}
var score = point.score + otherPoints[0].score + otherPoints[1].score;
return { points: [point].concat(otherPoints.slice(0, 2)), score: score };
})
Expand Down
3 changes: 3 additions & 0 deletions src/locator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ export function locate(matrix: BitMatrix): QRLocation {
.filter((p, ii) => i !== ii)
.map(p => ({ x: p.x, y: p.y, score: p.score + ((p.size - point.size) ** 2) / point.size, size: p.size }))
.sort((a, b) => a.score - b.score);
if (otherPoints.length < 2) {
return null;
}
const score = point.score + otherPoints[0].score + otherPoints[1].score;
return {points: [point].concat(otherPoints.slice(0, 2)), score};
})
Expand Down
Binary file added src/locator/test-data/missing-finder-patterns.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/locator/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ describe("locate", () => {
expect(output).toEqual(t.location);
});
});

it("handles images with missing finder patterns", async () => {
const binarized = await loadBinarized("./src/locator/test-data/missing-finder-patterns.png");
expect(() => locate(binarized)).not.toThrow();
});
});

0 comments on commit deff667

Please sign in to comment.