Vexifa QR Code
Try the Generator →
Technical Guide

How QR Codes Work: Encoding, Modules, and Decoding Explained

By Dave Rupe

A QR code looks like a random grid of black and white squares, but every element has a precise purpose. Understanding the structure - finder patterns, error correction, encoding modes, and the decoding pipeline - demystifies how a phone can read one in a fraction of a second.

The Basic Structure: Modules and the Grid

Every QR code is built on a square grid of small squares called modules. Each module is either dark or light, representing a binary 1 or 0. The size of the grid - and therefore the total number of modules - is determined by the code's version. A Version 1 QR code is a 21x21 module grid (441 modules total). Each version up adds four modules per side, so Version 2 is 25x25, Version 3 is 29x29, and so on up to Version 40, which is 177x177 (31,329 modules total).

Not all modules store data. A significant portion of every QR code is occupied by structural patterns that help the decoder locate, orient, and correctly interpret the symbol. These functional patterns are defined by the ISO/IEC 18004 standard and cannot be modified without breaking the code.

Finder Patterns: The Three Corner Squares

The most recognizable feature of any QR code is the three large square patterns in the top-left, top-right, and bottom-left corners. These are called finder patterns (or position detection patterns), and they serve a critical role: they allow any decoder to locate the QR code within an image and determine its size and orientation regardless of how the code is positioned or what angle it is photographed from.

Each finder pattern consists of a 7x7 module square: a 3x3 dark center, surrounded by a ring of light modules, surrounded by a ring of dark modules. This creates a distinctive dark:light:dark:light:dark ratio of 1:1:3:1:1 that can be detected along any scan line through the center of the pattern. Crucially, this ratio is unique enough that it almost never appears by chance in surrounding content.

Between the finder patterns and the rest of the code sit separator strips - single-module-wide bands of light modules that visually isolate the finder patterns from the data area.

Timing Patterns and Alignment Patterns

Timing Patterns

Running horizontally and vertically between the finder patterns are the timing patterns - alternating rows and columns of dark and light modules. Their purpose is to help the decoder accurately determine the module grid spacing, especially if the code is slightly distorted or the camera is at a mild angle. The timing patterns also establish the coordinate system for mapping every module's row and column position.

Alignment Patterns

Larger QR codes (Version 7 and above) include one or more smaller square patterns within the data area called alignment patterns. These are 5x5 module squares with a specific dark-light-dark pattern, placed at mathematically defined positions across the grid. They help the decoder apply a perspective correction transformation, which compensates for camera angle distortion and makes large codes more reliable to scan from a slight viewing angle.

A Version 7 code has one alignment pattern. A Version 40 code has up to 46 alignment patterns distributed across the grid.

Format Information and Version Information

Two strips of modules adjacent to the finder patterns store format information: the error correction level (L, M, Q, or H) and the data mask pattern that was applied. This information is encoded twice for redundancy and protected by a BCH error correction code. The decoder reads format information first - before touching any data modules - so it knows how to process the rest of the symbol.

QR codes at Version 7 and above also include version information strips, which explicitly state the version number. This helps decoders handle large codes where the version cannot be reliably inferred from the symbol size alone.

Encoding Modes: How Data Becomes Bits

Before any data can be embedded in a QR code's modules, it must be converted to a binary bit stream using one of four encoding modes. The encoder selects the most efficient mode for the input data, or a mixture of modes can be used within a single code.

The choice of encoding mode significantly affects how much data a QR code of a given size can hold. A URL containing only digits and uppercase letters could theoretically use alphanumeric mode for part of the string, but in practice most URLs contain lowercase letters and slashes, so binary mode is used and each character costs 8 bits.

Data Masking: Why QR Codes Look Random

After encoding the data bits into module positions, the QR code standard applies a data mask. There are eight predefined mask patterns - mathematical formulas applied to every module in the data area that flip dark modules to light and vice versa based on the module's row and column coordinates.

The purpose of masking is to prevent large areas of uniform dark or light modules, which would confuse some decoders. The encoder evaluates all eight mask patterns against four penalty criteria (large solid areas, similar patterns to finder patterns, etc.) and selects the pattern with the lowest total penalty score. The selected mask number is then stored in the format information so the decoder knows which mask to reverse.

This masking process is why QR codes look like random noise to the human eye - the data has been intentionally scrambled to ensure a balanced distribution of dark and light modules across the grid.

Reed-Solomon Error Correction

The most remarkable feature of QR codes from an engineering standpoint is their built-in error correction. The data bytes are supplemented with Reed-Solomon error correction codewords, which add redundant mathematical information that allows the decoder to detect and fix errors caused by physical damage, dirt, or obstruction.

Reed-Solomon coding works by treating the data as coefficients of a polynomial, then computing additional codewords from that polynomial. If some codewords are damaged, the decoder can solve for the original polynomial - and thus the original data - as long as the number of damaged codewords does not exceed the correction capacity.

At error correction level H (the highest), 30% of the code's total codewords can be damaged and the data can still be recovered. This is why you can place a logo over a significant portion of a QR code and it still scans - the missing modules are reconstructed from the error correction data surrounding them.

How a Scanner Decodes a QR Code

The full decoding process, from camera frame to readable data, involves several steps happening in milliseconds:

  1. Image capture - The camera captures a video frame and converts it to a grayscale bitmap.
  2. Binarization - An adaptive threshold algorithm converts the grayscale image to black-and-white, distinguishing dark and light modules even in uneven lighting.
  3. Finder pattern detection - The software scans horizontal lines across the image looking for the 1:1:3:1:1 dark/light ratio that identifies finder patterns. When three such patterns are found in the correct geometric relationship, the QR code's position is established.
  4. Perspective correction - A projective transformation is computed to map the tilted or distorted camera view of the code into a flat, square grid.
  5. Module sampling - The decoder samples the center of each module in the corrected grid to determine whether it is dark or light, building the full binary matrix.
  6. Format information reading - The format modules are read to determine the error correction level and mask pattern.
  7. Unmasking - The mask pattern is reversed to reveal the original data bit stream.
  8. Error correction decoding - Reed-Solomon decoding identifies and fixes any damaged codewords.
  9. Data decoding - The bit stream is parsed according to the encoding mode indicators to reconstruct the original characters.
  10. Output - The decoded string is returned to the application, which interprets it as a URL, contact, WiFi credential, or other content type.

You can generate standards-compliant QR codes that compress and encode your data correctly at Vexifa QR Code, with options to choose error correction level and download in multiple formats.

Frequently Asked Questions

Why can QR codes still scan when damaged?

QR codes use Reed-Solomon error correction, which adds redundant data throughout the code. At error correction level H, up to 30% of the modules can be damaged or obscured and the decoder can still reconstruct the full original data. This is the same principle that makes it possible to place a logo over part of a QR code without preventing it from scanning.

What do the three squares in the corners do?

The three large square patterns in the corners are called finder patterns. They allow the decoder to locate the QR code in an image, determine its boundaries, and establish its orientation - even if the code is rotated, skewed, or photographed at an angle. The distinctive 1:1:3:1:1 ratio of their dark and light bands is detectable from any direction and any scan angle, making them the essential anchor of the entire decoding process.

How does a phone camera read a QR code so fast?

Modern phone cameras continuously capture video frames at 30 or 60 frames per second and run image processing algorithms in real time on the device's dedicated image signal processor and CPU. The software detects finder patterns in the frame, computes a perspective correction, samples each module's dark or light value, then runs Reed-Solomon decoding. On modern hardware, the entire pipeline typically completes in well under 100 milliseconds per frame.

What is the maximum QR code version?

The maximum QR code version is Version 40, which is a 177x177 module grid containing 31,329 modules total. At this version with the lowest error correction level (L), the code can hold up to 7,089 numeric characters, 4,296 alphanumeric characters, or 2,953 bytes of binary data. In practice, Version 40 codes are extremely dense and require a large printed size to scan reliably.

Create Your QR Code Free

No sign-up. No watermarks. Download in seconds.

Open Generator →

Related Articles