Skip to content

Commit

Permalink
Add support for QRs without a termination byte (#80)
Browse files Browse the repository at this point in the history
* Add support for QRs without a termination byte

* Correctly follow spec
  • Loading branch information
cozmo authored Jul 24, 2018
1 parent 77191bb commit b3bac44
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/decoder/decodeData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,9 @@ export function decode(data: Uint8ClampedArray, version: number): DecodedQR {
});
}
}

// If there is no data left, or the remaining bits are all 0, then that counts as a termination marker
if (stream.available() === 0 || stream.readBits(stream.available()) === 0) {
return result;
}
}
Binary file added src/decoder/test-data/no-termination-byte.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions src/decoder/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,25 @@ describe("decode", () => {
],
});
});

it("Extracts a QR code that is missing the termination byte", async () => {
const data = await loadBinarized("./src/decoder/test-data/no-termination-byte.png");
expect(decode(data)).toEqual({
text: "1788c74b1c9262866c2071b65df7bfcb7911c2b064c931b580515c2d9d2cd7f8",
bytes: [ 49, 55, 56, 56, 99, 55, 52, 98, 49, 99, 57, 50, 54, 50, 56, 54, 54, 99, 50, 48, 55, 49, 98, 54, 53, 100,
102, 55, 98, 102, 99, 98, 55, 57, 49, 49, 99, 50, 98, 48, 54, 52, 99, 57, 51, 49, 98, 53, 56, 48, 53, 49, 53,
99, 50, 100, 57, 100, 50, 99, 100, 55, 102, 56 ],
chunks: [
{ type: "numeric", text: "1788" },
{ type: "byte", bytes: [99, 55, 52, 98, 49, 99], text: "c74b1c" },
{ type: "numeric", text: "9262866" },
{ type: "byte",
bytes: [99, 50, 48, 55, 49, 98, 54, 53, 100, 102, 55, 98, 102, 99, 98, 55, 57, 49, 49, 99, 50, 98, 48, 54, 52,
99, 57, 51, 49, 98],
text: "c2071b65df7bfcb7911c2b064c931b" },
{ type: "numeric", text: "580515" },
{ type: "byte", bytes: [99, 50, 100, 57, 100, 50, 99, 100, 55, 102, 56], text: "c2d9d2cd7f8" },
],
});
});
});

0 comments on commit b3bac44

Please sign in to comment.