Skip to content

Commit

Permalink
Support eci chunks (cozmo#71)
Browse files Browse the repository at this point in the history
* Support ECI encoding chunks

* Build
  • Loading branch information
cozmo authored Jun 28, 2018
1 parent 0794a90 commit 28fa4bf
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 6 deletions.
7 changes: 6 additions & 1 deletion dist/decoder/decodeData/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ export interface ByteChunk {
type: Mode.Byte | Mode.Kanji;
bytes: number[];
}
export declare type Chunks = Array<Chunk | ByteChunk>;
export interface ECIChunk {
type: Mode.ECI;
assignmentNumber: number;
}
export declare type Chunks = Array<Chunk | ByteChunk | ECIChunk>;
export interface DecodedQR {
text: string;
bytes: number[];
Expand All @@ -17,5 +21,6 @@ export declare enum Mode {
Alphanumeric = "alphanumeric",
Byte = "byte",
Kanji = "kanji",
ECI = "eci",
}
export declare function decode(data: Uint8ClampedArray, version: number): DecodedQR;
30 changes: 29 additions & 1 deletion dist/jsQR.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ var Mode;
Mode["Alphanumeric"] = "alphanumeric";
Mode["Byte"] = "byte";
Mode["Kanji"] = "kanji";
Mode["ECI"] = "eci";
})(Mode = exports.Mode || (exports.Mode = {}));
var ModeByte;
(function (ModeByte) {
Expand All @@ -817,8 +818,8 @@ var ModeByte;
ModeByte[ModeByte["Alphanumeric"] = 2] = "Alphanumeric";
ModeByte[ModeByte["Byte"] = 4] = "Byte";
ModeByte[ModeByte["Kanji"] = 8] = "Kanji";
ModeByte[ModeByte["ECI"] = 7] = "ECI";
// StructuredAppend = 0x3,
// ECI = 0x7,
// FNC1FirstPosition = 0x5,
// FNC1SecondPosition = 0x9,
})(ModeByte || (ModeByte = {}));
Expand Down Expand Up @@ -938,6 +939,33 @@ function decode(data, version) {
if (mode === ModeByte.Terminator) {
return result;
}
else if (mode === ModeByte.ECI) {
if (stream.readBits(1) === 0) {
result.chunks.push({
type: Mode.ECI,
assignmentNumber: stream.readBits(7),
});
}
else if (stream.readBits(1) === 0) {
result.chunks.push({
type: Mode.ECI,
assignmentNumber: stream.readBits(14),
});
}
else if (stream.readBits(1) === 0) {
result.chunks.push({
type: Mode.ECI,
assignmentNumber: stream.readBits(21),
});
}
else {
// ECI data seems corrupted
result.chunks.push({
type: Mode.ECI,
assignmentNumber: -1,
});
}
}
else if (mode === ModeByte.Numeric) {
var numericResult = decodeNumeric(stream, size);
result.text += numericResult.text;
Expand Down
30 changes: 29 additions & 1 deletion docs/jsQR.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ var Mode;
Mode["Alphanumeric"] = "alphanumeric";
Mode["Byte"] = "byte";
Mode["Kanji"] = "kanji";
Mode["ECI"] = "eci";
})(Mode = exports.Mode || (exports.Mode = {}));
var ModeByte;
(function (ModeByte) {
Expand All @@ -817,8 +818,8 @@ var ModeByte;
ModeByte[ModeByte["Alphanumeric"] = 2] = "Alphanumeric";
ModeByte[ModeByte["Byte"] = 4] = "Byte";
ModeByte[ModeByte["Kanji"] = 8] = "Kanji";
ModeByte[ModeByte["ECI"] = 7] = "ECI";
// StructuredAppend = 0x3,
// ECI = 0x7,
// FNC1FirstPosition = 0x5,
// FNC1SecondPosition = 0x9,
})(ModeByte || (ModeByte = {}));
Expand Down Expand Up @@ -938,6 +939,33 @@ function decode(data, version) {
if (mode === ModeByte.Terminator) {
return result;
}
else if (mode === ModeByte.ECI) {
if (stream.readBits(1) === 0) {
result.chunks.push({
type: Mode.ECI,
assignmentNumber: stream.readBits(7),
});
}
else if (stream.readBits(1) === 0) {
result.chunks.push({
type: Mode.ECI,
assignmentNumber: stream.readBits(14),
});
}
else if (stream.readBits(1) === 0) {
result.chunks.push({
type: Mode.ECI,
assignmentNumber: stream.readBits(21),
});
}
else {
// ECI data seems corrupted
result.chunks.push({
type: Mode.ECI,
assignmentNumber: -1,
});
}
}
else if (mode === ModeByte.Numeric) {
var numericResult = decodeNumeric(stream, size);
result.text += numericResult.text;
Expand Down
33 changes: 31 additions & 2 deletions src/decoder/decodeData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ export interface ByteChunk {
bytes: number[];
}

export type Chunks = Array<Chunk | ByteChunk>;
export interface ECIChunk {
type: Mode.ECI;
assignmentNumber: number;
}

export type Chunks = Array<Chunk | ByteChunk | ECIChunk>;

export interface DecodedQR {
text: string;
Expand All @@ -25,6 +30,7 @@ export enum Mode {
Alphanumeric = "alphanumeric",
Byte = "byte",
Kanji = "kanji",
ECI = "eci",
}

enum ModeByte {
Expand All @@ -33,8 +39,8 @@ enum ModeByte {
Alphanumeric = 0x2,
Byte = 0x4,
Kanji = 0x8,
ECI = 0x7,
// StructuredAppend = 0x3,
// ECI = 0x7,
// FNC1FirstPosition = 0x5,
// FNC1SecondPosition = 0x9,
}
Expand Down Expand Up @@ -178,6 +184,29 @@ export function decode(data: Uint8ClampedArray, version: number): DecodedQR {
const mode = stream.readBits(4);
if (mode === ModeByte.Terminator) {
return result;
} else if (mode === ModeByte.ECI) {
if (stream.readBits(1) === 0) {
result.chunks.push({
type: Mode.ECI,
assignmentNumber: stream.readBits(7),
});
} else if (stream.readBits(1) === 0) {
result.chunks.push({
type: Mode.ECI,
assignmentNumber: stream.readBits(14),
});
} else if (stream.readBits(1) === 0) {
result.chunks.push({
type: Mode.ECI,
assignmentNumber: stream.readBits(21),
});
} else {
// ECI data seems corrupted
result.chunks.push({
type: Mode.ECI,
assignmentNumber: -1,
});
}
} else if (mode === ModeByte.Numeric) {
const numericResult = decodeNumeric(stream, size);
result.text += numericResult.text;
Expand Down
Binary file added src/decoder/test-data/eci.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions src/decoder/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,23 @@ describe("decode", () => {
const matrix = new BitMatrix(d, Math.sqrt(d.length));
expect(decode(matrix)).toBeNull();
});

it("Supports ECI chunks", async () => {
const data = await loadBinarized("./src/decoder/test-data/eci.png");
expect(decode(data)).toEqual({
text: "7948,328,1019,149,12,12,15,4,14,11,32,4",
bytes: [55, 57, 52, 56, 44, 51, 50, 56, 44, 49, 48, 49, 57, 44, 49, 52, 57, 44, 49, 50, 44, 49, 50, 44, 49, 53, 44,
52, 44, 49, 52, 44, 49, 49, 44, 51, 50, 44, 52],
chunks: [
{
type: "eci", assignmentNumber: 26,
}, {
type: "byte",
bytes: [55, 57, 52, 56, 44, 51, 50, 56, 44, 49, 48, 49, 57, 44, 49, 52, 57, 44, 49, 50, 44, 49, 50, 44, 49,
53, 44, 52, 44, 49, 52, 44, 49, 49, 44, 51, 50, 44, 52],
text: "7948,328,1019,149,12,12,15,4,14,11,32,4",
},
],
});
});
});
Binary file added tests/end-to-end/issue-61/input.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
129 changes: 129 additions & 0 deletions tests/end-to-end/issue-61/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"binaryData": [
55,
57,
52,
56,
44,
51,
50,
56,
44,
49,
48,
49,
57,
44,
49,
52,
57,
44,
49,
50,
44,
49,
50,
44,
49,
53,
44,
52,
44,
49,
52,
44,
49,
49,
44,
51,
50,
44,
52
],
"data": "7948,328,1019,149,12,12,15,4,14,11,32,4",
"chunks": [
{
"type": "eci",
"assignmentNumber": 26
},
{
"type": "byte",
"bytes": [
55,
57,
52,
56,
44,
51,
50,
56,
44,
49,
48,
49,
57,
44,
49,
52,
57,
44,
49,
50,
44,
49,
50,
44,
49,
53,
44,
52,
44,
49,
52,
44,
49,
49,
44,
51,
50,
44,
52
],
"text": "7948,328,1019,149,12,12,15,4,14,11,32,4"
}
],
"location": {
"topRightCorner": {
"x": 383.84809348093484,
"y": 488.1519065190653
},
"topLeftCorner": {
"x": 173.02219884001437,
"y": 488.1141764615306
},
"bottomRightCorner": {
"x": 384.45612961610846,
"y": 698.7848078485414
},
"bottomLeftCorner": {
"x": 172.9384938617451,
"y": 699.061506138255
},
"topRightFinderPattern": {
"x": 358.5,
"y": 513.5
},
"topLeftFinderPattern": {
"x": 198.5,
"y": 513.5
},
"bottomLeftFinderPattern": {
"x": 198.5,
"y": 673.5
},
"bottomRightAlignmentPattern": {
"x": 337,
"y": 651.5
}
}
}
3 changes: 2 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": 206
"successful": 207
},
"tests": {
"0": true,
Expand Down Expand Up @@ -221,6 +221,7 @@
"issue-54-2": false,
"issue-59": true,
"issue-59-inverted": true,
"issue-61": true,
"japan-visa": false,
"japanese-jean-ad": false,
"kanji": true,
Expand Down

0 comments on commit 28fa4bf

Please sign in to comment.