From 23942e7db264c9cbabf80d0921286775bba31c77 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Sat, 7 Jan 2023 14:03:48 -0600 Subject: [PATCH] basic functions for rxing-cli --- Cargo.toml | 2 +- src/barcode_format.rs | 26 ++++++++++++++++++++++++++ src/common/bit_matrix.rs | 21 +++++++++++++++++++++ src/helpers.rs | 18 ++++++++++++++++-- src/multi_format_writer.rs | 1 + 5 files changed, 65 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 39116f5..47461d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rxing" -version = "0.2.6" +version = "0.2.7" description="A rust port of the zxing barcode library." license="Apache-2.0" repository="https://github.com/hschimke/rxing" diff --git a/src/barcode_format.rs b/src/barcode_format.rs index 82c4e14..3d9b193 100644 --- a/src/barcode_format.rs +++ b/src/barcode_format.rs @@ -104,3 +104,29 @@ impl Display for BarcodeFormat { ) } } + +/// Defaults to QRCode if no proper formatting available +impl From<&str> for BarcodeFormat { + fn from(value: &str) -> Self { + match value.to_lowercase().as_str() { + "aztec" => BarcodeFormat::AZTEC, + "codabar" => BarcodeFormat::CODABAR, + "code 39" | "code_39" | "code39" => BarcodeFormat::CODE_39, + "code 93" | "code_93" | "code93" => BarcodeFormat::CODE_93, + "code 128" | "code_129" | "code128" => BarcodeFormat::CODE_128, + "datamatrix" |"data matrix" | "data_matrix"=> BarcodeFormat::DATA_MATRIX, + "ean 8" | "ean_8" | "ean8" => BarcodeFormat::EAN_8, + "ean 13" |"ean_13" | "ean13"=> BarcodeFormat::EAN_13, + "itf" | "itf_code" | "itf14" | "itf 14" | "itf_14" | "interleaved 2 of 5"=> BarcodeFormat::ITF, + "maxicode" | "maxi_code"=> BarcodeFormat::MAXICODE, + "pdf 417" | "pdf_417" | "pdf417"=> BarcodeFormat::PDF_417, + "qrcode" | "qr_code" | "qr code"=> BarcodeFormat::QR_CODE, + "rss 14" | "rss_14" | "rss14" | "gs1 databar" => BarcodeFormat::RSS_14, + "rss expanded" | "expanded rss" | "rss_expanded"=> BarcodeFormat::RSS_EXPANDED, + "upc a" |"upc_a" | "upca"=> BarcodeFormat::UPC_A, + "upc e" |"upc_e" | "upce" => BarcodeFormat::UPC_E, + "upc ean extension" | "upc extension" | "ean extension" | "upc/ean extension" | "upc_ean_extension"=> BarcodeFormat::UPC_EAN_EXTENSION, + _ => BarcodeFormat::QR_CODE, + } + } +} diff --git a/src/common/bit_matrix.rs b/src/common/bit_matrix.rs index 68b3e27..d12721a 100644 --- a/src/common/bit_matrix.rs +++ b/src/common/bit_matrix.rs @@ -662,3 +662,24 @@ impl fmt::Display for BitMatrix { write!(f, "{}", self.toString("X ", " ")) } } + +#[cfg(feature = "image")] +impl From for image::DynamicImage { + fn from(value: BitMatrix) -> Self { + (&value).into() + } +} + +#[cfg(feature = "image")] +impl From<&BitMatrix> for image::DynamicImage { + fn from(value: &BitMatrix) -> Self { + let mut pixels = image::ImageBuffer::new(value.width, value.height); + + for y in 0..value.height { + for x in 0..value.width { + pixels.put_pixel(x, y, image::Luma([if value.get(x, y) {u8::MIN} else {u8::MAX} ])); + } + } + pixels.into() + } +} \ No newline at end of file diff --git a/src/helpers.rs b/src/helpers.rs index 9553758..b1c2232 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -4,7 +4,7 @@ use std::{ }; use crate::{ - common::HybridBinarizer, + common::{BitMatrix, HybridBinarizer}, multi::{GenericMultipleBarcodeReader, MultipleBarcodeReader}, BarcodeFormat, BinaryBitmap, BufferedImageLuminanceSource, DecodeHintType, DecodeHintValue, DecodingHintDictionary, Exceptions, Luma8LuminanceSource, MultiFormatReader, RXingResult, @@ -25,7 +25,9 @@ pub fn detect_in_file_with_hints( barcode_type: Option, hints: &mut DecodingHintDictionary, ) -> Result { - let img = image::open(file_name).unwrap(); + let Ok(img) = image::open(file_name) else { + return Err(Exceptions::IllegalArgumentException(Some(format!("file '{}' not found or cannot be opened", file_name)))); + }; let mut multi_format_reader = MultiFormatReader::default(); if let Some(bc_type) = barcode_type { @@ -138,3 +140,15 @@ pub fn detect_multiple_in_luma_with_hints( hints, ) } + +#[cfg(feature = "image")] +pub fn save_image(file_name: &str, bit_matrix: &BitMatrix) -> Result<(), Exceptions> { + let image: image::DynamicImage = bit_matrix.into(); + match image.save(file_name) { + Ok(_) => Ok(()), + Err(err) => Err(Exceptions::IllegalArgumentException(Some(format!( + "could not save file '{}': {}", + file_name, err + )))), + } +} diff --git a/src/multi_format_writer.rs b/src/multi_format_writer.rs index 770e3ea..6f02c34 100644 --- a/src/multi_format_writer.rs +++ b/src/multi_format_writer.rs @@ -34,6 +34,7 @@ use crate::{ * * @author dswitkin@google.com (Daniel Switkin) */ +#[derive(Default)] pub struct MultiFormatWriter; impl Writer for MultiFormatWriter {