use std::collections::HashMap;
use crate::{
common::{BitMatrix, DecoderRXingResult, DetectorRXingResult, Result},
point_f, BarcodeFormat, Binarizer, DecodeHintType, DecodeHintValue, Exceptions, Point,
RXingResult, RXingResultMetadataType, RXingResultMetadataValue, Reader,
};
use super::{
decoder::Decoder,
detector::{zxing_cpp_detector, Detector},
};
use once_cell::sync::Lazy;
static DECODER: Lazy<Decoder> = Lazy::new(Decoder::new);
#[derive(Default)]
pub struct DataMatrixReader;
impl Reader for DataMatrixReader {
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 decoderRXingResult;
let mut points = Vec::new();
if hints.contains_key(&DecodeHintType::PURE_BARCODE) {
let bits = self.extractPureBits(image.get_black_matrix())?;
decoderRXingResult = DECODER.decode(&bits)?;
points.clear();
} else {
decoderRXingResult = if let Ok(fnd) = || -> Result<DecoderRXingResult> {
let detectorRXingResult =
zxing_cpp_detector::detect(image.get_black_matrix(), try_harder, true)?;
let decoded = DECODER.decode(detectorRXingResult.getBits())?;
points = detectorRXingResult.getPoints().to_vec();
Ok(decoded)
}() {
fnd
} else if try_harder {
if let Ok(fnd) = || -> Result<DecoderRXingResult> {
let detectorRXingResult = Detector::new(image.get_black_matrix())?.detect()?;
let decoded = DECODER.decode(detectorRXingResult.getBits())?;
points = detectorRXingResult.getPoints().to_vec();
Ok(decoded)
}() {
fnd
} else {
let bits = self.extractPureBits(image.get_black_matrix())?;
DECODER.decode(&bits)?
}
} else {
return Err(Exceptions::NOT_FOUND);
};
}
let mut result = RXingResult::new(
decoderRXingResult.getText(),
decoderRXingResult.getRawBytes().clone(),
points.clone(),
BarcodeFormat::DATA_MATRIX,
);
let byteSegments = decoderRXingResult.getByteSegments();
if !byteSegments.is_empty() {
result.putMetadata(
RXingResultMetadataType::BYTE_SEGMENTS,
RXingResultMetadataValue::ByteSegments(byteSegments.clone()),
);
}
let ecLevel = decoderRXingResult.getECLevel();
if !ecLevel.is_empty() {
result.putMetadata(
RXingResultMetadataType::ERROR_CORRECTION_LEVEL,
RXingResultMetadataValue::ErrorCorrectionLevel(ecLevel.to_string()),
);
}
let other_meta = decoderRXingResult.getOther();
if let Some(other) = other_meta {
if let Some(dcr) = other.downcast_ref::<String>() {
result.putMetadata(
RXingResultMetadataType::OTHER,
RXingResultMetadataValue::OTHER(dcr.to_owned()),
);
}
}
let contentType = decoderRXingResult.getContentType();
if !contentType.is_empty() {
result.putMetadata(
RXingResultMetadataType::CONTENT_TYPE,
RXingResultMetadataValue::ContentType(contentType.to_owned()),
);
}
let mirrored = decoderRXingResult.getIsMirrored();
if mirrored {
result.putMetadata(
RXingResultMetadataType::IS_MIRRORED,
RXingResultMetadataValue::IsMirrored(mirrored),
);
}
result.putMetadata(
RXingResultMetadataType::SYMBOLOGY_IDENTIFIER,
RXingResultMetadataValue::SymbologyIdentifier(format!(
"]d{}",
decoderRXingResult.getSymbologyModifier()
)),
);
Ok(result)
}
fn reset(&mut self) {
}
}
impl DataMatrixReader {
fn extractPureBits(&self, image: &BitMatrix) -> Result<BitMatrix> {
let Some(leftTopBlack) = image.getTopLeftOnBit() else {
return Err(Exceptions::NOT_FOUND);
};
let Some(rightBottomBlack) = image.getBottomRightOnBit() else {
return Err(Exceptions::NOT_FOUND);
};
let moduleSize = Self::moduleSize(leftTopBlack, image)?;
let mut top = leftTopBlack.y;
let bottom = rightBottomBlack.y;
let mut left = leftTopBlack.x;
let right = rightBottomBlack.x;
let matrixWidth = (right as i32 - left as i32 + 1) / moduleSize as i32;
let matrixHeight = (bottom as i32 - top as i32 + 1) / moduleSize as i32;
if matrixWidth <= 0 || matrixHeight <= 0 {
return Err(Exceptions::NOT_FOUND);
}
let matrixWidth = matrixWidth as u32;
let matrixHeight = matrixHeight as u32;
let nudge = moduleSize as f32 / 2.0;
top += nudge;
left += nudge;
let mut bits = BitMatrix::new(matrixWidth, matrixHeight)?;
for y in 0..matrixHeight {
let iOffset = top + y as f32 * moduleSize as f32;
for x in 0..matrixWidth {
if image.get_point(point_f(left + x as f32 * moduleSize as f32, iOffset)) {
bits.set(x, y);
}
}
}
Ok(bits)
}
fn moduleSize(leftTopBlack: Point, image: &BitMatrix) -> Result<u32> {
let width = image.getWidth();
let mut x = leftTopBlack.x as u32;
let y = leftTopBlack.y as u32;
while x < width && image.get(x, y) {
x += 1;
}
if x == width {
return Err(Exceptions::NOT_FOUND);
}
let moduleSize = x - leftTopBlack.x as u32;
if moduleSize == 0 {
return Err(Exceptions::NOT_FOUND);
}
Ok(moduleSize)
}
}