zxing-cpp provides Python bindings for the high-performance C++ port of the ZXing barcode library. It supports reading and writing a wide range of 1D and 2D barcode formats like QR Code, Data Matrix, UPC-A, and Code 128. The current version is 3.0.0, and it maintains an active development and release cadence, typically tied to its underlying C++ library.
pip install zxing-cppVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to both read and write barcodes using `zxing-cpp`. Reading typically involves loading an image with OpenCV (`cv2`) and passing it to `zxingcpp.read_barcodes`. Writing involves creating a barcode object with `zxingcpp.create_barcode`, converting it to an image array, and saving it using a library like Pillow (`PIL`).
Update barcode creation and writing code to use the new `create_barcode` API and the updated `BarcodeFormat` and `BarcodeFormats` classes as shown in the quickstart example. Refer to the official README for details.
Ensure your development environment meets the C++20 compiler and CMake version requirements if you are building `zxing-cpp` from source or using the `--no-binary zxing-cpp` installation option.
Always verify file paths using `os.path.exists()` or convert to absolute paths with `os.path.abspath()`. Use raw strings (`r'C:\path\to\file'`) or forward slashes (`'C:/path/to/file'`) for consistency on Windows.
If your application frequently processes images with many barcodes, consider pre-processing the image to isolate individual barcodes, or evaluate alternative libraries if multi-barcode detection is critical and `zxing-cpp`'s performance is insufficient for your specific use case.
Double-check the file path. Use an absolute path or ensure your relative path is correct from the script's current working directory (`os.getcwd()`). On Windows, ensure path separators are handled correctly (e.g., `r'C:\path\image.png'` or `'C:/path/image.png'`).
Improve image quality (ensure good lighting, focus, contrast). Try rotating or downscaling the image before passing it to `read_barcodes`. Ensure the barcode format is one of the supported types by `zxing-cpp`. For critical cases, inspect image binarization/thresholding parameters or consider `try_rotate` and `try_downscale` options in `read_barcodes` for more robust detection.
Consult the `zxing-cpp` documentation or the `PYBIND11_MODULE` definition in the C++ source for the exact properties and their types on the `Barcode` object. For position, it's typically a string that needs parsing if numerical coordinates are required. Common attributes include `text`, `format`, `content_type`, `position`.