a29ca921b302f5713e333d4473cded1b9180dbcc
[zxing.git] / cpp / core / src / zxing / oned / EAN13Reader.cpp
1 /*
2  *  EAN13Reader.cpp
3  *  ZXing
4  *
5  *  Created by Lukasz Warchol on 10-01-22.
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
21 #include "EAN13Reader.h"
22 #include <zxing/ReaderException.h>
23
24 namespace zxing {
25   namespace oned {
26     
27     static const int FIRST_DIGIT_ENCODINGS[10] = {0x00, 0x0B, 0x0D, 0xE, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A};
28         
29     
30     EAN13Reader::EAN13Reader() { }
31     
32     int EAN13Reader::decodeMiddle(Ref<BitArray> row, int startRange[], int startRangeLen, std::string& resultString){
33       const int countersLen = 4;
34       int counters[countersLen] = { 0, 0, 0, 0 };
35       
36       
37       int end = row->getSize();
38       int rowOffset = startRange[1];
39       
40       int lgPatternFound = 0;
41       
42       for (int x = 0; x < 6 && rowOffset < end; x++) {
43         int bestMatch = decodeDigit(row, counters, countersLen, rowOffset, UPC_EAN_PATTERNS_L_AND_G_PATTERNS);
44         resultString.append(1, (char) ('0' + bestMatch % 10));
45         for (int i = 0; i < countersLen; i++) {
46           rowOffset += counters[i];
47         }
48         if (bestMatch >= 10) {
49           lgPatternFound |= 1 << (5 - x);
50         }
51       }
52       
53       determineFirstDigit(resultString, lgPatternFound);
54       
55       int* middleRange = 0;
56                         try {
57                                 middleRange = findGuardPattern(row, rowOffset, true, (int*)getMIDDLE_PATTERN(), getMIDDLE_PATTERN_LEN());
58                                 rowOffset = middleRange[1];
59       
60                                 for (int x = 0; x < 6 && rowOffset < end; x++) {
61                 int bestMatch = decodeDigit(row, counters, countersLen, rowOffset, UPC_EAN_PATTERNS_L_PATTERNS);
62                                         resultString.append(1, (char) ('0' + bestMatch));
63                                         for (int i = 0; i < countersLen; i++) {
64                                             rowOffset += counters[i];
65                                         }
66                                 }
67       
68                                 delete [] middleRange;
69                                 return rowOffset;
70                         } catch (ReaderException const& re) {
71                             delete [] middleRange;
72                             throw re;
73                         }
74     }
75     
76     void EAN13Reader::determineFirstDigit(std::string& resultString, int lgPatternFound){
77       for (int d = 0; d < 10; d++) {
78         if (lgPatternFound == FIRST_DIGIT_ENCODINGS[d]) {
79           resultString.insert(0, 1, (char) ('0' + d));
80           return;
81         }
82       }
83       throw ReaderException("determineFirstDigit");
84     }
85     
86     BarcodeFormat EAN13Reader::getBarcodeFormat(){
87       return BarcodeFormat_EAN_13;
88     }
89   }
90 }