This program creates a QR code (Quick Response code) with the string provided from the command line, then:
- It prints the QR code on the terminal.
- It saves the QR code as a PNG file.
// You can verify the generated code with:
sudo apt install zbar-tools
zbarimg qrcode.png
libqrencode
is a C library designed for generating QR codes. It is widely used in various applications to create QR codes that can store information such as URLs, text, contact information, and more. The library is efficient, lightweight, and provides a straightforward API for developers to integrate QR code generation into their applications.
sudo apt install libqrencode-dev
// Or, on Termux (Android)
pkg install libqrencode
Used for producing image files in PNG format that contain the QR codes.
sudo apt install libpng-dev
// Or, on Termux
pkg install libpng
// Run
make
./qrgenerator <string_to_convert>
QR code generation involves encoding data into a 2D matrix of black and white squares. Each square, known as a module, represents either a binary 0 (white) or 1 (black). The pattern and arrangement of these modules are designed to allow accurate decoding, even with some level of distortion or damage. Here are the main characteristics of QR code generation:
A QR code consists of several key components, each serving a specific purpose:
- Location: Found in three corners of the QR code (top-left, top-right, bottom-left).
- Purpose: Used to detect and align the QR code during scanning, regardless of its orientation (rotation or tilt).
- Appearance: Large black-and-white square patterns, with an outer black square, a white square inside it, and a black square in the center.
- Location: Smaller squares located near the bottom-right corner and at multiple locations in larger QR codes.
- Purpose: Help correct distortion when scanning the QR code at an angle.
- Appearance: Single black squares surrounded by white, embedded within the data matrix.
- Location: Horizontal and vertical lines connecting the three finder patterns.
- Purpose: Ensure proper alignment and module placement.
- Appearance: Alternating black and white modules.
- Location: Empty border around the QR code.
- Purpose: Provides clear separation from other elements in the environment, ensuring accurate scanning.
- Appearance: White space surrounding the QR code.
- Modules: The smallest square units in the QR code, representing data or functional components. Each module is either black (binary
1
) or white (binary0
). - Data Encoding:
- The data is encoded using binary representations.
- Error correction codes (e.g., Reed-Solomon codes) are added to ensure the QR code can be decoded even if part of it is damaged.
- Encoded data is interleaved with error correction data and mapped into the QR code grid.
QR codes use error correction to restore data if part of the code is damaged or obscured. There are four levels of error correction:
- Level L (Low): Can restore up to 7% of the code.
- Level M (Medium): Can restore up to 15%.
- Level Q (Quartile): Can restore up to 25%.
- Level H (High): Can restore up to 30%.
- Purpose: To prevent large areas of the QR code from appearing uniformly black or white, which can interfere with scanning.
- Process: Different masking patterns (predefined algorithms) are applied to the QR code to balance the distribution of black and white modules. The pattern resulting in the least visual artifacts is chosen.
- Each module has a specific value depending on its function:
- Black Module: Represents a binary
1
. - White Module: Represents a binary
0
.
- Black Module: Represents a binary
The encoded data is combined with error correction codes, which introduce redundancy. This ensures that the QR code can still be decoded if up to a certain percentage of it is damaged or obscured.
QR code data is encoded into modules using the following steps:
- Encoding Mode Selection: Numeric, alphanumeric, binary, or Kanji encoding is chosen based on the input data.
- Data Segmentation: Input data is segmented and converted into binary form.
- Data Placement: Binary data is interleaved with error correction data and placed in the QR code grid.
When a QR code is scanned:
- Finder and alignment patterns are used to locate and orient the QR code.
- The scanner reads the modules and decodes the binary data.
- Error correction is applied to recover any corrupted data.
- The final data is interpreted based on the encoding mode.