fb94f150054ac5a6a9dab32a5b9db1f0cea92188
[zxing.git] / cpp / core / src / zxing / oned / EAN8Reader.cpp
1 /*
2  *  EAN8Reader.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
20 #include "EAN8Reader.h"
21 #include <zxing/ReaderException.h>
22
23 namespace zxing {
24   namespace oned {
25
26     EAN8Reader::EAN8Reader(){ }
27
28     int EAN8Reader::decodeMiddle(Ref<BitArray> row, int startRange[], int startRangeLen,
29         std::string& resultString){
30       const int countersLen = 4;
31       int counters[countersLen] = { 0, 0, 0, 0 };
32
33       int end = row->getSize();
34       int rowOffset = startRange[1];
35
36       for (int x = 0; x < 4 && rowOffset < end; x++) {
37         int bestMatch = decodeDigit(row, counters, countersLen, rowOffset,
38             UPC_EAN_PATTERNS_L_PATTERNS);
39         if (bestMatch < 0) {
40           return -1;
41         }
42         resultString.append(1, (char) ('0' + bestMatch));
43         for (int i = 0; i < countersLen; i++) {
44           rowOffset += counters[i];
45         }
46       }
47
48       int* middleRange = findGuardPattern(row, rowOffset, true, (int*)getMIDDLE_PATTERN(),
49             getMIDDLE_PATTERN_LEN());
50       if (middleRange != NULL) {
51         rowOffset = middleRange[1];
52         for (int x = 0; x < 4 && rowOffset < end; x++) {
53           int bestMatch = decodeDigit(row, counters, countersLen, rowOffset,
54               UPC_EAN_PATTERNS_L_PATTERNS);
55           if (bestMatch < 0) {
56             delete [] middleRange;
57             return -1;
58           }
59           resultString.append(1, (char) ('0' + bestMatch));
60           for (int i = 0; i < countersLen; i++) {
61             rowOffset += counters[i];
62           }
63         }
64
65         delete [] middleRange;
66         return rowOffset;
67       }
68       return -1;
69     }
70
71     BarcodeFormat EAN8Reader::getBarcodeFormat(){
72       return BarcodeFormat_EAN_8;
73     }
74   }
75 }