Skip to content

Commit

Permalink
Add a test for locator fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
jefff committed Apr 23, 2020
1 parent a52cda5 commit 49eca1a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/locator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,17 @@ export function locate(matrix: BitMatrix): QRLocation[] {
const { topRight, topLeft, bottomLeft } = reorderFinderPatterns(
finderPatternGroups[0].points[0], finderPatternGroups[0].points[1], finderPatternGroups[0].points[2],
);
const { alignmentPattern, dimension } = findAlignmentPattern(matrix, alignmentPatternQuads, topRight, topLeft, bottomLeft);
const alignment = findAlignmentPattern(matrix, alignmentPatternQuads, topRight, topLeft, bottomLeft);
const result: QRLocation[] = [];
if (alignment) {
result.push({
alignmentPattern: { x: alignment.alignmentPattern.x, y: alignment.alignmentPattern.y },
bottomLeft: {x: bottomLeft.x, y: bottomLeft.y },
dimension: alignment.dimension,
topLeft: {x: topLeft.x, y: topLeft.y },
topRight: {x: topRight.x, y: topRight.y },
});
}

// We normally use the center of the quads as the location of the tracking points, which is optimal for most cases and will account
// for a skew in the image. However, In some cases, a slight skew might not be real and instead be caused by image compression
Expand All @@ -396,20 +406,21 @@ export function locate(matrix: BitMatrix): QRLocation[] {
const midTopLeft = recenterLocation(matrix, topLeft);
const midBottomLeft = recenterLocation(matrix, bottomLeft);
const centeredAlignment = findAlignmentPattern(matrix, alignmentPatternQuads, midTopRight, midTopLeft, midBottomLeft);
if (centeredAlignment) {
result.push({
alignmentPattern: { x: centeredAlignment.alignmentPattern.x, y: centeredAlignment.alignmentPattern.y },
bottomLeft: { x: midBottomLeft.x, y: midBottomLeft. y },
topLeft: { x: midTopLeft.x, y: midTopLeft. y },
topRight: { x: midTopRight.x, y: midTopRight. y },
dimension: centeredAlignment.dimension,
});
}

if (result.length === 0) {
return null;
}

return [{
alignmentPattern: { x: alignmentPattern.x, y: alignmentPattern.y },
bottomLeft: {x: bottomLeft.x, y: bottomLeft.y },
dimension,
topLeft: {x: topLeft.x, y: topLeft.y },
topRight: {x: topRight.x, y: topRight.y },
}, {
alignmentPattern: { x: centeredAlignment.alignmentPattern.x, y: centeredAlignment.alignmentPattern.y },
bottomLeft: { x: midBottomLeft.x, y: midBottomLeft. y },
topLeft: { x: midTopLeft.x, y: midTopLeft. y },
topRight: { x: midTopRight.x, y: midTopRight. y },
dimension: centeredAlignment.dimension,
}];
return result;
}

function findAlignmentPattern(matrix: BitMatrix, alignmentPatternQuads: Quad[], topRight: Point, topLeft: Point, bottomLeft: Point) {
Expand Down
Binary file added src/locator/test-data/odd-skew.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions src/locator/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,15 @@ describe("locate", () => {
const binarized = await loadBinarized("./src/locator/test-data/malformed-infinity.png");
expect(locate(binarized)).toEqual(null);
});

it("returns a centered alignment as a fallback", async () => {
const binarized = await loadBinarized("./src/locator/test-data/odd-skew.png");
expect(locate(binarized)[1]).toEqual({
alignmentPattern: { x: 163.5, y: 170 },
bottomLeft: { x: 56.5, y: 185.5 },
dimension: 29,
topLeft: { x: 57, y: 60 },
topRight: { x: 185.5, y: 57.5 },
});
});
});

0 comments on commit 49eca1a

Please sign in to comment.