use std::collections::HashMap;
use crate::{
common::{BitMatrix, DetectorRXingResult, Result},
BarcodeFormat, Binarizer, DecodeHintType, DecodeHintValue, Exceptions, RXingResult,
RXingResultMetadataType, Reader,
};
use super::{decoder::maxicode_decoder, detector};
#[derive(Default)]
pub struct MaxiCodeReader {
}
impl Reader for MaxiCodeReader {
fn decode<B: Binarizer>(
&mut self,
image: &mut crate::BinaryBitmap<B>,
) -> Result<crate::RXingResult> {
self.decode_with_hints(image, &HashMap::new())
}
fn decode_with_hints<B: Binarizer>(
&mut self,
image: &mut crate::BinaryBitmap<B>,
hints: &crate::DecodingHintDictionary,
) -> Result<crate::RXingResult> {
let try_harder = matches!(
hints.get(&DecodeHintType::TRY_HARDER),
Some(DecodeHintValue::TryHarder(true))
);
let mut rotation = None;
let decoderRXingResult = if try_harder {
let result = detector::detect(image.get_black_matrix_mut(), try_harder)?;
rotation = Some(result.rotation());
let parsed_result = detector::read_bits(result.getBits())?;
maxicode_decoder::decode_with_hints(&parsed_result, hints)?
} else {
let bits = Self::extractPureBits(image.get_black_matrix())?;
maxicode_decoder::decode_with_hints(&bits, hints)?
};
let mut result = RXingResult::new(
decoderRXingResult.getText(),
decoderRXingResult.getRawBytes().clone(),
Vec::new(),
BarcodeFormat::MAXICODE,
);
let ecLevel = decoderRXingResult.getECLevel();
if !ecLevel.is_empty() {
result.putMetadata(
RXingResultMetadataType::ERROR_CORRECTION_LEVEL,
crate::RXingResultMetadataValue::ErrorCorrectionLevel(ecLevel.to_owned()),
);
}
if let Some(rot) = rotation {
if rot > 0.0 {
result.putMetadata(
RXingResultMetadataType::ORIENTATION,
crate::RXingResultMetadataValue::Orientation(rot as i32),
)
}
}
Ok(result)
}
fn reset(&mut self) {
}
}
impl MaxiCodeReader {
pub const MATRIX_WIDTH: u32 = 30;
pub const MATRIX_HEIGHT: u32 = 33;
fn extractPureBits(image: &BitMatrix) -> Result<BitMatrix> {
let enclosingRectangleOption = image.getEnclosingRectangle();
if enclosingRectangleOption.is_none() {
return Err(Exceptions::NOT_FOUND);
}
let enclosingRectangle = enclosingRectangleOption.ok_or(Exceptions::NOT_FOUND)?;
let left = enclosingRectangle[0];
let top = enclosingRectangle[1];
let width = enclosingRectangle[2];
let height = enclosingRectangle[3];
let mut bits = BitMatrix::new(Self::MATRIX_WIDTH, Self::MATRIX_HEIGHT)?;
for y in 0..Self::MATRIX_HEIGHT {
let iy = (top + (y * height + height / 2) / Self::MATRIX_HEIGHT).min(height - 1);
for x in 0..Self::MATRIX_WIDTH {
let ix = left
+ ((x * width + width / 2 + (y & 0x01) * width / 2) / Self::MATRIX_WIDTH)
.min(width - 1);
if image.get(ix, iy) {
bits.set(x, y);
}
}
}
Ok(bits)
}
}