mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-27 21:02:35 +00:00
Fixes for all features
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
use std::{borrow::Cow, rc::Rc};
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use image::{DynamicImage, ImageBuffer, Luma};
|
use image::{DynamicImage, ImageBuffer, Luma};
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
@@ -8,16 +8,16 @@ use crate::{Binarizer, Exceptions, LuminanceSource};
|
|||||||
|
|
||||||
use super::{BitArray, BitMatrix};
|
use super::{BitArray, BitMatrix};
|
||||||
|
|
||||||
pub struct OtsuLevelBinarizer {
|
pub struct OtsuLevelBinarizer<LS: LuminanceSource> {
|
||||||
width: usize,
|
width: usize,
|
||||||
height: usize,
|
height: usize,
|
||||||
source: Box<dyn LuminanceSource>,
|
source: LS,
|
||||||
black_matrix: OnceCell<BitMatrix>,
|
black_matrix: OnceCell<BitMatrix>,
|
||||||
black_row_cache: Vec<OnceCell<BitArray>>,
|
black_row_cache: Vec<OnceCell<BitArray>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OtsuLevelBinarizer {
|
impl<LS: LuminanceSource> OtsuLevelBinarizer<LS> {
|
||||||
fn generate_threshold_matrix(source: &dyn LuminanceSource) -> Result<BitMatrix> {
|
fn generate_threshold_matrix<LS2: LuminanceSource>(source: &LS2) -> Result<BitMatrix> {
|
||||||
let image_buffer = {
|
let image_buffer = {
|
||||||
let Some(buff) : Option<ImageBuffer<Luma<u8>,Vec<u8>>> = ImageBuffer::from_vec(source.get_width() as u32, source.get_height() as u32, source.get_matrix()) else {
|
let Some(buff) : Option<ImageBuffer<Luma<u8>,Vec<u8>>> = ImageBuffer::from_vec(source.get_width() as u32, source.get_height() as u32, source.get_matrix()) else {
|
||||||
return Err(Exceptions::ILLEGAL_ARGUMENT)
|
return Err(Exceptions::ILLEGAL_ARGUMENT)
|
||||||
@@ -34,7 +34,7 @@ impl OtsuLevelBinarizer {
|
|||||||
dynamic_filtered.try_into()
|
dynamic_filtered.try_into()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(source: Box<dyn LuminanceSource>) -> Self {
|
pub fn new(source: LS) -> Self {
|
||||||
Self {
|
Self {
|
||||||
width: source.get_width(),
|
width: source.get_width(),
|
||||||
height: source.get_height(),
|
height: source.get_height(),
|
||||||
@@ -45,12 +45,14 @@ impl OtsuLevelBinarizer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Binarizer for OtsuLevelBinarizer {
|
impl<LS: LuminanceSource> Binarizer for OtsuLevelBinarizer<LS> {
|
||||||
fn get_luminance_source(&self) -> &Box<dyn crate::LuminanceSource> {
|
type Source = LS;
|
||||||
|
|
||||||
|
fn get_luminance_source(&self) -> &LS {
|
||||||
&self.source
|
&self.source
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_black_row(&self, y: usize) -> Result<std::borrow::Cow<super::BitArray>> {
|
fn get_black_row(&self, y: usize) -> Result<Cow<BitArray>> {
|
||||||
let row = self.black_row_cache[y].get_or_try_init(|| {
|
let row = self.black_row_cache[y].get_or_try_init(|| {
|
||||||
let matrix = self.get_black_matrix()?;
|
let matrix = self.get_black_matrix()?;
|
||||||
Ok(matrix.getRow(y as u32))
|
Ok(matrix.getRow(y as u32))
|
||||||
@@ -59,18 +61,15 @@ impl Binarizer for OtsuLevelBinarizer {
|
|||||||
Ok(Cow::Borrowed(row))
|
Ok(Cow::Borrowed(row))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_black_matrix(&self) -> Result<&super::BitMatrix> {
|
fn get_black_matrix(&self) -> Result<&BitMatrix> {
|
||||||
let matrix = self
|
let matrix = self
|
||||||
.black_matrix
|
.black_matrix
|
||||||
.get_or_try_init(|| Self::generate_threshold_matrix(self.source.as_ref()))?;
|
.get_or_try_init(|| Self::generate_threshold_matrix(&self.source))?;
|
||||||
Ok(matrix)
|
Ok(matrix)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_binarizer(
|
fn create_binarizer(&self, source: LS) -> Self {
|
||||||
&self,
|
Self::new(source)
|
||||||
source: Box<dyn crate::LuminanceSource>,
|
|
||||||
) -> std::rc::Rc<dyn Binarizer> {
|
|
||||||
Rc::new(Self::new(source))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_width(&self) -> usize {
|
fn get_width(&self) -> usize {
|
||||||
|
|||||||
@@ -57,9 +57,7 @@ pub fn detect_in_svg_with_hints(
|
|||||||
.or_insert(DecodeHintValue::TryHarder(true));
|
.or_insert(DecodeHintValue::TryHarder(true));
|
||||||
|
|
||||||
multi_format_reader.decode_with_hints(
|
multi_format_reader.decode_with_hints(
|
||||||
&mut BinaryBitmap::new(Rc::new(HybridBinarizer::new(Box::new(
|
&mut BinaryBitmap::new(HybridBinarizer::new(SVGLuminanceSource::new(&svg_data)?)),
|
||||||
SVGLuminanceSource::new(&svg_data)?,
|
|
||||||
)))),
|
|
||||||
hints,
|
hints,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -100,9 +98,7 @@ pub fn detect_multiple_in_svg_with_hints(
|
|||||||
.or_insert(DecodeHintValue::TryHarder(true));
|
.or_insert(DecodeHintValue::TryHarder(true));
|
||||||
|
|
||||||
scanner.decode_multiple_with_hints(
|
scanner.decode_multiple_with_hints(
|
||||||
&mut BinaryBitmap::new(Rc::new(HybridBinarizer::new(Box::new(
|
&mut BinaryBitmap::new(HybridBinarizer::new(SVGLuminanceSource::new(&svg_data)?)),
|
||||||
SVGLuminanceSource::new(&svg_data)?,
|
|
||||||
)))),
|
|
||||||
hints,
|
hints,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -310,7 +310,7 @@ fn decodeByteSegment(
|
|||||||
{
|
{
|
||||||
encoding::all::ISO_8859_1
|
encoding::all::ISO_8859_1
|
||||||
} else {
|
} else {
|
||||||
StringUtils::guessCharset(&readBytes, hints)
|
StringUtils::guessCharset(&readBytes, hints).ok_or(Exceptions::ILLEGAL_STATE)?
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
CharacterSetECI::getCharset(
|
CharacterSetECI::getCharset(
|
||||||
|
|||||||
@@ -22,34 +22,28 @@ impl LuminanceSource for SVGLuminanceSource {
|
|||||||
self.0.get_height()
|
self.0.get_height()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn invert(&mut self) {
|
|
||||||
self.0.invert()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_crop_supported(&self) -> bool {
|
fn is_crop_supported(&self) -> bool {
|
||||||
self.0.is_crop_supported()
|
self.0.is_crop_supported()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn crop(
|
fn crop(&self, left: usize, top: usize, width: usize, height: usize) -> Result<Self> {
|
||||||
&self,
|
self.0.crop(left, top, width, height).map(|src| Self(src))
|
||||||
left: usize,
|
|
||||||
top: usize,
|
|
||||||
width: usize,
|
|
||||||
height: usize,
|
|
||||||
) -> Result<Box<dyn LuminanceSource>> {
|
|
||||||
self.0.crop(left, top, width, height)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_rotate_supported(&self) -> bool {
|
fn is_rotate_supported(&self) -> bool {
|
||||||
self.0.is_rotate_supported()
|
self.0.is_rotate_supported()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rotate_counter_clockwise(&self) -> Result<Box<dyn LuminanceSource>> {
|
fn invert(&mut self) {
|
||||||
self.0.rotate_counter_clockwise()
|
self.0.invert()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rotate_counter_clockwise_45(&self) -> Result<Box<dyn LuminanceSource>> {
|
fn rotate_counter_clockwise(&self) -> Result<Self> {
|
||||||
self.0.rotate_counter_clockwise_45()
|
self.0.rotate_counter_clockwise().map(|src| Self(src))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rotate_counter_clockwise_45(&self) -> Result<Self> {
|
||||||
|
self.0.rotate_counter_clockwise_45().map(|src| Self(src))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user