ZXing-C++ ("zebra crossing") is an open-source, multi-format linear/matrix barcode image processing library implemented in C++.
It was originally ported from the Java ZXing Library but has been developed further and now includes many improvements in terms of runtime and detection performance. It can both read and write barcodes in a number of formats.
You can sponsor this library at GitHub Sponsors.
Named Sponsors:
Thanks a lot for your contribution!
- Written in pure C++17 (/C++20), no third-party dependencies (for the library itself)
- Thread safe
- Wrappers/Bindings for:
Linear product | Linear industrial | Matrix |
---|---|---|
UPC-A | Code 39 | QR Code |
UPC-E | Code 93 | Micro QR Code |
EAN-8 | Code 128 | rMQR Code |
EAN-13 | Codabar | Aztec |
DataBar | DataBar Expanded | DataMatrix |
DX Film Edge | PDF417 | |
ITF | MaxiCode (partial) |
[Note:]
- DataBar used to be called RSS.
- DataBar, DX Film Edge, MaxiCode, Micro QR Code and rMQR Code are not supported for writing.
- Building with C++20 (see CMakeLists.txt) changes the behaviour of the library: it then supports multi-symbol and position independent detection for DataMatrix. This comes at a noticable performace cost. C++20 is enabled by default for the Android, iOS, Python and WASM wrappers.
- Load your image into memory (3rd-party library required).
- Call
ReadBarcodes()
fromReadBarcode.h
, the simplest API to get a list ofResult
objects.
A very simple example looks like this:
#include "ZXing/ReadBarcode.h"
#include <iostream>
int main(int argc, char** argv)
{
int width, height;
unsigned char* data;
// load your image data from somewhere. ImageFormat::Lum assumes grey scale image data.
auto image = ZXing::ImageView(data, width, height, ZXing::ImageFormat::Lum);
auto options = ZXing::ReaderOptions().setFormats(ZXing::BarcodeFormat::Any);
auto results = ZXing::ReadBarcodes(image, options);
for (const auto& r : results)
std::cout << ZXing::ToString(r.format()) << ": " << r.text() << "\n";
return 0;
}
To see the full capability of the API, have a look at ZXingReader.cpp
.
[Note: At least C++17 is reqired on the client side to use the API.]
- Create a
MultiFormatWriter
instance with the format you want to generate. Set encoding and margins if needed. - Call
encode()
with text content and the image size. This returns aBitMatrix
which is a binary image of the barcode wheretrue
== visual black andfalse
== visual white. - Convert the bit matrix to your native image format. See also the
ToMatrix<T>(BitMatrix&)
helper function.
As an example, have a look at ZXingWriter.cpp
.
[Note: those live demos are not necessarily fully up-to-date at all times.]
These are the generic instructions to build the library on Windows/macOS/Linux. For details on how to build the individual wrappers, follow the links above.
- Make sure CMake version 3.15 or newer is installed.
- Make sure a C++17 compliant compiler is installed (minimum VS 2019 16.8 / gcc 7 / clang 5).
- See the cmake
BUILD_...
options to enable the testing code, python wrapper, etc.
git clone https://github.com/zxing-cpp/zxing-cpp.git --single-branch --depth 1
cmake -S zxing-cpp -B zxing-cpp.release -DCMAKE_BUILD_TYPE=Release
cmake --build zxing-cpp.release -j8 --config Release
[Note: binary packages are available for/as vcpkg, conan, mingw and a bunch of linux distributions.]