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