mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
basic functions for rxing-cli
This commit is contained in:
@@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -662,3 +662,24 @@ impl fmt::Display for BitMatrix {
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -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<BarcodeFormat>,
|
||||
hints: &mut DecodingHintDictionary,
|
||||
) -> 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();
|
||||
|
||||
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
|
||||
)))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ use crate::{
|
||||
*
|
||||
* @author dswitkin@google.com (Daniel Switkin)
|
||||
*/
|
||||
#[derive(Default)]
|
||||
pub struct MultiFormatWriter;
|
||||
|
||||
impl Writer for MultiFormatWriter {
|
||||
|
||||
Reference in New Issue
Block a user