ReCaseText
9 min read

How QR Codes Work: Structure, Encoding, and Error Correction Explained

QR codes encode data in a pattern of black and white squares that any smartphone can read. This guide explains the structure — finder patterns, encoding modes, Reed-Solomon error correction, and version capacity.

QR codes are everywhere — restaurant menus, concert tickets, payment terminals, product packaging, business cards, vaccination certificates, and billboard advertisements. A smartphone camera points at a pattern of black and white squares, and instantly you're directed to a URL, shown a Wi-Fi password, or presented with contact information. The technology feels almost magical in its speed and reliability, but the engineering behind it is methodical and elegant.

This article explains how QR codes actually work: the visual structure that makes them scannable, the encoding modes that convert data into binary, the Reed-Solomon error correction that keeps them readable even when damaged, and the version system that determines how much data a QR code can hold.

The Visual Structure

Every QR code contains several structural elements that serve specific purposes. Understanding these elements explains why QR codes look the way they do.

Finder patterns are the three large squares in the top-left, top-right, and bottom-left corners. Each consists of a 7x7 module (the smallest square unit in a QR code) pattern: a 3x3 black center, surrounded by a 1-module white border, surrounded by a 1-module black border. These patterns are what allows a scanner to instantly locate and orient the QR code in an image, regardless of the viewing angle. The specific ratio of black-white-black-white-black (1:1:3:1:1) when scanned horizontally or vertically through a finder pattern is unique in the QR code and can be detected at any rotation.

The reason there are three finder patterns (not four) is deliberate: three points define the orientation unambiguously. If there were four identical corner patterns, the scanner couldn't distinguish between a 0-degree and 90-degree rotation. With three, the "missing" corner tells the scanner which way the code is oriented.

Alignment patterns are smaller square markers (5x5 modules) placed at specific positions throughout larger QR codes. Version 1 (the smallest, 21x21 modules) has no alignment patterns. Version 2 and above have one or more. Version 40 (the largest, 177x177 modules) has 46 alignment patterns. These help the scanner correct for perspective distortion — when a QR code is photographed at an angle, the alignment patterns provide reference points for geometric correction.

Timing patterns are alternating black and white module rows that run horizontally between the top-left and top-right finder patterns, and vertically between the top-left and bottom-left finder patterns. They tell the scanner the exact module size and grid spacing, ensuring accurate sampling of each module even if the image is slightly stretched or skewed.

The format information area (adjacent to the finder patterns) encodes two pieces of metadata: the error correction level (L, M, Q, or H) and the mask pattern used on the data area. This information is stored twice (for redundancy) and is protected by its own error correction coding.

The version information area (in versions 7 and above) encodes the QR code's version number, allowing the scanner to determine the grid size. This is also stored twice with error correction.

The data area is everything else — the modules that actually encode your data, interleaved with error correction codewords.

The quiet zone is a mandatory border of white space (at least 4 modules wide) surrounding the entire QR code. Without it, the scanner might mistake nearby visual elements for part of the code.

Encoding Modes

QR codes support four standard encoding modes, each optimized for a different type of data.

Numeric mode encodes only digits 0-9. It's the most efficient mode — three digits are encoded in 10 bits (since 999 fits in 10 bits), giving approximately 3.33 bits per character. A QR code in numeric mode can hold up to 7,089 digits at the largest version with the lowest error correction.

Alphanumeric mode encodes digits 0-9, uppercase letters A-Z, and nine special characters (space, dollar sign, percent, asterisk, plus, hyphen, period, forward slash, and colon) — 45 characters total. Two characters are encoded in 11 bits (since 45 x 45 = 2,025 fits in 11 bits), giving approximately 5.5 bits per character. Maximum capacity: 4,296 characters.

Byte mode encodes any 8-bit byte value, typically interpreted as ISO 8859-1 (Latin-1) or UTF-8. Each character uses 8 bits. This is the most commonly used mode for URLs, which contain lowercase letters and special characters not available in alphanumeric mode. Maximum capacity: 2,953 bytes.

Kanji mode encodes Japanese Kanji characters from the Shift JIS character set. Each character uses 13 bits. Maximum capacity: 1,817 characters.

The QR code encoder automatically selects the most efficient mode for the data, and can switch modes within a single QR code if different segments of the data are best encoded differently. For example, a URL like https://example.com/page?id=12345 might encode the numeric portion in numeric mode and the rest in byte mode.

To understand how text becomes binary data before QR encoding, see our article on text to binary, ASCII, and Morse encoding. Our text to binary converter shows the binary representation that underlies all digital encoding.

Error Correction: Reed-Solomon Codes

The feature that makes QR codes remarkably resilient is Reed-Solomon error correction. This is the same class of error-correcting codes used in CDs, DVDs, Blu-ray discs, satellite communications, and deep-space probes. It allows a QR code to be read correctly even when part of the code is obscured, damaged, or dirty.

QR codes offer four error correction levels, each trading data capacity for resilience.

Level L (Low) can recover up to 7% of damaged codewords. This level maximizes data capacity — use it when the QR code will be displayed on a screen or printed in controlled conditions where damage is unlikely.

Level M (Medium) can recover up to 15% of damaged codewords. This is the default level for most QR code generators and provides a good balance between capacity and resilience.

Level Q (Quartile) can recover up to 25% of damaged codewords. Use this when the QR code will be printed on surfaces that might get scuffed, partially covered, or exposed to weather.

Level H (High) can recover up to 30% of damaged codewords. This is the level that allows QR codes to function even with a logo or image placed over part of the code — a common practice in marketing. The logo obscures some modules, but the error correction recovers the lost data.

Reed-Solomon works by treating the data as a polynomial over a finite field (Galois field GF(256)) and computing additional "check" codewords that are appended to the data. When the scanner reads the QR code, it uses these check codewords to detect and correct errors. The mathematics are complex — they involve polynomial division in finite fields — but the practical effect is simple: QR codes are far more robust than they need to be for clean, undamaged display conditions, and remarkably resilient when they're printed on physical surfaces that get dirty, scratched, or partially covered.

This is why QR codes can have logos in the center and still scan. The logo destroys the data modules it covers, but the Reed-Solomon error correction treats those destroyed modules as errors and corrects them — as long as the total damage doesn't exceed the error correction level's recovery capacity.

Versions and Capacity

QR codes come in 40 "versions," numbered 1 through 40. Each version increases the grid size by 4 modules in each dimension. Version 1 is 21x21 modules. Version 2 is 25x25. Version 10 is 57x57. Version 40 is 177x177. The version is automatically selected based on the amount of data and the error correction level.

At the maximum size (version 40) with the lowest error correction (Level L), a QR code can store 7,089 numeric characters, or 4,296 alphanumeric characters, or 2,953 bytes of binary data. In practice, most QR codes you encounter are versions 1 through 10 — a typical URL of 50 to 100 characters fits comfortably in a small QR code.

Higher versions produce larger QR codes with more modules, which means they need to be printed larger or scanned from closer to be readable. A version 40 QR code with 177x177 modules requires a much larger physical size and more precise scanning than a version 3 code with 29x29 modules. For most practical applications, keeping the data short (using URL shorteners if necessary) produces a smaller, more easily scannable QR code.

Data Masking

After the data and error correction codewords are placed in the QR code grid, a mask pattern is applied to the data area. The purpose of masking is to ensure that the QR code doesn't contain patterns that might confuse the scanner — for example, large areas of uniform black or white that could be mistaken for finder patterns, or patterns that make the grid alignment ambiguous.

QR codes define eight mask patterns (numbered 0 through 7), each a simple mathematical function that determines whether to flip a module's color based on its row and column position. The encoder tries all eight masks, evaluates the result using a set of penalty rules (which penalize large single-color blocks, patterns resembling finder patterns, and other problematic features), and selects the mask with the lowest penalty score. The selected mask number is stored in the format information area so the scanner knows which mask to reverse.

Generating QR Codes

Our QR code generator handles the entire encoding process — you enter text or a URL, select error correction level, and get a scannable QR code. The tool handles mode selection, version sizing, error correction calculation, data placement, and masking automatically.

For practical use, keep these guidelines in mind. Shorter data produces smaller, more scannable codes — if your URL is long, consider a URL shortener. Higher error correction levels produce larger codes for the same data — use Level M for general purposes and Level H only when you need a logo overlay or expect physical damage. Test your QR codes on multiple devices before deploying them — edge cases in encoding, unusual characters, or very dense codes can cause scanning failures on some readers.

QR Codes vs. Other 2D Barcodes

QR codes are the most popular but not the only 2D barcode format. Data Matrix is commonly used in industrial settings (product marking, electronics) and can be smaller than QR codes for small amounts of data because it doesn't have the three large finder patterns. Aztec Code is used for boarding passes and transit tickets — it has a single central finder pattern and can encode data starting from a very small size. PDF417 is a stacked linear barcode used on ID cards, shipping labels, and some government documents — it's a 2D format but reads more like a stack of linear barcodes.

QR codes dominate consumer applications because of their fast scanning speed (the finder patterns enable instant location and orientation), high data capacity, strong error correction, and universal smartphone camera support.

The Bottom Line

QR codes are an elegant piece of applied mathematics. The finder patterns enable instant orientation. The multiple encoding modes optimize data density for different character sets. The Reed-Solomon error correction provides resilience against damage, dirt, and even intentional logo overlays. The version system scales from a 21x21 grid holding a few dozen characters to a 177x177 grid holding thousands. All of this happens invisibly every time you point your phone camera at a pattern of black and white squares — a process that takes milliseconds but represents decades of encoding theory, error correction research, and computer vision engineering.

References

QRcode.com — About QR Code — Official site from Denso Wave, the inventors of QR codes, with technical documentation.

QRcode.com — Error Correction Feature — Explanation of Reed-Solomon error correction levels in QR codes.

QRcode.com — Information Capacity and Versions — Version-by-version capacity tables for all encoding modes.

Wikipedia — QR Code — Comprehensive overview of QR code history, structure, and encoding.

Wikiversity — Reed-Solomon Codes for Coders — Detailed walkthrough of Reed-Solomon coding with QR code examples.