use crate::{
common::{cpp_essentials::ConcentricPattern, DetectorRXingResult},
multi::MultipleBarcodeReader,
BarcodeFormat, DecodeHintType, DecodeHintValue, DecodingHintDictionary, Exceptions,
RXingResult, Reader,
};
use super::{
decoder::Decode,
detector::{
DetectPureMQR, DetectPureQR, FindFinderPatterns, GenerateFinderPatternSets, SampleMQR,
SampleQR,
},
};
#[derive(Default)]
pub struct QrReader;
impl Reader for QrReader {
fn decode<B: crate::Binarizer>(
&mut self,
image: &mut crate::BinaryBitmap<B>,
) -> crate::common::Result<crate::RXingResult> {
self.decode_with_hints(image, &DecodingHintDictionary::new())
}
fn decode_with_hints<B: crate::Binarizer>(
&mut self,
image: &mut crate::BinaryBitmap<B>,
hints: &crate::DecodingHintDictionary,
) -> crate::common::Result<RXingResult> {
if !matches!(
hints.get(&DecodeHintType::PURE_BARCODE),
Some(DecodeHintValue::PureBarcode(true))
)
{
return Ok(self
.decode_set_number_with_hints(image, hints, 1)?
.first()
.ok_or(Exceptions::NOT_FOUND)?
.clone());
}
let binImg = image.get_black_matrix();
let mut detectorResult = Err(Exceptions::NOT_FOUND);
if let Some(DecodeHintValue::PossibleFormats(formats)) =
hints.get(&DecodeHintType::POSSIBLE_FORMATS)
{
if formats.contains(&BarcodeFormat::QR_CODE) {
detectorResult = DetectPureQR(binImg);
}
if formats.contains(&BarcodeFormat::MICRO_QR_CODE) && detectorResult.is_err() {
detectorResult = DetectPureMQR(binImg);
}
}
if detectorResult.is_err() {
for decode_function in [DetectPureQR, DetectPureMQR] {
detectorResult = decode_function(binImg);
if detectorResult.is_ok() {
break;
}
}
}
let detectorResult = detectorResult?;
let decoderResult = Decode(detectorResult.getBits())?;
let position = detectorResult.getPoints();
Ok(RXingResult::with_decoder_result(
decoderResult,
position,
if detectorResult.getBits().width() < 21 {
BarcodeFormat::MICRO_QR_CODE
} else {
BarcodeFormat::QR_CODE
},
))
}
}
impl MultipleBarcodeReader for QrReader {
fn decode_multiple<B: crate::Binarizer>(
&mut self,
image: &mut crate::BinaryBitmap<B>,
) -> crate::common::Result<Vec<crate::RXingResult>> {
self.decode_multiple_with_hints(image, &DecodingHintDictionary::new())
}
fn decode_multiple_with_hints<B: crate::Binarizer>(
&mut self,
image: &mut crate::BinaryBitmap<B>,
hints: &DecodingHintDictionary,
) -> crate::common::Result<Vec<crate::RXingResult>> {
self.decode_set_number_with_hints(image, hints, u32::MAX)
}
}
impl QrReader {
fn decode_set_number_with_hints<B: crate::Binarizer>(
&mut self,
image: &mut crate::BinaryBitmap<B>,
hints: &DecodingHintDictionary,
count: u32,
) -> crate::common::Result<Vec<RXingResult>> {
let binImg = image.get_black_matrix(); let maxSymbols = count;
let try_harder = matches!(
hints.get(&DecodeHintType::TRY_HARDER),
Some(DecodeHintValue::TryHarder(true))
);
let mut allFPs = FindFinderPatterns(binImg, try_harder);
let mut usedFPs: Vec<ConcentricPattern> = Vec::new();
let mut results: Vec<RXingResult> = Vec::new();
let (check_qr, check_mqr) = if let Some(DecodeHintValue::PossibleFormats(formats)) =
hints.get(&DecodeHintType::POSSIBLE_FORMATS)
{
(
formats.contains(&BarcodeFormat::QR_CODE),
formats.contains(&BarcodeFormat::MICRO_QR_CODE),
)
} else {
(true, true)
};
if check_qr {
let allFPSets = GenerateFinderPatternSets(&mut allFPs);
for fpSet in allFPSets {
if usedFPs.contains(&fpSet.bl)
|| usedFPs.contains(&fpSet.tl)
|| usedFPs.contains(&fpSet.tr)
{
continue;
}
let detectorResult = SampleQR(binImg, &fpSet);
if let Ok(detectorResult) = detectorResult {
let decoderResult = Decode(detectorResult.getBits());
let position = detectorResult.getPoints();
if let Ok(decoderResult) = decoderResult {
if decoderResult.isValid() {
usedFPs.push(fpSet.bl);
usedFPs.push(fpSet.tl);
usedFPs.push(fpSet.tr);
}
if decoderResult.isValid() {
results.push(RXingResult::with_decoder_result(
decoderResult,
position,
BarcodeFormat::QR_CODE,
));
if maxSymbols != 0 && (results.len() as u32) == maxSymbols {
break;
}
}
}
}
}
}
if check_mqr && !(maxSymbols != 0 && (results.len() as u32) == maxSymbols) {
for fp in allFPs {
if usedFPs.contains(&fp) {
continue;
}
let detectorResult = SampleMQR(binImg, fp);
if let Ok(detectorResult) = detectorResult {
let decoderResult = Decode(detectorResult.getBits());
let position = detectorResult.getPoints();
if let Ok(decoderResult) = decoderResult {
if decoderResult.isValid() {
results.push(RXingResult::with_decoder_result(
decoderResult,
position,
BarcodeFormat::MICRO_QR_CODE,
));
if maxSymbols != 0 && (results.len() as u32) == maxSymbols {
break;
}
}
}
}
}
}
Ok(results)
}
}