How Do QR Code Scanner Work in a Nutshell

Ashish Singh
12 min readDec 15, 2022

--

What is a QR Code

In a Nutshell, a QR code is an two-dimensional encoded information that can be read by a QR code reader or smartphones.

QR Code could be of any color combination, graphics and design. Although, the boring black and white QR Code image is most popular one.

Why Do We Need a QR Code

  • Laziness at its Core

With advancement in the technology and astronomical rise of automation, users have become Lazy and needs everything done in automated manner.

Any process that could be automated should be automated.

  • Increase in Mobile Centric Apps

In last one decade, the use of smart phones has seen an astronomical rise. Mobile phones are yet not very convenient tool for writing text because of the small real estate. Since, QR codes is simply an encoded information, it could be used to fill in lot of static data by simply scanning the QR code thus saving a lot of typing.

Smartphone (or tablet PC) keyboards are tricky compared to desktop/laptop keyboards and most of the time, people simply can’t be bothered to type in lot of information on their Smartphone.

Writing an URL using laptop keyboard is much simpler than writing the same URL using mobile phone keyboard.

Resurrection of QR Code

QR Code technology has been around for couple of decades now. It was first introduced in 1994 by a company called Denso Wave(Toyota). The tech was almost dying in terms of usage vs ROI until Covid happen in 2020 and suddenly social distancing became a norm.

One of the characteristics of a QR code is it can be read from a distance which was also a necessity during Covid. This lead to an increase in usage of QR codes across various industries such as marketing, retail, event management, transportation, and so on for sharing information without the need for a direct contact.

QR code gave businesses a new way to communicate with customers in a suddenly touchless society.

Difference Between Bar Code and QR Code

Bar Code

Bar code could only store information along one vertical, majorly horizontally.

QR code store information in both vertical which allow them to store lot more data than Bar code could store. We’ll see later how this capability of storing extra amount of data makes QR code such a versatile tech.

QR code is a two-dimensional barcode

How Does QR Code Works Even When Some Part of the QR Code Is Damaged?

QR codes are designed with data redundancy. Even if as much as 30% of the QR code is destroyed or difficult to read, the data can still be recovered.

Because of this the missing dots(data) can be recovered by looking at the remaining visible dots. More on this below.

Anatomy of a QR Code

Let’s look these segment in a PayMe QR Code

QR Code = Finder patterns + Alignment patterns + Timing patterns + Quiet Zone + Version Number + Format Information + Data Area

  • Module

The smallest building element of a QR Code represented by a small dark or light square. One module represents one bit: usually, dark module for the “1” and light module for the “0”. Modules are organized into rows and columns and form squared matrix.

  • Finder Pattern

The finder patterns are located in three corners of the QR code and are used to help the QR code reader locate and orientation of the QR code.

Each Finder Pattern is formed by an inner dark square (of size 3 × 3 modules) surrounded by a dark frame (of size 7 × 7 modules).Each Finder Pattern is formed by an inner dark square (of size 3 × 3 modules) surrounded by a dark frame (of size 7 × 7 modules).

  • Alignment pattern

The alignment patterns help the reader determine the position of the data cells. There may be none or more alignment patterns according to a version of a QR Code (QR Code version 1 has no alignment pattern).

  • Timing patterns

Timing patterns are horizontal and vertical lines formed by sequence of alternating dark and light modules that run through the center of the QR code which inter connect Finder Patterns and help the reader determine the size and orientation of the code.

The timing pattern is used to determine the size of a module, the number of rows and columns, and possible distortion of a code.

  • Quiet Zone

The quiet zone helps ensure that the code can be scanned without any interference from other visual elements in the environment. In general, the quiet zone should be at least four times the width of a single module (the smallest unit in the QR code grid) on all sides of the code. This helps ensure that the code can be read accurately from a variety of distances and angles.

  • Version Number

The version number specifies the size and capacity of the QR code

  • Format Information

Format information section specifies the error correction level and data encoding method used in the code.

  • Data Area

The data area is the portion of the QR code where the encoded data is stored. This area is protected by an error correction carried out via a Reed–Solomon algorithm (allows restoration of damaged data). This also means that a QR Code can be partially damaged and can still be entirely read out. QR Codes provide four user selectable levels of error correction: L (Low), M (Medium), Q (Quartile), and H (High). It means that up to approximately 7%, 15%, 25%, and 30% of the code words, which are damaged, can be restored [3]. Increasing the level of error correction reduces the available data capacity of a QR Code.

How QR Code allow you to correctly scan from any Direction?

Answer is Position Markers and Black Spaces.

Position markers: Also known as Finder Pattern. These are squares placed in a QR code’s top-left, top-right, and bottom-left corners. These markers let a smartphone camera or other device know where the edges of the QR Code are when scanning it.

Quiet Zone: QR codes are surrounded by blank space, the quiet zone, to help the computer determine where the QR code begins and ends. QR codes can include an optional logo in the middle.

Constant Part: Also known an alignment marker. The scanner uses them to ensure accurate alignment.

How is the data stored in a QR Code?

The data in a QR code is a series of dots in a square grid. Each dot represents a one and each blank a zero in binary code.

Each square in the grid represents a single bit of information and the arrangement of the squares encodes the information that the QR code is intended to store.

A QR code consists of black and white squares arranged in a specific pattern. The information that the QR code represents is encoded in the pattern of these squares.

What is the size limit for a QR Code?

QR Codes are available in 40 different versions (version 1 represents a QR Code with 21 × 21 modules and version 40 represents QR Code with 177 × 177 modules; the relation between size and version can be expressed as: Size = 21 + (Version − 1) × 4).

The amount of data that can be stored inside the QR Code depends on its Version.

What is Most Commonly used QR Code pattern?

In most cases, QR codes use black squares on a white background, making the dots easy to distinguish. However, this is not a strict requirement, and QR codes can use any color or shape for the dots and background.

Little Deep Dive Into How QR Code is Scanned Internally By The Scanner

We receive QR Codes from various angles and the scanner still able to determine the correct data. Let’s see some of the images of QR Codes that are cylindrical, tilted and small, close to one another. We’ll look into how these could be decoded by the scanner.

Covert the QR Code Image to Grayscale Image

First step is to convert the QR Code to grayscale image to reduce the range of colors.

Convert Grayscale to Binary Image

Adaptive thresholding is used to convert grayscale image into binary image.

Adaptive thresholding techniques is an effective way to separate the dark modules of a QR Code from the light ones, even for images with low contrast or uneven illumination

Detect 3 Finding Patterns and Width of QR Code

The binary image is scanned horizontally and then vertically to find all points matching the magic ratios 1:1:3:1:1. This ratio 1:1:3:1:1 (B:W:BBB:W:B), is the fixed pattern for finding pattern regardless of an angle of scanning.

The structure has the property that also when it is rotated, it is possible to make such a horizontal and vertical cut that dark and light points will alternate in the ratio 1:1:3:1:1

Then the points belonging to the same Finding pattern are combined and the angle between three FP is checked. This is performed by scanning image horizontally line by line from top to bottom, to find consecutive sequences of black and white points in a line matching the ratios 1:1:3:1:1 (B1:W2:BBB3:W4:B5 where Bx, Wx denotes the number of black, white points).

There are many other ways Finding Patterns are determined such as compression + KMP Algo, ML Model trained to detect Finder Patterns in QR Code, Horizontal and Vertical Scanning using Principal Component Analysis, etc. All these are out of scope of this article.

Excluding False Positives using Quiet Zones

To exclude false positive candidates for a QR Code, defined by a triplet of finder patterns, we verify the presence of the quiet zone around the QR Code and the timing pattern inside the QR Code.

Identity Quiet Zones

To verify the quiet zone, we check if there are only white points in the rectangular area of binary image which is parallel to line segments defined by FP1–FP2 and FP2–FP3. For fast inspection of line points we use Bresenham’s line algorithm.

Verify Timing Pattern for any distortion in the Image

We examine the rectangular area between inner corners of finder patterns.

We count the number of white and black points in the binary image and we calculate average length LAVG of white and black line segments and its standard deviation LSTDDEV. The timing pattern is expected to satisfy the following conditions:

Determine the Box Width

We find the point P3 extending the vector FP1 & FP3 using the expression

Identity the 4th Point to determine the image distortation

We can find the 4th point using the Edge Projection.

Looking closely into the image below once can identify possible deformation of QR Codes. Explaining this will probably deviate the article focus, hence not going into details of how edge projection works.

Identifying the 4th point help us also determine the possible deformation of the QR Code. We use standard deviation and tolerance levels to reach as close possible to original 4th point.

Decoding the QR Code

Once we determine the 4 points, we convert the binary image into binary matrix where in dark modules become binary 1 and light modules become 0.

This binary matrix is the input of a deterministic process that decodes the data stored in the QR Code.

Mapping QR Code to Binary matrix

Decoding the Data from the QR Code

After identifying the edges, the scanner begins at the bottom right of the QR Code. It then moves up two data modules at a time until it hits the first position marker. Then it moves two data modules to the left and goes down. It repeats this right-to-left, up-then-down zig-zag process until every data module is covered.

  1. Data start indicator — 4 data modules in the binary matrix
  2. Count of character — Next 8 data modules up from the start indicator. This determine the length of encoded data.
  3. Data Modules
  4. Data Over Indicator
  5. Error Correction Modules

Let’s see how we store and read data wikipedia.org

How Does Error Correction occur in QR Code?

We know a QR Code could read the correct data even if the QR Code is partially damaged.

Error correction works by adding extra data to the QR code in the form of additional modules (small black and white squares) arranged in specific patterns. This extra data allows a QR code reader to reconstruct the original information even if some of the code is missing or damaged.

The amount of error correction used in a QR code is represented by a “error correction level” (ECL), which can be one of four values: low, medium, quarter, or high. The higher the ECL, the more error correction data is added to the QR code, which means that the code can be read more accurately, even if it is damaged. However, this also means that the code will be larger and require more space.

The technique used for error correction is called Reed Solomon method.

The Reed Solomon method is a type of error-correcting code that can be used to detect and correct errors in data storage and transmission.

The basic idea behind Reed-Solomon coding is to add additional “check” symbols to a message, which can be used to detect and correct errors that may occur during transmission or storage.

Note: Reed Solomon in general much complex and deserves its own article to explain its various choices and it’s parameters. This article Practical Reed-Solomon for Programmers has some nice explanations about it.

A very simple Reed Solomon that encodes the message

from reedsolo import ReedSolomonError, ReedSolomonEncoder

# Generate a Reed-Solomon encoder, with 8-bit symbols and a codeword length of 10 symbols
encoder = ReedSolomonEncoder(8, 10)

# Encode the message "3-0 through Final"
message = "3-0 through to Final".encode("utf-8")
coded_message = encoder.encode(message)

# The coded message will have 10 symbols, with 2 extra symbols added for error correction
assert len(coded_message) == 10

# To decode the message, we can use the same Reed-Solomon encoder
decoded_message = encoder.decode(coded_message)

# The decoded message should be the same as the original message
assert decoded_message == message

A Very Simple QR Code Generator

import qrcode

# Create a QR code instance
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)

# Add data to the QR code
qr.add_data('If you do not pay me after scanning, I am going to send John Wick after you')

# Generate the QR code image
img = qr.make_image(fill_color="black", back_color="white")

# Save the QR code image to a file
img.save('pay-me-qr-code.png')

--

--