Skip to content

Commit

Permalink
Support inverted QR codes (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
cozmo authored Jun 28, 2018
1 parent 7de3098 commit bd9d700
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/BitMatrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ export class BitMatrix {
}
}
}

public getInverted() {
return new BitMatrix(this.data.map(d => d === 0 ? 1 : 0), this.width);
}
}
16 changes: 12 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
2 changes: 1 addition & 1 deletion tests/end-to-end/152/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,4 @@
"y": 307
}
}
}
}
Binary file added tests/end-to-end/issue-59-inverted/input.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 91 additions & 0 deletions tests/end-to-end/issue-59-inverted/output.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Binary file added tests/end-to-end/issue-59/input.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 91 additions & 0 deletions tests/end-to-end/issue-59/output.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
4 changes: 3 additions & 1 deletion tests/end-to-end/report.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"counts": {
"failed": 47,
"successful": 204
"successful": 206
},
"tests": {
"0": true,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit bd9d700

Please sign in to comment.