1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright 2010 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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};

/**
 * This implementation can detect and decode Aztec codes in an image.
 *
 * @author David Olivier
 */
#[derive(Default)]
pub struct AztecReader;

impl Reader for AztecReader {
    /**
     * Locates and decodes a Data Matrix code in an image.
     *
     * @return a String representing the content encoded by the Data Matrix code
     * @throws NotFoundException if a Data Matrix code cannot be found
     * @throws FormatException if a Data Matrix code cannot be decoded
     */
    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 notFoundException = None;
        // let formatException = None;
        let mut detector = Detector::new(image.get_black_matrix());

        //  try {

        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)?;
        // } catch (NotFoundException e) {
        //   notFoundException = e;
        // } catch (FormatException e) {
        //   formatException = e;
        // }
        // if (decoderRXingResult == null) {
        // try {
        // let detectorRXingResult = detector.detect(true)?;
        // points = detectorRXingResult.getPoints();
        // decoderRXingResult = decoder::decode(&detectorRXingResult)?;
        // } catch (NotFoundException | FormatException e) {
        //   if (notFoundException != null) {
        //     throw notFoundException;
        //   }
        //   if (formatException != null) {
        //     throw formatException;
        //   }
        //   throw e;
        // }
        // }

        if let Some(DecodeHintValue::NeedResultPointCallback(cb)) =
            hints.get(&DecodeHintType::NEED_RESULT_POINT_CALLBACK)
        {
            // if let DecodeHintValue::NeedResultPointCallback(cb) = rpcb {
            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)
    }
}