Understanding QR Codes: A Detailed Exploration

5 min readJan 9, 2025

QR Code, short for Quick Response Code, is a two-dimensional barcode invented by Denso Wave in 1994. Unlike traditional barcodes that store information in a linear format, QR codes store data in horizontal and vertical dimensions, allowing them to hold much more information.

QR codes have become ubiquitous in modern applications, from marketing and payments to authentication and information sharing. This article dives deep into their structure, functionality, underlying algorithms, and how to decode and interpret the data.

Structure of a QR Code

A QR code consists of various components, each serving a specific purpose to ensure accurate data encoding, alignment, and decoding. Below are the essential elements:

  1. Finder Patterns:
  • The three large squares at the corners of a QR code.
  • Help the scanner quickly locate the QR code and determine its orientation.

2. Alignment Patterns:

  • Smaller squares near the bottom-right corner.
  • Help correct distortions or perspective changes when scanning at an angle.

3. Timing Patterns:

  • Alternating black and white lines connecting the finder patterns.
  • Enable the scanner to determine the size and grid spacing of the QR code.

4. Quiet Zone:

  • A blank margin around the QR code.
  • Ensures the scanner can easily differentiate the QR code from the background.

5. Data Area:

  • It contains the actual encoded information, represented as black-and-white modules.

6. Error Correction Code:

  • Data redundancy ensures the QR code can still be read if partially damaged.

7. Format Information:

  • Encodes information about the QR code’s error correction level and masking pattern.

Data Encoding in QR Codes

QR codes store data as binary numbers (1s and 0s). This section explains the steps involved in encoding information into a QR code:

  1. Data Conversion:
  • The data (e.g., text, URL) is first converted into a binary format using the character encoding specified (e.g., UTF-8).

2. Data Segmentation:

  • Depending on the data type (numeric, alphanumeric, binary, or kanji), the input is segmented into appropriate formats.

For example:

  • Numeric mode: Encodes digits (0–9) in groups of three.
  • Alphanumeric mode: Encodes alphanumeric characters using a predefined table.
  • Binary mode: Encodes raw binary data.

3. Mode Indicator:

  • A 4-bit code indicating the type of data being encoded (e.g., 0001 for numeric, 0010 for alphanumeric).

4. Character Count Indicator:

  • Specifies the length of the input data.

5. Error Correction:

  • QR codes use Reed-Solomon error correction to add redundancy to the data. The amount of error correction is determined by one of four levels (L, M, Q, H).

6. Masking:

  • A masking pattern is applied to ensure even distribution of black and white modules. This prevents large blocks of similar colors that could confuse scanners.

7. Final Placement:

  • The binary data is placed into the QR code’s grid following a zigzag pattern, with error correction and format information added.

Decoding a QR Code: Converting Modules to Data

Decoding a QR code involves converting the black and white modules into binary data and interpreting it. Here’s how it works:

  1. Scanning and Alignment:
  • A QR code scanner identifies the finder patterns to determine the code’s position, orientation, and size.

2. Grid Sampling:

  • The scanner maps the black and white modules into a grid of 1s (black) and 0s (white).

3. Mask Removal:

  • The masking pattern applied during encoding is removed by XORing the data with the mask pattern.

4. Error Correction:

  • The Reed-Solomon algorithm is applied to detect and correct errors in the data.

5. Data Parsing:

  • The binary data is split into its components:
  • Mode Indicator: Identifies the data type.
  • Character Count: Specifies the number of characters.
  • Encoded Data: Contains the actual information.

6. Character Decoding:

  • The encoded data is interpreted based on the mode indicator, converting it back to text, a URL, or other types of data.

Example: Encoding and Decoding a QR Code

Encoding “HELLO123” into a QR Code:

  1. Convert to Alphanumeric:
  • Using the alphanumeric table: H = 17, E = 14, L = 21, O = 24, 1 = 1, 2 = 2, 3 = 3.
  • Combine into pairs: “HE” (174), “LL” (214), “O1” (241), “23” (23).

2. Convert to Binary:

  • 174 (Binary: 10101110), 214 (Binary: 11010110), etc.

3. Add Mode Indicator and Error Correction:

  • Append the mode indicator and error correction bits.

4. Place Data in Grid:

  • Fill the QR code grid with binary values, alternating in a zigzag pattern.

Decoding a Scanned QR Code:

1. Scan and Identify Finder Patterns.

2. Map Modules to Binary:

  • Read the black (1) and white (0) modules.

3. Remove Mask and Apply Error Correction.

4. Parse Data:

  • Extract the mode, length, and actual data.

5. Convert to Human-Readable Format:

  • Use the encoding table to reconstruct “HELLO123.”

Algorithms Behind QR Codes

Reed-Solomon Error Correction

The Reed-Solomon algorithm adds redundant data to ensure the QR code remains readable even if partially damaged. It involves:

  • Dividing data into blocks.
  • Generating parity symbols using polynomial division.
  • Appending parity symbols to the original data.

Mask Patterns

Masking ensures an even distribution of black-and-white modules. There are eight possible mask patterns, and the one minimizing consecutive modules and maximizing contrast is chosen.

Each mask follows a formula, such as:

  • Mask 0: (row + column) % 2 == 0
  • Mask 1: row % 2 == 0

Applications of QR Codes

  1. Payments: UPI, PayPal, and other digital payment platforms.
  2. Authentication: Boarding passes, event tickets, and login authentication.
  3. Marketing: Linking to websites, apps, and product information.
  4. Inventory Management: Tracking products in warehouses.

Creating a QR Code Yourself

You can generate a QR code using libraries like Python’s qrcode module:

import qrcode
# Create QR code
qr = qrcode.QRCode(
version=1, # Controls the size of the QR Code
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data("https://example.com")
qr.make(fit=True)
# Save QR code as an image
img = qr.make_image(fill_color="black", back_color="white")
img.save("example_qr.png")

Conclusion

QR codes are a remarkable innovation, blending simplicity with advanced technology. Understanding their structure, encoding process, and underlying algorithms not only demystifies their functionality but also opens doors to creative applications. By decoding the black-and-white patterns, we unlock a wealth of information — all within the palm of our hands.

--

--

Sumit Bopche
Sumit Bopche

No responses yet