view mupdf-source/thirdparty/zxing-cpp/wrappers/rust/examples/demo_reader.rs @ 31:baeb8bdeff3a

Fortify sources using _FORTIFY_SOURCE=3 and also apply -fno-delete-null-pointer-checks. See: https://github.com/ossf/wg-best-practices-os-developers/issues/659.
author Franz Glasner <fzglas.hg@dom66.de>
date Sun, 21 Sep 2025 13:11:30 +0200
parents b50eed0cc0ef
children
line wrap: on
line source

/*
* Copyright 2024 Axel Waggershauser
*/
// SPDX-License-Identifier: Apache-2.0

use zxingcpp::*;

fn main() -> anyhow::Result<()> {
	let filename = std::env::args().nth(1).expect("no image file name provided");
	let formats = std::env::args().nth(2);
	let fast = std::env::args().nth(3).is_some();

	let image = image::open(&filename)?;

	#[cfg(not(feature = "image"))]
	let lum_img = image.into_luma8();
	#[cfg(not(feature = "image"))]
	let iv = ImageView::from_slice(&lum_img, lum_img.width(), lum_img.height(), ImageFormat::Lum)?;

	let formats = BarcodeFormats::from_str(formats.unwrap_or_default())?;
	let read_barcodes = BarcodeReader::new()
		.formats(formats)
		.try_harder(!fast)
		.try_invert(!fast)
		.try_rotate(!fast)
		.try_downscale(!fast)
		.return_errors(true);

	#[cfg(feature = "image")]
	let barcodes = read_barcodes.from(&image)?;
	#[cfg(not(feature = "image"))]
	let barcodes = read_barcodes.from(iv)?;

	if barcodes.is_empty() {
		println!("No barcode found.");
	} else {
		for barcode in barcodes {
			println!("Text:       {}", barcode.text());
			println!("Bytes:      {:?}", barcode.bytes());
			println!("Format:     {}", barcode.format());
			println!("Content:    {}", barcode.content_type());
			println!("Identifier: {}", barcode.symbology_identifier());
			println!("EC Level:   {}", barcode.ec_level());
			println!("Error:      {}", barcode.error());
			println!("Rotation:   {}", barcode.orientation());
			println!("Position:   {}", barcode.position());
			println!();
		}
	}

	Ok(())
}