Began removing the excessive use of exceptions in the 1D readers by drawing
[zxing.git] / cpp / core / src / zxing / oned / MultiFormatUPCEANReader.cpp
1 /*
2  *  MultiFormatUPCEANReader.cpp
3  *  ZXing
4  *
5  *  Copyright 2010 ZXing authors All rights reserved.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 #include "MultiFormatUPCEANReader.h"
20
21 #include <zxing/oned/EAN13Reader.h>
22 #include <zxing/oned/EAN8Reader.h>
23 #include <zxing/oned/UPCEReader.h>
24 #include <zxing/oned/UPCAReader.h>
25 #include <zxing/oned/OneDResultPoint.h>
26 #include <zxing/common/Array.h>
27 #include <zxing/ReaderException.h>
28 #include <math.h>
29
30 namespace zxing {
31         namespace oned {
32
33                 MultiFormatUPCEANReader::MultiFormatUPCEANReader(DecodeHints hints) : readers() {
34                   if (hints.containsFormat(BarcodeFormat_EAN_13)) {
35                     readers.push_back(Ref<OneDReader>(new EAN13Reader()));
36                   } else if (hints.containsFormat(BarcodeFormat_UPC_A)) {
37                     readers.push_back(Ref<OneDReader>(new UPCAReader()));
38                   }
39                   if (hints.containsFormat(BarcodeFormat_EAN_8)) {
40                     readers.push_back(Ref<OneDReader>(new EAN8Reader()));
41                   }
42                   if (hints.containsFormat(BarcodeFormat_UPC_E)) {
43                     readers.push_back(Ref<OneDReader>(new UPCEReader()));
44                   }
45                   if (readers.size() == 0) {
46               readers.push_back(Ref<OneDReader>(new EAN13Reader()));
47               // UPC-A is covered by EAN-13
48               readers.push_back(Ref<OneDReader>(new EAN8Reader()));
49               readers.push_back(Ref<OneDReader>(new UPCEReader()));
50                   }
51                 }
52
53                 Ref<Result> MultiFormatUPCEANReader::decodeRow(int rowNumber, Ref<BitArray> row) {
54                         // Compute this location once and reuse it on multiple implementations
55                         int size = readers.size();
56                         for (int i = 0; i < size; i++) {
57                                 Ref<OneDReader> reader = readers[i];
58                                 Ref<Result> result = reader->decodeRow(rowNumber, row);
59                                 if (result.empty()) {
60                                   continue;
61                                 }
62
63                                 // Special case: a 12-digit code encoded in UPC-A is identical to a "0"
64                                 // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,
65                                 // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".
66                                 // Individually these are correct and their readers will both read such a code
67                                 // and correctly call it EAN-13, or UPC-A, respectively.
68                                 //
69                                 // In this case, if we've been looking for both types, we'd like to call it
70                                 // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read
71                                 // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A
72                                 // result if appropriate.
73                                 if (result->getBarcodeFormat() == BarcodeFormat_EAN_13) {
74                                         const std::string& text = (result->getText())->getText();
75                                         if (text[0] == '0') {
76                                                 Ref<String> resultString(new String(text.substr(1)));
77                                                 Ref<Result> res(new Result(resultString, result->getRawBytes(),
78                                                     result->getResultPoints(), BarcodeFormat_UPC_A));
79                                                 return res;
80                                         }
81                                 }
82                                 return result;
83                         }
84                         return Ref<Result>();
85                 }
86         }
87 }