Live model · QR codes

How QR Codes Actually Work

A QR code turns your text into bytes, adds Reed–Solomon check codewords computed over a field of 256 values, and lays every bit out in a zigzag around three finder patterns, flipped by one of eight masks. When a smudge spoils some codewords, the scanner uses the check codewords to find which ones and fix them.

Text to a QR code, one step at a time

“https://codebrew.ai” is 19 characters. Byte mode stores each one as a byte, its ASCII code: “h” is 104, 0x68.
StopQR encoding
TextError correction
Version2Size25 × 25LevelMData28Check16

Bytes (19)

25 × 25 modules, empty

Penalty for each mask (lowest wins)

Scored once the data is placed.

Watch the Python behind this model run, line by line

How to read the stage

The stage builds a real code for https://codebrew.ai at level M. The row of tiles holds its 44 codewords: 28 of data (mode and length, your text, then pad bytes) and 16 check codewords. The grid is the code, 25 × 25 modules, filled step by step. The bars score all eight masks; mask 6 wins with 499 penalty points. The last frame scans with a phone.

Why the code is masked

Raw data can leave big blank patches, or a run that looks like a finder pattern and fools the scanner. So the encoder tries eight masks, each flipping the data modules on a fixed pattern, and scores each one: points for long runs, 2 × 2 boxes, finder look-alikes and too much dark or light. The lowest score is printed, and 15 format bits say which mask it was.

Why a blotted code still scans

A 7 × 7 blot over the middle covers 49 modules but ruins only 8 of the 44 codewords, since each codeword's 8 bits sit together. The scanner doesn't know which 8. Every unknown error hides two unknowns, where it is and what it should be, so 16 check codewords can fix 8. That's exactly enough here. Make the blot 8 × 8 and the code fails.

Blot it, then let Reed–Solomon repair it

An ink blot 7 × 7 covers 49 modules, 8% of the code. Every module under it now reads dark.
StopReed–Solomon repair
Blot sizeBlot rowBlot columnScanner knows whereError correctionText
LevelMBlot7 × 7Wrong8Fixes up to8

Version 2-M, blotted

Codewords as printed (44)

Syndromes: the codewords at each root of g(x)

0 on every root means no error found.

The same blot on each level's code

LevelSizeFixesWrongReads
L25×2559
M25×2588
Q25×25118
H29×2911 + 114 + 5

What L, M, Q and H mean

The level sets how many check codewords a code carries, so roughly 7%, 15%, 25% or 30% of it can be restored. Check codewords take room: at level H the same address needs version 3, 29 × 29 modules. The table under the repair stage puts the same blot on all four: level L fails, the rest read. A scanner told where the damage is fixes twice as many.

What the model leaves out

Real codes go up to version 40 and also have numeric, alphanumeric and kanji modes; this one uses byte mode in versions 1 to 3. It assumes the scanner has already found the code in a photo, squared it up and read the format bits, so the blot only spoils what it covers. The mask penalties follow Thonky's reading of the standard, ISO/IEC 18004.

Questions

How much data can a QR code hold?
The largest, version 40 at level L, is 177 × 177 modules and holds 2,953 bytes, 4,296 letters and digits, or 7,089 digits alone. The smallest, version 1, holds 17 bytes at level L and 7 at level H. The 19-byte address on the stage needs version 2 at level M.
Why do QR codes have three squares in the corners?
They're finder patterns: a dark square, a light ring and a dark 3 × 3 core, so any line through the middle crosses dark and light in the ratio 1:1:3:1:1. A scanner hunts for that ratio to find the code, its size and which way up it is. The fourth corner holds data, which is how the scanner tells the corners apart.
Can you put a logo in the middle of a QR code?
Yes, if it spoils fewer codewords than the level can fix. To the scanner a logo is damage it can't locate, so each codeword it covers costs two check codewords. Level H leaves the most room. Keep the finder patterns and the format bits beside them clear, and try it on the repair stage with a blot.
What does each error correction level cost?
Space. For the 19 characters on the stage, level L and level M both fit in version 2, 25 × 25, with 10 and 16 check codewords. Level Q uses 22. Level H needs version 3, 29 × 29, split into two blocks of 22 check codewords each, so each block can fix 11 unknown errors.

More of how it works