5 * Created by Lukasz Warchol on 10-01-22.
6 * Copyright 2010 ZXing authors All rights reserved.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "EAN13Reader.h"
22 #include <zxing/ReaderException.h>
27 static const int FIRST_DIGIT_ENCODINGS[10] = {0x00, 0x0B, 0x0D, 0xE, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A};
30 EAN13Reader::EAN13Reader(){
31 decodeMiddleCounters = new int[4];
32 for (int i=0; i<4; i++) {
33 decodeMiddleCounters[i] = 0;
37 int EAN13Reader::decodeMiddle(Ref<BitArray> row, int startRange[], int startRangeLen, std::string& resultString){
39 int* counters = decodeMiddleCounters;
46 int end = row->getSize();
47 int rowOffset = startRange[1];
49 int lgPatternFound = 0;
51 for (int x = 0; x < 6 && rowOffset < end; x++) {
52 int bestMatch = decodeDigit(row, counters, countersLen, rowOffset, UPC_EAN_PATTERNS_L_AND_G_PATTERNS);
53 resultString.append(1, (char) ('0' + bestMatch % 10));
54 for (int i = 0; i < countersLen; i++) {
55 rowOffset += counters[i];
57 if (bestMatch >= 10) {
58 lgPatternFound |= 1 << (5 - x);
62 determineFirstDigit(resultString, lgPatternFound);
64 int* middleRange = findGuardPattern(row, rowOffset, true, (int*)getMIDDLE_PATTERN(), getMIDDLE_PATTERN_LEN());
65 rowOffset = middleRange[1];
67 for (int x = 0; x < 6 && rowOffset < end; x++) {
68 int bestMatch = decodeDigit(row, counters, countersLen, rowOffset, UPC_EAN_PATTERNS_L_PATTERNS);
69 resultString.append(1, (char) ('0' + bestMatch));
70 for (int i = 0; i < countersLen; i++) {
71 rowOffset += counters[i];
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;
91 EAN13Reader::~EAN13Reader(){
92 delete [] decodeMiddleCounters;