When encoding a QR code, there are eight mask patterns that you can use to change the outputted matrix. Each mask pattern changes the bits according to their coordinates in the QR matrix. The purpose of a mask pattern is to make the QR code easier for a QR scanner to read.
The Mask Patterns
Each mask pattern uses a formula to determine whether or not to change the color of the current bit. You put the coordinates of the current bit into the formula, and if the result is 0, you use the opposite bit at that coordinate. For example, if the bit for coordinate (0,3) is 1, and the formula is equal to 0 for that coordinate, then you put a 0 at (0,3) instead of a 1.
Here is the list of the mask pattern formulas. Note that some versions of the QR code standard have had errors in the section about mask patterns. The information below has been corrected.
Note: mod is short for Modulo.
Mask Number | If the formula below is true for a given row/column coordinate, switch the bit at that coordinate |
---|---|
0 | (row + column) mod 2 == 0 |
1 | (row) mod 2 == 0 |
2 | (column) mod 3 == 0 |
3 | (row + column) mod 3 == 0 |
4 | ( floor(row / 2) + floor(column / 3) ) mod 2 == 0 |
5 | ((row * column) mod 2) + ((row * column) mod 3) == 0 |
6 | ( ((row * column) mod 2) + ((row * column) mod 3) ) mod 2 == 0 |
7 | ( ((row + column) mod 2) + ((row * column) mod 3) ) mod 2 == 0 |