QR codes (Quick Response codes) are two-dimensional barcodes that can store significantly more data than the one-dimensional barcodes on product packaging. A QR code can encode a URL, contact information, Wi-Fi credentials, or any short text string. Smartphones can decode them instantly using the camera. Understanding how they work illuminates a piece of engineering that is both elegant and clever.
The Structure of a QR Code
A QR code is a square grid of black and white modules (pixels). The three large square patterns in the corners are finder patterns — they allow a scanner to locate and orient the code even if it is photographed at an angle. The small square near the bottom-right is the alignment pattern, used to correct for image distortion. Timing patterns (alternating black and white modules running between the finder patterns) help the scanner determine the module grid. All of these are fixed; the remaining modules encode the actual data.
How Data Is Encoded
QR codes support several encoding modes optimized for different content. Numeric mode stores digits 0–9 using approximately 3.3 bits per character. Alphanumeric mode stores A–Z, 0–9, and a handful of symbols (space, $, %, *, +, -, ., /, :) using about 5.5 bits per character. Byte mode stores arbitrary bytes as 8 bits each, which allows any UTF-8 text. Kanji mode compresses Japanese characters to 13 bits each. The encoder automatically selects the most efficient mode for the input.
Error Correction
One of the most important features of QR codes is error correction. QR codes use Reed-Solomon error correction, which adds redundant data so the code can be decoded even if part of it is obscured, dirty, or damaged. There are four error correction levels: L (low, recovers 7% damage), M (medium, 15%), Q (quartile, 25%), and H (high, 30%). A higher level means more redundancy and a larger code for the same data. This is why QR codes with logos embedded in the center still scan — the logo covers part of the data, but error correction fills in the gaps.
QR Code Versions and Capacity
QR codes come in 40 versions, from Version 1 (21×21 modules) to Version 40 (177×177 modules). Each increase in version adds four modules per side. Higher versions can store more data but are larger and harder to scan at small sizes. A Version 1 code with maximum error correction can store only 7 numeric characters. A Version 40 code with minimum error correction can store 7,089 numeric characters or 4,296 alphanumeric characters. For typical use cases — a URL under 100 characters — Version 5 or lower is sufficient.
Generating and Scanning
QR code generation is entirely client-side — no network request is needed, and the generated code never leaves the user's device. Modern browsers can generate QR codes using JavaScript libraries that implement the encoding and Reed-Solomon math directly. Scanning is handled by the device camera and image processing; the scanner locates the finder patterns, extracts the grid, decodes the data, and applies error correction to recover the payload.