How do QR codes work?

What each part of the pattern does, how a camera turns squares back into text, and why a code with a hole punched in it still scans.

Short answerA QR code stores data as black and white squares called modules. Fixed patterns in the corners let a scanner find and orient the code; the rest of the grid holds the data plus Reed–Solomon error-correction codes, which allow the original text to be rebuilt even when part of the symbol is unreadable.

The parts of the pattern

Not every square in a QR code carries data. A significant share is structure, and knowing which is which explains most of the format’s behaviour.

The fixed structures in every QR code
PartWhereWhat it does
Finder patternsThree cornersLarge squares-within-squares that let a scanner locate the code and work out its rotation.
SeparatorsAround each finderA one-module white border that stops the finder blending into nearby data.
Timing patternsOne row and one columnAlternating black and white modules that tell the scanner the grid size and pitch.
Alignment patternsVersion 2 and aboveSmaller squares that correct perspective distortion when scanning at an angle or on a curve.
Format informationBeside the findersWhich error-correction level and mask were used. Stored twice, so it survives damage.
Quiet zoneOutside the symbolFour modules of clear space. Not decoration — without it the scanner cannot tell where the code ends.

Everything left over holds the message and its error correction, written in a zigzag from the bottom-right corner upward, two columns at a time.

Versions: how the grid grows

QR codes come in forty sizes, called versions. Version 1 is 21 × 21 modules; each step adds four modules per side, so version 40 is 177 × 177. A generator picks the smallest version that fits your data at the error-correction level requested.

This is why a short URL produces a visibly chunkier code than a long one at the same printed size. The physical square stays the same; the number of modules crammed into it does not. A coarse code tolerates poor light, cheap printing, and distance. A dense one does not.

The generator on this site reports the version and module count under every code it makes, so you can see the density before committing it to print.

Error correction: why damage does not matter

QR codes use Reed–Solomon error correction, the same family of mathematics that keeps a scratched CD playing. Redundant data is interleaved through the symbol so that a loss in one area can be reconstructed from information elsewhere.

The four error-correction levels
LevelRecoverableTypical use
L (Low)about 7%Clean digital display, maximum data in minimum space.
M (Medium)about 15%The usual default. Good balance for print.
Q (Quartile)about 25%Print that will be handled, or codes with a logo over the centre.
H (High)about 30%Industrial labels, heavy wear, large logo overlays.

Higher correction is not free: the redundancy occupies capacity, so the same text pushes the code to a larger version with smaller modules. Past a point, the extra robustness is cancelled out by the modules becoming too small to read reliably.

That trade-off is why this generator uses level Q for codes up to version 12 and drops to M above it (a code with a logo uses H instead). Below that threshold, the extra protection is worth the density; above it, the smaller modules cost more scans than the redundancy saves.

Masking: why two similar links look completely different

A raw encoding can easily produce large blank areas, long runs of the same colour, or shapes that resemble a finder pattern — all of which confuse scanners. To avoid this, the encoder applies a mask: a regular pattern XORed over the data area to break up those runs.

Eight masks are defined. The encoder tries each, scores the result against penalty rules for undesirable features, and keeps the best. The chosen mask is recorded in the format information so the reader can undo it.

This is why changing one character in a URL can rearrange the entire pattern: a different mask may now score best, and the whole data area is transformed differently.

What happens when you point a camera at one

  1. The camera frame is thresholded to black and white.
  2. The software sweeps for the 1:1:3:1:1 ratio of a finder pattern along horizontal and vertical lines.
  3. Three finders give position, scale, and rotation; alignment patterns correct perspective.
  4. The timing patterns establish the module grid, and the format information is read to learn the mask and correction level.
  5. The mask is removed, the data and error-correction blocks are de-interleaved, and Reed–Solomon decoding repairs any damage.
  6. The resulting bytes are interpreted — as a URL, contact card, or plain text — and the phone offers the matching action.

All of that happens many times a second, which is why a code seems to resolve the instant it enters the frame.

See the version, correction level, and module count for your own link:Generate a QR code

Related questions

Because of Reed–Solomon error correction. Redundant information is spread throughout the symbol, so between roughly 7% and 30% of it — depending on the level chosen when the code was made — can be missing and the original data still reconstructed exactly.

It is the four-module-wide blank margin around the symbol, and yes. Without it the scanner cannot establish where the code ends and the surrounding artwork begins. Cropping the quiet zone to make a design fit is the most common reason a printed code fails.

Because the encoder picks whichever of the eight mask patterns scores best for that particular data, and a one-character change can shift the winner. The mask is XORed across the whole data area, so a different choice rearranges the entire code.

They have to be dark on light with strong contrast. Black on white is the most reliable; a dark ink on a pale background usually works. Inverting to light-on-dark breaks many scanners, and low-contrast or metallic finishes reduce the read rate substantially.