From data to pattern
Making a QR code starts by choosing an encoding mode that fits the data: numeric, alphanumeric, byte or kanji. The encoder converts the data to bits, splits them into 8-bit codewords and adds Reed–Solomon error correction codewords at the chosen level. It then picks the smallest version that fits.
The codewords are laid into the grid in a zigzag path that skips the fixed patterns. Finally the encoder applies one of eight mask patterns to break up large blank areas or confusing shapes, and records the mask and error correction level in the format information.
- Choose the encoding mode
- Encode data and add error correction codewords
- Pick the version (size)
- Place finder, timing and alignment patterns
- Fill in data modules
- Apply the best of eight masks and write format information
From camera image to data
Decoding runs the same steps in reverse. The scanner finds the three finder patterns, whose 1:1:3:1:1 ratio of dark and light stands out from most images, and uses them with the timing and alignment patterns to map the grid. It reads the format information, removes the mask and collects the codewords.
Reed–Solomon decoding then repairs any codewords that were misread, up to the limit of the error correction level. If too much is damaged, the code fails to read rather than returning wrong data, which is why QR codes rarely produce garbled results.
Why size and contrast matter
Each step depends on the camera telling dark modules from light ones. Small modules, low contrast, glare, blur or a missing quiet zone all push more modules into doubt, and error correction can only absorb so much. Keeping the data short, the contrast strong and the margin clear gives the decoder the easiest job.