use std::collections::HashMap;
use crate::{
common::{DecoderRXingResult, DetectorRXingResult, Result},
exceptions::Exceptions,
BarcodeFormat, Binarizer, BinaryBitmap, DecodeHintType, DecodeHintValue, RXingResult,
RXingResultMetadataType, RXingResultMetadataValue, Reader,
};
use super::{decoder, detector::Detector};
#[derive(Default)]
pub struct AztecReader;
impl Reader for AztecReader {
fn decode<B: Binarizer>(&mut self, image: &mut BinaryBitmap<B>) -> Result<RXingResult> {
self.decode_with_hints(image, &HashMap::new())
}
fn decode_with_hints<B: Binarizer>(
&mut self,
image: &mut BinaryBitmap<B>,
hints: &HashMap<DecodeHintType, DecodeHintValue>,
) -> Result<RXingResult> {
let mut detector = Detector::new(image.get_black_matrix());
let detectorRXingResult = if let Ok(det) = detector.detect(false) {
det
} else if let Ok(det) = detector.detect(true) {
det
} else {
return Err(Exceptions::NOT_FOUND);
};
let points = detectorRXingResult.getPoints();
let decoderRXingResult: DecoderRXingResult = decoder::decode(&detectorRXingResult)?;
if let Some(DecodeHintValue::NeedResultPointCallback(cb)) =
hints.get(&DecodeHintType::NEED_RESULT_POINT_CALLBACK)
{
for point in points {
cb(*point);
}
}
let mut result = RXingResult::new_complex(
decoderRXingResult.getText(),
decoderRXingResult.getRawBytes().clone(),
decoderRXingResult.getNumBits(),
points.to_vec(),
BarcodeFormat::AZTEC,
chrono::Utc::now().timestamp_millis() as u128,
);
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_owned()),
);
}
result.putMetadata(
RXingResultMetadataType::SYMBOLOGY_IDENTIFIER,
RXingResultMetadataValue::SymbologyIdentifier(format!(
"]z{}",
decoderRXingResult.getSymbologyModifier()
)),
);
Ok(result)
}
}