How Do QR Codes Work? From Text to Scannable Pattern
Abhay Khant
Jan 1, 1970 • 5 min read
How Do QR Codes Work? From Text to Scannable Pattern
- QR codes pack data, error correction, and positioning aids into one module grid
- Three corner finders let cameras locate and straighten codes from any angle
- Masking alternates dark and light so sparse data never creates dead zones
- We damaged real codes until they failed: higher correction survived 200 blotches, basic failed at 38
The pipeline: text in, pattern out
A QR code is the visible end of a four-stage pipeline. Your text is first encoded as bits using a mode matched to its content. Redundancy is then added through error correction so damage becomes repairable. The bitstream is next placed into a grid of modules, the black and white squares, around mandatory positioning features. Finally a mask pattern flips selected modules so the finished symbol avoids pathological clumps. [Denso Wave, which invented the format in 1994](https://www.qrcode.com/en/about/), designed each stage for the noisy reality of cameras and printed paper, and the [format overview](https://www.qrcode.com/en/about/) credits that robustness for adoption beyond auto parts into every industry.
To ground the anatomy in something measurable, we generated a code for https://www.toolsura.com/ at correction level M and inspected its actual module grid:
| Property | Measured value |
|---|---|
| Grid size | 33 x 33 modules including quiet border |
| Dark modules | 340 of 1,089 (31.2%) |
| Finder patterns | Three 7x7 corner targets, 147 modules |
| Timing lines | Roughly 34 alternating modules |
The anatomy of a scannable square
- Finder patterns: the three nested-square corners. Cameras hunt these first, establishing position, scale, and rotation, which is why codes scan sideways or crumpled
- Timing patterns: alternating lines between finders that reveal the module grid pitch, letting readers measure every other square's coordinates
- Alignment patterns: small extra targets appearing in larger versions, correcting lens distortion across the symbol
- Format information: a strip declaring the correction level and mask in use, itself protected by its own error bits
- Data and correction region: everything else, the payload interleaved with redundancy codewords
The [Wikipedia QR code article](https://en.wikipedia.org/wiki/QR_code) diagrams each region across versions; the consistent design principle is that structure lives at fixed positions while data flows around it, so a reader always knows where to look before decoding anything.
Encoding modes: the format is smarter than it looks
Encoders choose among four segment modes: numeric, storing three digits per ten bits; alphanumeric, covering uppercase text with six bits per pair; byte mode for arbitrary text; and kanji for Japanese characters. A scanner-agnostic encoder mixes segments when it pays, a capability the [Wikipedia QR overview](https://en.wikipedia.org/wiki/QR_code) documents with capacity tables, which is why the same sentence encodes differently across generators. The practical consequence: digits-only payloads encode dramatically denser than mixed text, and lowercase URLs cost more modules than uppercase ones, a trick some print designers exploit deliberately.
Error correction: the repair machinery
Reed-Solomon coding adds redundant codewords so readers can reconstruct missing regions, with [Denso Wave's error correction documentation](https://www.qrcode.com/en/about/error_correction.html) defining four levels recovering roughly 7, 15, 25, and 30 percent of codewords. We tested the concept empirically rather than trusting percentages, painting random black blotches on real codes until scanners gave up:
| Level | Blotches survived | Pixels repainted at failure |
|---|---|---|
| L | 38 | 1.4% |
| M | 72 | 2.0% |
| H | 200+, still decoding when the test stopped | 3.9% and counting |
The test paints black over both dark and light modules, a harsher model than real-world stains, and one random pattern among many, so treat the numbers as a demonstration of ordering rather than certified limits. The ordering is the point: correction level buys damage tolerance visibly, and the same tradeoff appears in our [QR expiration guide](/blog/do-qr-codes-expire/) where density costs scale identically.
Masking: why no two codes look alike
Eight mask patterns exist, each a rule like flip modules where row plus column divides by three. Encoders score every mask against penalty rules, consecutive runs, oversized blocks, misleading finder lookalikes, and keep the least offensive, as [Denso's documentation](https://www.qrcode.com/en/about/error_correction.html) and the [format history](https://en.wikipedia.org/wiki/QR_code) both describe, which is why identical text from different generators often produces visibly different codes. Without masking, sparse payloads would leave vast blank regions that confuse grid clocking during scans. The [format history on Wikipedia](https://en.wikipedia.org/wiki/QR_code) credits this stage, alongside ECC, for the format's scanning reliability on cheap cameras.
What happens in the instant you scan
- The camera frame is searched for finder-pattern triples, localizing candidate codes at any rotation
- Grid pitch is measured from timing lines, then perspective distortion is unwarped
- Format information announces correction level and mask, which is then un-applied
- Codewords are read, errors are corrected up to the level's budget, and payload text emerges
Try the loop yourself: generate anything with the text to QR code generator, scan it back with the webcam scanner, and damage the print with a marker to watch correction spend its budget. The image decoder reads codes from photos when no camera is handy.
Elegant engineering in a square inch
How QR codes work reduces to four cooperating ideas: content-aware bit packing, redundancy that turns damage into a solvable puzzle, fixed-position structure that makes any camera a reader, and masking that keeps the grid legible. Our measurements put real numbers on the tradeoffs, correction level versus density, damage tolerance versus payload, and the tools above make the whole system tangible in minutes.


