The parts of the pattern
Every QR code follows the same layout, set out in the international standard ISO/IEC 18004. Some parts are fixed and help the camera find and measure the grid. The rest carries data.
- Finder patterns: the three large squares in the corners, each 7 by 7 modules. The empty fourth corner tells the phone which way the code is turned.
- Timing patterns: a row and a column of alternating dark and light modules running between the finders. They let the phone count the grid.
- Alignment patterns: smaller squares, from version 2 upward, that help when the code is printed on a curve or photographed at an angle.
- Format information: 15 bits next to the finders that record the error correction level and the mask. They are written twice, so one copy can be damaged.
- Version information: from version 7 upward, 18 bits near two of the corners that record the size of the grid.
- Data and error correction: every other module.
- Quiet zone: a blank margin, four modules wide, around the whole code.
How a QR code is generated
A generator turns your content into a pattern in a fixed order of steps.
- Choose an encoding mode: digits only, digits with uppercase letters and a few symbols, any bytes, or Japanese kanji. Digits pack tightest, three of them in 10 bits.
- Pick the smallest version that holds the data at the chosen error correction level.
- Write the data as bits, with a short header for the mode and the length, and cut it into 8-bit codewords.
- Calculate the Reed-Solomon error correction codewords and interleave them with the data.
- Place the bits in the grid in a zigzag, two columns at a time, starting at the bottom right and flowing around the fixed patterns.
- Apply each of the eight masks, score the results, and keep the one with the fewest problem areas.
- Write the format information, and the version information for larger codes.
Error correction: why a damaged code still scans
QR codes use Reed-Solomon error correction, the same family of codes used on CDs and DVDs. The generator adds extra codewords calculated from the data. If some codewords can't be read, the phone uses the extra ones to work out what they were.
| Level | Share that can be restored | Typical use |
|---|---|---|
| L | About 7% | Clean screens and documents |
| M | About 15% | General use |
| Q | About 25% | Print that may get dirty or worn |
| H | About 30% | Codes with a logo, rough conditions |
More correction means more codewords, so the same content needs a bigger grid. GridQR uses level Q by default and switches to H when you add a logo, because the logo covers part of the pattern.
Versions: why more data means more squares
The size of the grid is called the version. Version 1 is 21 by 21 modules, each version adds 4 modules per side, and version 40 is 177 by 177. A generator picks the smallest version that fits.
So the pattern grows with the content. At level Q, a 25-character short link fits in version 3, a grid of 29 by 29. A 137-character shop link with tracking tags needs version 10, a grid of 57 by 57. Printed at the same width, the second code's modules are about half as wide, and the camera has to be closer and steadier.
At the top end, a version 40 code at level L holds 7,089 digits, 4,296 characters from the uppercase set, or 2,953 bytes. Real codes are kept far smaller than that, because dense codes are hard to scan.
Masking
Raw data can produce awkward patterns: large solid blocks, long stripes, or shapes that look like a finder square. Those confuse cameras. So the generator flips modules according to one of eight fixed masks, such as a checkerboard. It tries all eight, scores each one for problem areas, and keeps the best.
The number of the mask goes into the format information. The phone reads it and flips the same modules back before decoding. The fixed patterns are never masked.
What the phone does when it scans
- The camera finds the three finder patterns. Their size and position give the angle, distance and tilt of the code.
- It uses the timing and alignment patterns to map the grid of modules, even if the code is slightly curved.
- It reads the format information to learn the error correction level and the mask.
- It removes the mask and reads the codewords in the same zigzag order they were written.
- It fixes any errors with the Reed-Solomon codewords.
- It decodes the bits into text and checks what kind of text it is: a web address, Wi-Fi details, a contact card, an email or a text message. Then it offers the matching action.
All of this happens on the phone, in a fraction of a second, with no internet connection. A connection is needed only if the content is a link to open.
Static and dynamic codes
The standard doesn't know the difference. Both are ordinary QR codes; what differs is what the generator puts in them.
A static code holds the content itself: your full web address, or the Wi-Fi name and password. A dynamic code holds a short link to a server. When the phone opens it, the server looks up the current destination, sends the phone on and counts the scan. That is why the destination can change after printing, and why the pattern stays small: it only ever holds the short link.