basic functions for rxing-cli

This commit is contained in:
Henry Schimke
2023-01-07 14:03:48 -06:00
parent dbd2ec9a39
commit 23942e7db2
5 changed files with 65 additions and 3 deletions

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "rxing" name = "rxing"
version = "0.2.6" version = "0.2.7"
description="A rust port of the zxing barcode library." description="A rust port of the zxing barcode library."
license="Apache-2.0" license="Apache-2.0"
repository="https://github.com/hschimke/rxing" repository="https://github.com/hschimke/rxing"

View File

@@ -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,
}
}
}

View File

@@ -662,3 +662,24 @@ impl fmt::Display for BitMatrix {
write!(f, "{}", self.toString("X ", " ")) write!(f, "{}", self.toString("X ", " "))
} }
} }
#[cfg(feature = "image")]
impl From<BitMatrix> 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()
}
}

View File

@@ -4,7 +4,7 @@ use std::{
}; };
use crate::{ use crate::{
common::HybridBinarizer, common::{BitMatrix, HybridBinarizer},
multi::{GenericMultipleBarcodeReader, MultipleBarcodeReader}, multi::{GenericMultipleBarcodeReader, MultipleBarcodeReader},
BarcodeFormat, BinaryBitmap, BufferedImageLuminanceSource, DecodeHintType, DecodeHintValue, BarcodeFormat, BinaryBitmap, BufferedImageLuminanceSource, DecodeHintType, DecodeHintValue,
DecodingHintDictionary, Exceptions, Luma8LuminanceSource, MultiFormatReader, RXingResult, DecodingHintDictionary, Exceptions, Luma8LuminanceSource, MultiFormatReader, RXingResult,
@@ -25,7 +25,9 @@ pub fn detect_in_file_with_hints(
barcode_type: Option<BarcodeFormat>, barcode_type: Option<BarcodeFormat>,
hints: &mut DecodingHintDictionary, hints: &mut DecodingHintDictionary,
) -> Result<RXingResult, Exceptions> { ) -> Result<RXingResult, Exceptions> {
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(); let mut multi_format_reader = MultiFormatReader::default();
if let Some(bc_type) = barcode_type { if let Some(bc_type) = barcode_type {
@@ -138,3 +140,15 @@ pub fn detect_multiple_in_luma_with_hints(
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
)))),
}
}

View File

@@ -34,6 +34,7 @@ use crate::{
* *
* @author dswitkin@google.com (Daniel Switkin) * @author dswitkin@google.com (Daniel Switkin)
*/ */
#[derive(Default)]
pub struct MultiFormatWriter; pub struct MultiFormatWriter;
impl Writer for MultiFormatWriter { impl Writer for MultiFormatWriter {