mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
basic luma8 source
This commit is contained in:
@@ -8,7 +8,8 @@ use crate::{
|
||||
common::HybridBinarizer,
|
||||
multi::{GenericMultipleBarcodeReader, MultipleBarcodeReader},
|
||||
BarcodeFormat, BinaryBitmap, BufferedImageLuminanceSource, DecodeHintType, DecodeHintValue,
|
||||
DecodingHintDictionary, Exceptions, MultiFormatReader, RXingResult, Reader,
|
||||
DecodingHintDictionary, Exceptions, Luma8LuminanceSource, MultiFormatReader, RXingResult,
|
||||
Reader,
|
||||
};
|
||||
|
||||
#[cfg(feature = "image")]
|
||||
@@ -74,27 +75,67 @@ pub fn detect_multiple_in_file_with_hints(
|
||||
}
|
||||
|
||||
pub fn detect_in_luma(
|
||||
luma: &[u8],
|
||||
luma: Vec<u8>,
|
||||
width: u32,
|
||||
height: u32,
|
||||
barcode_type: Option<BarcodeFormat>,
|
||||
) -> Result<RXingResult, Exceptions> {
|
||||
detect_in_luma_with_hints(luma, barcode_type, &mut HashMap::new())
|
||||
detect_in_luma_with_hints(luma, height, width, barcode_type, &mut HashMap::new())
|
||||
}
|
||||
|
||||
pub fn detect_in_luma_with_hints(
|
||||
luma: &[u8],
|
||||
luma: Vec<u8>,
|
||||
width: u32,
|
||||
height: u32,
|
||||
barcode_type: Option<BarcodeFormat>,
|
||||
hints: &mut DecodingHintDictionary,
|
||||
) -> Result<RXingResult, Exceptions> {
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
let mut multi_format_reader = MultiFormatReader::default();
|
||||
|
||||
if let Some(bc_type) = barcode_type {
|
||||
hints.insert(
|
||||
DecodeHintType::POSSIBLE_FORMATS,
|
||||
DecodeHintValue::PossibleFormats(HashSet::from([bc_type])),
|
||||
);
|
||||
}
|
||||
|
||||
if !hints.contains_key(&DecodeHintType::TRY_HARDER) {
|
||||
hints.insert(DecodeHintType::TRY_HARDER, DecodeHintValue::TryHarder(true));
|
||||
}
|
||||
|
||||
multi_format_reader.decode_with_hints(
|
||||
&mut BinaryBitmap::new(Rc::new(RefCell::new(HybridBinarizer::new(Box::new(
|
||||
Luma8LuminanceSource::new(luma, width, height),
|
||||
))))),
|
||||
&hints,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn detect_multiple_in_luma(luma: &[u8]) -> Result<Vec<RXingResult>, Exceptions> {
|
||||
detect_multiple_in_luma_with_hints(luma, &mut HashMap::new())
|
||||
pub fn detect_multiple_in_luma(
|
||||
luma: Vec<u8>,
|
||||
width: u32,
|
||||
height: u32,
|
||||
) -> Result<Vec<RXingResult>, Exceptions> {
|
||||
detect_multiple_in_luma_with_hints(luma, width, height, &mut HashMap::new())
|
||||
}
|
||||
|
||||
pub fn detect_multiple_in_luma_with_hints(
|
||||
file_name: &[u8],
|
||||
luma: Vec<u8>,
|
||||
width: u32,
|
||||
height: u32,
|
||||
hints: &mut DecodingHintDictionary,
|
||||
) -> Result<Vec<RXingResult>, Exceptions> {
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
let multi_format_reader = MultiFormatReader::default();
|
||||
let mut scanner = GenericMultipleBarcodeReader::new(multi_format_reader);
|
||||
|
||||
if !hints.contains_key(&DecodeHintType::TRY_HARDER) {
|
||||
hints.insert(DecodeHintType::TRY_HARDER, DecodeHintValue::TryHarder(true));
|
||||
}
|
||||
|
||||
scanner.decode_multiple_with_hints(
|
||||
&mut BinaryBitmap::new(Rc::new(RefCell::new(HybridBinarizer::new(Box::new(
|
||||
Luma8LuminanceSource::new(luma, width, height),
|
||||
))))),
|
||||
&hints,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -95,4 +95,7 @@ mod multi_format_reader;
|
||||
pub use multi_format_reader::*;
|
||||
|
||||
// Simple methods to help detect barcodes in common situations
|
||||
pub mod helpers;
|
||||
pub mod helpers;
|
||||
|
||||
mod luma_luma_source;
|
||||
pub use luma_luma_source::*;
|
||||
63
src/luma_luma_source.rs
Normal file
63
src/luma_luma_source.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use crate::LuminanceSource;
|
||||
|
||||
pub struct Luma8LuminanceSource(u32, u32, Vec<u8>, bool);
|
||||
impl LuminanceSource for Luma8LuminanceSource {
|
||||
fn getRow(&self, y: usize) -> Vec<u8> {
|
||||
self.2
|
||||
.chunks_exact(y * self.0 as usize)
|
||||
.skip(y)
|
||||
.take(1)
|
||||
.flatten()
|
||||
.map(|byte| Self::invert_if_should(*byte, self.3))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn getMatrix(&self) -> Vec<u8> {
|
||||
self.2
|
||||
.iter()
|
||||
.map(|byte| Self::invert_if_should(*byte, self.3))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn getWidth(&self) -> usize {
|
||||
self.0 as usize
|
||||
}
|
||||
|
||||
fn getHeight(&self) -> usize {
|
||||
self.1 as usize
|
||||
}
|
||||
|
||||
fn invert(&mut self) {
|
||||
self.3 = !self.3;
|
||||
}
|
||||
|
||||
fn isCropSupported(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn crop(
|
||||
&self,
|
||||
_left: usize,
|
||||
_top: usize,
|
||||
_width: usize,
|
||||
_height: usize,
|
||||
) -> Result<Box<dyn LuminanceSource>, crate::Exceptions> {
|
||||
Err(crate::Exceptions::UnsupportedOperationException(Some(
|
||||
"This luminance source does not support cropping.".to_owned(),
|
||||
)))
|
||||
}
|
||||
}
|
||||
impl Luma8LuminanceSource {
|
||||
pub fn new(source: Vec<u8>, width: u32, height: u32) -> Self {
|
||||
Self(width, height, source, false)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn invert_if_should(byte: u8, invert: bool) -> u8 {
|
||||
if invert {
|
||||
255 - byte
|
||||
} else {
|
||||
byte
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -135,6 +135,7 @@ pub trait LuminanceSource {
|
||||
)))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn invert_block_of_bytes(&self, vec_to_invert: Vec<u8>) -> Vec<u8> {
|
||||
let mut iv = vec_to_invert;
|
||||
for itm in iv.iter_mut() {
|
||||
|
||||
Reference in New Issue
Block a user