demo_reader.rs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright 2024 Axel Waggershauser
  3. */
  4. // SPDX-License-Identifier: Apache-2.0
  5. use zxingcpp::*;
  6. fn main() -> anyhow::Result<()> {
  7. let filename = std::env::args().nth(1).expect("no image file name provided");
  8. let formats = std::env::args().nth(2);
  9. let fast = std::env::args().nth(3).is_some();
  10. let image = image::open(&filename)?;
  11. #[cfg(not(feature = "image"))]
  12. let lum_img = image.into_luma8();
  13. #[cfg(not(feature = "image"))]
  14. let iv = ImageView::from_slice(&lum_img, lum_img.width(), lum_img.height(), ImageFormat::Lum)?;
  15. let formats = BarcodeFormats::from_str(formats.unwrap_or_default())?;
  16. let read_barcodes = BarcodeReader::new()
  17. .formats(formats)
  18. .try_harder(!fast)
  19. .try_invert(!fast)
  20. .try_rotate(!fast)
  21. .try_downscale(!fast)
  22. .return_errors(true);
  23. #[cfg(feature = "image")]
  24. let barcodes = read_barcodes.from(&image)?;
  25. #[cfg(not(feature = "image"))]
  26. let barcodes = read_barcodes.from(iv)?;
  27. if barcodes.is_empty() {
  28. println!("No barcode found.");
  29. } else {
  30. for barcode in barcodes {
  31. println!("Text: {}", barcode.text());
  32. println!("Bytes: {:?}", barcode.bytes());
  33. println!("Format: {}", barcode.format());
  34. println!("Content: {}", barcode.content_type());
  35. println!("Identifier: {}", barcode.symbology_identifier());
  36. println!("EC Level: {}", barcode.ec_level());
  37. println!("Error: {}", barcode.error());
  38. println!("Rotation: {}", barcode.orientation());
  39. println!("Position: {}", barcode.position());
  40. println!();
  41. }
  42. }
  43. Ok(())
  44. }