diff --git a/src/BitMatrix.ts b/src/BitMatrix.ts index 2d3b86d1..5983ccb3 100644 --- a/src/BitMatrix.ts +++ b/src/BitMatrix.ts @@ -31,4 +31,8 @@ export class BitMatrix { } } } + + public getInverted() { + return new BitMatrix(this.data.map(d => d === 0 ? 1 : 0), this.width); + } } diff --git a/src/index.ts b/src/index.ts index 1158048e..e6fd7d74 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,13 +23,12 @@ export interface QRCode { }; } -function jsQR(data: Uint8ClampedArray, width: number, height: number): QRCode | null { - const binarized = binarize(data, width, height); - const location = locate(binarized); +function scan(matrix: BitMatrix): QRCode | null { + const location = locate(matrix); if (!location) { return null; } - const extracted = extract(binarized, location); + const extracted = extract(matrix, location); const decoded = decode(extracted.matrix); if (!decoded) { @@ -55,5 +54,14 @@ function jsQR(data: Uint8ClampedArray, width: number, height: number): QRCode | }; } +function jsQR(data: Uint8ClampedArray, width: number, height: number): QRCode | null { + const binarized = binarize(data, width, height); + let result = scan(binarized); + if (!result) { + result = scan(binarized.getInverted()); + } + return result; +} + (jsQR as any).default = jsQR; export default jsQR; diff --git a/tests/end-to-end/152/output.json b/tests/end-to-end/152/output.json index e587c5c9..e49a19b3 100644 --- a/tests/end-to-end/152/output.json +++ b/tests/end-to-end/152/output.json @@ -245,4 +245,4 @@ "y": 307 } } -} +} \ No newline at end of file diff --git a/tests/end-to-end/issue-59-inverted/input.png b/tests/end-to-end/issue-59-inverted/input.png new file mode 100644 index 00000000..b3e58b6e Binary files /dev/null and b/tests/end-to-end/issue-59-inverted/input.png differ diff --git a/tests/end-to-end/issue-59-inverted/output.json b/tests/end-to-end/issue-59-inverted/output.json new file mode 100644 index 00000000..fd5c7dfb --- /dev/null +++ b/tests/end-to-end/issue-59-inverted/output.json @@ -0,0 +1,91 @@ +{ + "binaryData": [ + 104, + 116, + 116, + 112, + 58, + 47, + 47, + 119, + 119, + 119, + 46, + 113, + 114, + 115, + 116, + 117, + 102, + 102, + 46, + 99, + 111, + 109 + ], + "data": "http://www.qrstuff.com", + "chunks": [ + { + "type": "byte", + "bytes": [ + 104, + 116, + 116, + 112, + 58, + 47, + 47, + 119, + 119, + 119, + 46, + 113, + 114, + 115, + 116, + 117, + 102, + 102, + 46, + 99, + 111, + 109 + ], + "text": "http://www.qrstuff.com" + } + ], + "location": { + "topRightCorner": { + "x": 159.66666666666669, + "y": 20.333333333333332 + }, + "topLeftCorner": { + "x": 26.085501858736055, + "y": 20.08550185873606 + }, + "bottomRightCorner": { + "x": 158.18365235385414, + "y": 152.18365235385411 + }, + "bottomLeftCorner": { + "x": 26.333333333333332, + "y": 153.66666666666666 + }, + "topRightFinderPattern": { + "x": 141, + "y": 39 + }, + "topLeftFinderPattern": { + "x": 45, + "y": 39 + }, + "bottomLeftFinderPattern": { + "x": 45, + "y": 135 + }, + "bottomRightAlignmentPattern": { + "x": 124.5, + "y": 118.5 + } + } +} \ No newline at end of file diff --git a/tests/end-to-end/issue-59/input.png b/tests/end-to-end/issue-59/input.png new file mode 100644 index 00000000..a5c4a7a5 Binary files /dev/null and b/tests/end-to-end/issue-59/input.png differ diff --git a/tests/end-to-end/issue-59/output.json b/tests/end-to-end/issue-59/output.json new file mode 100644 index 00000000..b685339a --- /dev/null +++ b/tests/end-to-end/issue-59/output.json @@ -0,0 +1,91 @@ +{ + "binaryData": [ + 104, + 116, + 116, + 112, + 58, + 47, + 47, + 119, + 119, + 119, + 46, + 113, + 114, + 115, + 116, + 117, + 102, + 102, + 46, + 99, + 111, + 109 + ], + "data": "http://www.qrstuff.com", + "chunks": [ + { + "type": "byte", + "bytes": [ + 104, + 116, + 116, + 112, + 58, + 47, + 47, + 119, + 119, + 119, + 46, + 113, + 114, + 115, + 116, + 117, + 102, + 102, + 46, + 99, + 111, + 109 + ], + "text": "http://www.qrstuff.com" + } + ], + "location": { + "topRightCorner": { + "x": 153.66666666666666, + "y": 20.333333333333332 + }, + "topLeftCorner": { + "x": 20.08550185873606, + "y": 20.08550185873606 + }, + "bottomRightCorner": { + "x": 152.18365235385411, + "y": 152.18365235385411 + }, + "bottomLeftCorner": { + "x": 20.333333333333332, + "y": 153.66666666666666 + }, + "topRightFinderPattern": { + "x": 135, + "y": 39 + }, + "topLeftFinderPattern": { + "x": 39, + "y": 39 + }, + "bottomLeftFinderPattern": { + "x": 39, + "y": 135 + }, + "bottomRightAlignmentPattern": { + "x": 118.5, + "y": 118.5 + } + } +} \ No newline at end of file diff --git a/tests/end-to-end/report.json b/tests/end-to-end/report.json index 3c891219..ad593cce 100644 --- a/tests/end-to-end/report.json +++ b/tests/end-to-end/report.json @@ -1,7 +1,7 @@ { "counts": { "failed": 47, - "successful": 204 + "successful": 206 }, "tests": { "0": true, @@ -219,6 +219,8 @@ "issue-34-regression": true, "issue-54-1": false, "issue-54-2": false, + "issue-59": true, + "issue-59-inverted": true, "japan-visa": false, "japanese-jean-ad": false, "kanji": true,