2012-05-13 20:47:37 +00:00
|
|
|
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
|
2011-11-16 13:40:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2010 ZXing authors All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "EAN13Reader.h"
|
|
|
|
#include <zxing/ReaderException.h>
|
2012-05-14 14:01:57 +00:00
|
|
|
#include <qglobal.h>
|
2011-11-16 13:40:46 +00:00
|
|
|
|
|
|
|
namespace zxing {
|
2012-05-14 14:01:57 +00:00
|
|
|
namespace oned {
|
2011-11-16 13:40:46 +00:00
|
|
|
|
2012-05-14 14:01:57 +00:00
|
|
|
static const int FIRST_DIGIT_ENCODINGS[10] = {
|
|
|
|
0x00, 0x0B, 0x0D, 0xE, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A
|
|
|
|
};
|
2011-11-16 13:40:46 +00:00
|
|
|
|
2012-05-14 14:01:57 +00:00
|
|
|
EAN13Reader::EAN13Reader() { }
|
2011-11-16 13:40:46 +00:00
|
|
|
|
2012-05-14 14:01:57 +00:00
|
|
|
int EAN13Reader::decodeMiddle(Ref<BitArray> row, int startGuardBegin, int startGuardEnd,
|
|
|
|
std::string& resultString) {
|
|
|
|
(void)startGuardBegin;
|
|
|
|
const int countersLen = 4;
|
|
|
|
int counters[countersLen] = { 0, 0, 0, 0 };
|
2011-11-16 13:40:46 +00:00
|
|
|
|
2012-05-14 14:01:57 +00:00
|
|
|
int end = row->getSize();
|
|
|
|
int rowOffset = startGuardEnd;
|
|
|
|
int lgPatternFound = 0;
|
2011-11-16 13:40:46 +00:00
|
|
|
|
2012-05-14 14:01:57 +00:00
|
|
|
for (int x = 0; x < 6 && rowOffset < end; x++) {
|
2011-11-16 13:40:46 +00:00
|
|
|
int bestMatch = decodeDigit(row, counters, countersLen, rowOffset,
|
2012-05-14 14:01:57 +00:00
|
|
|
UPC_EAN_PATTERNS_L_AND_G_PATTERNS);
|
2011-11-16 13:40:46 +00:00
|
|
|
if (bestMatch < 0) {
|
2012-05-14 14:01:57 +00:00
|
|
|
return -1;
|
2011-11-16 13:40:46 +00:00
|
|
|
}
|
|
|
|
resultString.append(1, (char) ('0' + bestMatch % 10));
|
|
|
|
for (int i = 0; i < countersLen; i++) {
|
2012-05-14 14:01:57 +00:00
|
|
|
rowOffset += counters[i];
|
2011-11-16 13:40:46 +00:00
|
|
|
}
|
|
|
|
if (bestMatch >= 10) {
|
2012-05-14 14:01:57 +00:00
|
|
|
lgPatternFound |= 1 << (5 - x);
|
2011-11-16 13:40:46 +00:00
|
|
|
}
|
2012-05-14 14:01:57 +00:00
|
|
|
}
|
2011-11-16 13:40:46 +00:00
|
|
|
|
2012-05-14 14:01:57 +00:00
|
|
|
if (!determineFirstDigit(resultString, lgPatternFound)) {
|
2011-11-16 13:40:46 +00:00
|
|
|
return -1;
|
2012-05-14 14:01:57 +00:00
|
|
|
}
|
2011-11-16 13:40:46 +00:00
|
|
|
|
2012-05-14 14:01:57 +00:00
|
|
|
int middleRangeStart;
|
|
|
|
int middleRangeEnd;
|
|
|
|
if (findGuardPattern(row, rowOffset, true, (int*)getMIDDLE_PATTERN(),
|
|
|
|
getMIDDLE_PATTERN_LEN(), &middleRangeStart, &middleRangeEnd)) {
|
2011-11-16 13:40:46 +00:00
|
|
|
rowOffset = middleRangeEnd;
|
|
|
|
for (int x = 0; x < 6 && rowOffset < end; x++) {
|
2012-05-14 14:01:57 +00:00
|
|
|
int bestMatch = decodeDigit(row, counters, countersLen, rowOffset,
|
|
|
|
UPC_EAN_PATTERNS_L_PATTERNS);
|
|
|
|
if (bestMatch < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
resultString.append(1, (char) ('0' + bestMatch));
|
|
|
|
for (int i = 0; i < countersLen; i++) {
|
|
|
|
rowOffset += counters[i];
|
|
|
|
}
|
2011-11-16 13:40:46 +00:00
|
|
|
}
|
|
|
|
return rowOffset;
|
|
|
|
}
|
2012-05-14 14:01:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2011-11-16 13:40:46 +00:00
|
|
|
|
2012-05-14 14:01:57 +00:00
|
|
|
bool EAN13Reader::determineFirstDigit(std::string& resultString, int lgPatternFound) {
|
|
|
|
for (int d = 0; d < 10; d++) {
|
2011-11-16 13:40:46 +00:00
|
|
|
if (lgPatternFound == FIRST_DIGIT_ENCODINGS[d]) {
|
2012-05-14 14:01:57 +00:00
|
|
|
#if defined(Q_OS_SYMBIAN)
|
|
|
|
resultString.insert((char*)0, 1, (char) ('0' + d));
|
|
|
|
#else
|
|
|
|
resultString.insert(/*(char*)*/0, 1, (char) ('0' + d));
|
|
|
|
#endif
|
|
|
|
return true;
|
2011-11-16 13:40:46 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-14 14:01:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-11-16 13:40:46 +00:00
|
|
|
|
2012-05-14 14:01:57 +00:00
|
|
|
BarcodeFormat EAN13Reader::getBarcodeFormat(){
|
|
|
|
return BarcodeFormat_EAN_13;
|
|
|
|
}
|
|
|
|
}
|
2011-11-16 13:40:46 +00:00
|
|
|
}
|