1. Error Correction

2. Mask Pattern

3. Unmasking

4. Block Values

5. Decoding

Although this won't help us decode a QR code by hand it's important to note that machines read differently from us. Typically in case of damage, a QR reader will use error correction bytes to fix a damaged message.

Level L : 07% recoverable
Level M : 15% recoverable
Level Q : 25% recoverable
Level H : 30% recoverable

The percentages given represent the surface % of damage that a QR can whitstand before becoming unreadable

Each QR is affected to one of the eight possible mask pattern types

j % 3
(i+j) % 3
(i+j) % 2
i % 2
[(i*j) % 3 + i*j] % 2
[(i*j) % 3 + i+j] % 2
(i/2 + j/3) % 2
(i*j) % 2 + (i*j) % 3

The mask patterns can be generated from their respective operations using [i,j] as the coordinate of each square. If the result of the operation is 0 then the square should be black, otherwise it should be white.

The unmasked QR can be calculated by substracting the original QR (1) to its mask pattern (2)

a
b
c
d
+
a
b
c
d
=
a
b
c
d

In the example above we unmask the bottom-right 2x2 block of the QR. A white block in the mask pattern means no change, a block one means we should invert the original color.

Once the QR is unmasked, the first 2x2 block gives us the data type

Binary/Byte
all ASCII chars
Numerical
0-9
Alphanumerical
A-Z and 0-9
Kanji
JISX0208 + JISX0201

The QR code is then divided into blocks of 2x4 squares to be read sequentially in a zig zag pattern Each block is assigned a value by counting the black squares and adding their coefficients according to the blocks orientation

1
2
4
8
16
32
64
128

up

64
128
16
32
4
8
1
2

down

4
8
16
32
1
2
64
128

up to left

1
2
64
128
4
8
16
32

down to left

1
2
4
8
16
32
64
128

If we add all the coefficients of the second block we have 1 + 4 + 8 = 13. Indicating that the message is 13 blocks long.

The remaining blocks each correspond to a character of the encoded message. All that is left to do is look up the sums of each block inside an ASCII table

046 .
047 /
048 0
049 1
050 2
051 3
052 4
053 5
054 6
055 7
056 8
057 9
097 a
098 b
099 c
100 d
101 e
102 f
103 g
104 h
105 i
106 j
107 k
108 l
109 m
110 n
111 o
112 p
113 q
114 r
115 s
116 t
117 u
118 v
119 w
120 x
121 y
122 z