QR Code Generation in C++: QR-Code-generator Library

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

QR-Code-generator Library for C++: Complete Guide



QR codes have become an integral part of the modern digital world. They are used in advertising, logistics, payment systems, and even educational projects. If you are developing a C++ application and want to add QR code generation functionality, the QR-Code-generator library is a simple, lightweight, and efficient tool. In this article, we will thoroughly examine what this library is, how to install it, and provide working code examples.



What is QR-Code-generator?

QR-Code-generator is a cross-platform C++ library (also available in C, Python, Java) that allows you to create QR codes of various versions and error correction levels. Key advantages:

  • No external dependencies (works out of the box).
  • Supports all 40 QR code versions (from 21×21 to 177×177 modules).
  • Error correction levels: L (7%), M (15%), Q (25%), H (30%).
  • Output as a string (char*) or binary array.
  • Support for Unicode (UTF-8) and numeric/alphanumeric modes.

The library is ideal for embedding in server applications, command-line utilities, or educational C++ projects.



Installation and Setup

Installation is extremely simple. You can download the source code from Nayuki's GitHub repository or use a package manager (e.g., vcpkg). Let's look at the manual method:

  1. Download the files qrcodegen.hpp and qrcodegen.cpp.
  2. Place them in your project folder.
  3. Include the header file in your code: #include "qrcodegen.hpp".
  4. Compile the project with the -std=c++11 flag or higher.

Example compilation using g++:

g++ -std=c++11 main.cpp qrcodegen.cpp -o qr_generator


Main API Functions

The library provides a minimalistic yet powerful API. Main functions:

  • qrcodegen::QrCode::encodeText(text, ecl) — encodes a text string into a QR code with a specified error correction level.
  • qrcodegen::QrCode::encodeBinary(data, ecl) — encodes binary data.
  • getSize() — returns the size of the QR code (number of modules in width).
  • getModule(x, y) — checks if a module (pixel) is set at coordinates (x, y).


Example 1: Generating a QR Code and Printing to Console

The simplest example — create a QR code for a text string and display it as ASCII art. This code is useful for debugging or terminal demonstrations.

#include <iostream>#include <string>#include "qrcodegen.hpp"

using namespace std;using namespace qrcodegen;

int main() { // Create a QR code for text with error correction level H (30%) const QrCode qr = QrCode::encodeText("https://pythonlib.ru", QrCode::Ecc::HIGH); // Print the QR code to the console (█ characters and spaces) for (int y = 0; y < qr.getSize(); y++) { for (int x = 0; x < qr.getSize(); x++) { cout << (qr.getModule(x, y) ? "██" : " "); } cout << endl; } return 0;}

Result: The console will display a QR code that can be scanned by any smartphone. Level H provides maximum resistance to damage.



Example 2: Creating a QR Code with Binary Data

Sometimes you need to encode not text, but arbitrary bytes (e.g., serialized data). Use encodeBinary.

#include <vector>#include "qrcodegen.hpp"

using namespace qrcodegen;

int main() { // Prepare binary data (byte array) std::vector<uint8

Recommendations