5 releases (3 breaking)

new 0.4.1 Nov 5, 2024
0.4.0 Mar 13, 2024
0.3.0 Feb 18, 2024
0.2.2 Jan 30, 2024
0.1.0 Jan 21, 2024

#413 in Images

Apache-2.0

3.5MB
36K SLoC

C 21K SLoC // 0.1% comments C++ 14K SLoC // 0.1% comments Rust 823 SLoC // 0.0% comments Shell 2 SLoC

Contains (ELF exe/lib, 6.5MB) rustfmt

zxing-cpp

zxing-cpp is a Rust wrapper for the C++ library zxing-cpp.

It 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.

Usage

In your Cargo.toml:

[dependencies]
# `bundled` causes cargo to compile and statically link an up to
# date version of the c++ core library. This is the most convenient
# and safe way to build the library.
zxing-cpp = { version = "0.4.1", features = ["bundled", "image"] }

Simple example reading some barcodes from a jpg file:

use zxingcpp::BarcodeFormat;

fn main() -> anyhow::Result<()> {
	let image = image::open("some-image-file.jpg")?;

	let read_barcodes = zxingcpp::read()
		.formats(BarcodeFormat::QRCode | BarcodeFormat::LinearCodes)
		.try_invert(false);

	let barcodes = read_barcodes.from(&image)?;

	for barcode in barcodes {
		println!("{}: {}", barcode.format(), barcode.text());
	}

	Ok(())
}

Simple example creating a barcode and writing it to a svg file:

fn main() -> anyhow::Result<()> {
	let svg = zxingcpp::create(zxingcpp::BarcodeFormat::QRCode)
		.from_str("https://github.com/zxing-cpp/zxing-cpp")?
		.to_svg_with(&zxingcpp::write().scale(5))?;
	std::fs::write("zxingcpp.svg", svg)?;
	Ok(())
}

Note: This should currently be considered a pre-release. The API may change slightly to be even more idiomatic rust depending on community feedback.

Optional Features

zxing-cpp provides features that are behind Cargo features. They are:

  • bundled uses a bundled version of the zxing-cpp c++ library.
  • image allows convenient/implicit conversion between ImageView/Image andGreyImage/DynamicImage.

Benchmarking

To compare the performance of this Rust wrapper project with other available barcode scanner Rust libraries, I started the project zxing-bench. The README contains a few results to get an idea.

Dependencies