How QR codes work

Also called: QR code encoding, QR code decoding

Definition

A QR code works by turning data into codewords, adding Reed–Solomon error correction, and placing the bits in a grid around fixed patterns that help a camera find, straighten and read it.

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.

Frequently asked questions

How does a phone know where the QR code is?

It looks for the three large square finder patterns in the corners. Their distinctive 1:1:3:1:1 ratio lets the scanner find them from any angle and work out the orientation.

Why do QR codes have a mask pattern?

The mask flips modules in a set pattern so the code avoids large solid areas and shapes that look like finder patterns. The encoder tries all eight masks and keeps the one that reads most reliably.

Can a QR code be read upside down?

Yes. The finder patterns tell the scanner the orientation, so a QR code can be read at any rotation.

Sources and standards

All terms