One-D barcodes reader port on c++
[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/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(){
34                         readers = new std::vector<OneDReader*>();
35                         readers->push_back(new EAN13Reader());
36                         // UPC-A is covered by EAN-13
37                         readers->push_back(new EAN8Reader());
38                         readers->push_back(new UPCEReader());
39                 }
40                 
41                 Ref<Result> MultiFormatUPCEANReader::decodeRow(int rowNumber, Ref<BitArray> row){                       
42                         // Compute this location once and reuse it on multiple implementations
43                         int size = readers->size();
44                         for (int i = 0; i < size; i++) {
45                                 OneDReader* reader = (*readers)[i];
46                                 Ref<Result> result;
47                                 try {
48                                         result = reader->decodeRow(rowNumber, row);//decodeRow(rowNumber, row, startGuardPattern);
49                                 } catch (ReaderException re) {
50                                         continue;
51                                 }
52                                 // Special case: a 12-digit code encoded in UPC-A is identical to a "0"
53                                 // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,
54                                 // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".
55                                 // Individually these are correct and their readers will both read such a code
56                                 // and correctly call it EAN-13, or UPC-A, respectively.
57                                 //
58                                 // In this case, if we've been looking for both types, we'd like to call it
59                                 // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read
60                                 // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A
61                                 // result if appropriate.
62                                 if (result->getBarcodeFormat() == BarcodeFormat_EAN_13) {
63                                         std::string& text = (result->getText())->getText();
64                                         if (text[0] == '0') {
65                                                 Ref<String> resultString(new String(text.substr(1)));
66                                                 Ref<Result> res(new Result(resultString, result->getRawBytes(), result->getResultPoints(), BarcodeFormat_UPC_A));
67                                                 return res;
68                                         }
69                                 }
70                                 return result;
71                         }
72                         throw ReaderException("No EAN code detected");
73                 }
74                 
75                 MultiFormatUPCEANReader::~MultiFormatUPCEANReader(){
76                         delete readers;
77                 }
78         }
79 }