Skip to content

Latest commit

 

History

History
66 lines (47 loc) · 2.21 KB

README.md

File metadata and controls

66 lines (47 loc) · 2.21 KB

Python bindings for zxing-cpp

PyPI

The package uses scikit-build-core as its build backend and nanobind for the Python bindings.

Installation

pip install zxing-cpp

To build from a checked out / extracted soruce tree, a suitable build environment including a C++20 compiler is required:

pip install .

Usage

Reading barcodes

import cv2, zxingcpp

img = cv2.imread('test.png')
barcodes = zxingcpp.read_barcodes(img)
for barcode in barcodes:
	print('Found barcode:'
		f'\n Text:    "{barcode.text}"'
		f'\n Format:   {barcode.format}'
		f'\n Content:  {barcode.content_type}'
		f'\n Position: {barcode.position}')
if len(barcodes) == 0:
	print("Could not find any barcode.")

The read_barcodes function can accept images in various formats, including OpenCV, Pillow, and Qt images, as well as NumPy arrays with dtype uint8. Grayscale arrays can have shape (height, width) or (height, width, 1). Three-channel arrays are interpreted as BGR, matching OpenCV's default imread output. To explicitly specify the image buffer memory layout including the number of channels, the shape and the stride of any given memory view / array, you can use the ImageView class:

view = zxingcpp.ImageView(memoryview(img), width, height, zxingcpp.ImageFormat.RGBA, row_stride, pix_stride)
barcodes = zxingcpp.read_barcodes(view)

Writing barcodes

import zxingcpp
from PIL import Image

barcode = zxingcpp.create_barcode('This is a test', zxingcpp.BarcodeFormat.QRCode, ec_level = "50%")

img = barcode.to_image(scale = 5)
Image.fromarray(img).save("test.png")

svg = barcode.to_svg(add_quiet_zones = False)
with open("test.svg", "w") as svg_file:
	svg_file.write(svg)

To get a full list of available parameters for read_barcodes and create_barcode as well as the properties of the Barcode objects, have a look at the nanobind module definition in this C++ source file.