Skip to main content

rxing/oned/
upc_a_reader.rs

1/*
2 * Copyright 2008 ZXing authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use crate::{BarcodeFormat, Binarizer, Exceptions, RXingResult, Reader, common::Result};
18
19use super::{EAN13Reader, OneDReader, UPCEANReader};
20
21/**
22 * <p>Implements decoding of the UPC-A format.</p>
23 *
24 * @author dswitkin@google.com (Daniel Switkin)
25 * @author Sean Owen
26 */
27#[derive(Default)]
28pub struct UPCAReader(EAN13Reader);
29
30impl Reader for UPCAReader {
31    fn decode<B: Binarizer>(&mut self, image: &mut crate::BinaryBitmap<B>) -> Result<RXingResult> {
32        Self::maybeReturnRXingResult(self.0.decode(image)?)
33    }
34
35    fn decode_with_hints<B: Binarizer>(
36        &mut self,
37        image: &mut crate::BinaryBitmap<B>,
38        hints: &crate::DecodeHints,
39    ) -> Result<RXingResult> {
40        Self::maybeReturnRXingResult(self.0.decode_with_hints(image, hints)?)
41    }
42}
43
44impl OneDReader for UPCAReader {
45    fn decode_row(
46        &mut self,
47        rowNumber: u32,
48        row: &crate::common::BitArray,
49        hints: &crate::DecodeHints,
50    ) -> Result<RXingResult> {
51        Self::maybeReturnRXingResult(self.0.decode_row(rowNumber, row, hints)?)
52    }
53}
54
55impl UPCEANReader for UPCAReader {
56    fn decodeRowWithGuardRange(
57        &self,
58        rowNumber: u32,
59        row: &crate::common::BitArray,
60        startGuardRange: &[usize; 2],
61        hints: &crate::DecodeHints,
62    ) -> Result<RXingResult>
63    where
64        Self: Sized,
65    {
66        Self::maybeReturnRXingResult(self.0.decodeRowWithGuardRange(
67            rowNumber,
68            row,
69            startGuardRange,
70            hints,
71        )?)
72    }
73
74    fn getBarcodeFormat(&self) -> BarcodeFormat {
75        BarcodeFormat::UPC_A
76    }
77
78    fn decodeMiddle(
79        &self,
80        row: &crate::common::BitArray,
81        startRange: &[usize; 2],
82        resultString: &mut String,
83    ) -> Result<usize> {
84        self.0.decodeMiddle(row, startRange, resultString)
85    }
86}
87
88impl UPCAReader {
89    // private final UPCEANReader ean13Reader = new EAN13Reader();
90
91    fn maybeReturnRXingResult(result: RXingResult) -> Result<RXingResult> {
92        let text = result.getText();
93        if let Some(stripped_text) = text.strip_prefix('0') {
94            let mut upcaRXingResult = RXingResult::new(
95                stripped_text,
96                Vec::new(),
97                result.getPoints().to_vec(),
98                BarcodeFormat::UPC_A,
99            );
100            upcaRXingResult.putAllMetadata(result.getRXingResultMetadata().clone());
101
102            Ok(upcaRXingResult)
103        } else {
104            Err(Exceptions::NOT_FOUND)
105        }
106    }
107}