75bc4ad24dcc63c15802bc91a842031d30fe886d
[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         resultString.append(1, (char) ('0' + bestMatch));
40         for (int i = 0; i < countersLen; i++) {
41           rowOffset += counters[i];
42         }
43       }
44
45       int* middleRange = 0;
46       try {
47         middleRange = findGuardPattern(row, rowOffset, true, (int*)getMIDDLE_PATTERN(),
48             getMIDDLE_PATTERN_LEN());
49         rowOffset = middleRange[1];
50
51         for (int x = 0; x < 4 && rowOffset < end; x++) {
52           int bestMatch = decodeDigit(row, counters, countersLen, rowOffset,
53               UPC_EAN_PATTERNS_L_PATTERNS);
54           resultString.append(1, (char) ('0' + bestMatch));
55           for (int i = 0; i < countersLen; i++) {
56             rowOffset += counters[i];
57           }
58         }
59
60         delete [] middleRange;
61         return rowOffset;
62       } catch (ReaderException const& re) {
63         delete [] middleRange;
64         throw re;
65       }
66     }
67
68     BarcodeFormat EAN8Reader::getBarcodeFormat(){
69       return BarcodeFormat_EAN_8;
70     }
71   }
72 }