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