5 * Copyright 2010 ZXing authors All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "EAN13Reader.h"
21 #include <zxing/ReaderException.h>
26 static const int FIRST_DIGIT_ENCODINGS[10] = {
27 0x00, 0x0B, 0x0D, 0xE, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A
30 EAN13Reader::EAN13Reader() { }
32 int EAN13Reader::decodeMiddle(Ref<BitArray> row, int startRange[], int startRangeLen,
33 std::string& resultString){
34 const int countersLen = 4;
35 int counters[countersLen] = { 0, 0, 0, 0 };
37 int end = row->getSize();
38 int rowOffset = startRange[1];
39 int lgPatternFound = 0;
41 for (int x = 0; x < 6 && rowOffset < end; x++) {
42 int bestMatch = decodeDigit(row, counters, countersLen, rowOffset,
43 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];
48 if (bestMatch >= 10) {
49 lgPatternFound |= 1 << (5 - x);
53 determineFirstDigit(resultString, lgPatternFound);
57 middleRange = findGuardPattern(row, rowOffset, true, (int*)getMIDDLE_PATTERN(),
58 getMIDDLE_PATTERN_LEN());
59 rowOffset = middleRange[1];
61 for (int x = 0; x < 6 && rowOffset < end; x++) {
62 int bestMatch = decodeDigit(row, counters, countersLen, rowOffset,
63 UPC_EAN_PATTERNS_L_PATTERNS);
64 resultString.append(1, (char) ('0' + bestMatch));
65 for (int i = 0; i < countersLen; i++) {
66 rowOffset += counters[i];
70 delete [] middleRange;
72 } catch (ReaderException const& re) {
73 delete [] middleRange;
78 void EAN13Reader::determineFirstDigit(std::string& resultString, int lgPatternFound) {
79 for (int d = 0; d < 10; d++) {
80 if (lgPatternFound == FIRST_DIGIT_ENCODINGS[d]) {
81 resultString.insert(0, 1, (char) ('0' + d));
85 throw ReaderException("determineFirstDigit");
88 BarcodeFormat EAN13Reader::getBarcodeFormat(){
89 return BarcodeFormat_EAN_13;