c++ changes for r2517

git-svn-id: https://zxing.googlecode.com/svn/trunk@2611 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
smparkes@smparkes.net 2013-04-01 06:01:12 +00:00
parent e6b876ae71
commit 0ccb52ab37
3 changed files with 60 additions and 38 deletions

View file

@ -89,41 +89,42 @@ Code39Reader::Code39Reader(bool usingCheckDigit_, bool extendedMode_) {
}
Ref<Result> Code39Reader::decodeRow(int rowNumber, Ref<BitArray> row) {
std::vector<int>& theCounters (counters);
{ // Arrays.fill(counters, 0);
int size = counters.size();
counters.resize(0);
counters.resize(size); }
decodeRowResult.clear();
int size = theCounters.size();
theCounters.resize(0);
theCounters.resize(size); }
std::string& result (decodeRowResult);
result.clear();
vector<int> start (findAsteriskPattern(row, counters));
// Read off white space
vector<int> start (findAsteriskPattern(row, theCounters));
// Read off white space
int nextStart = row->getNextSet(start[1]);
int end = row->getSize();
char decodedChar;
int lastStart;
do {
recordPattern(row, nextStart, counters);
int pattern = toNarrowWidePattern(counters);
recordPattern(row, nextStart, theCounters);
int pattern = toNarrowWidePattern(theCounters);
if (pattern < 0) {
throw NotFoundException();;
}
decodedChar = patternToChar(pattern);
decodeRowResult.append(1, decodedChar);
result.append(1, decodedChar);
lastStart = nextStart;
for (int i = 0, end=counters.size(); i < end; i++) {
nextStart += counters[i];
for (int i = 0, end=theCounters.size(); i < end; i++) {
nextStart += theCounters[i];
}
// Read off white space
nextStart = row->getNextSet(nextStart);
} while (decodedChar != '*');
decodeRowResult.resize(decodeRowResult.length()-1);// remove asterisk
result.resize(decodeRowResult.length()-1);// remove asterisk
// Look for whitespace after pattern:
int lastPatternSize = 0;
for (int i = 0, e = counters.size(); i < e; i++) {
lastPatternSize += counters[i];
for (int i = 0, e = theCounters.size(); i < e; i++) {
lastPatternSize += theCounters[i];
}
int whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize;
// If 50% of last pattern size, following last pattern, is not whitespace,
@ -133,27 +134,27 @@ Ref<Result> Code39Reader::decodeRow(int rowNumber, Ref<BitArray> row) {
}
if (usingCheckDigit) {
int max = decodeRowResult.length() - 1;
int max = result.length() - 1;
int total = 0;
for (int i = 0; i < max; i++) {
total += alphabet_string.find_first_of(decodeRowResult[i], 0);
}
if (decodeRowResult[max] != ALPHABET[total % 43]) {
if (result[max] != ALPHABET[total % 43]) {
throw ChecksumException();
}
decodeRowResult.resize(max);
result.resize(max);
}
if (decodeRowResult.length() == 0) {
if (result.length() == 0) {
// Almost false positive
throw NotFoundException();
}
Ref<String> resultString;
if (extendedMode) {
resultString = decodeExtended(decodeRowResult);
resultString = decodeExtended(result);
} else {
resultString = Ref<String>(new String(decodeRowResult));
resultString = Ref<String>(new String(result));
}
float left = (float) (start[1] + start[0]) / 2.0f;

View file

@ -56,27 +56,38 @@ namespace {
const int ASTERISK_ENCODING = CHARACTER_ENCODINGS[47];
}
Code93Reader::Code93Reader() {
decodeRowResult.reserve(20);
counters.resize(6);
}
Ref<Result> Code93Reader::decodeRow(int rowNumber, Ref<BitArray> row) {
Range start (findAsteriskPattern(row));
// Read off white space
int nextStart = row->getNextSet(start[1]);
int end = row->getSize();
string result;
vector<int> counters (6, 0);
vector<int>& theCounters (counters);
{ // Arrays.fill(counters, 0);
int size = theCounters.size();
theCounters.resize(0);
theCounters.resize(size); }
string& result (decodeRowResult);
result.clear();
char decodedChar;
int lastStart;
do {
recordPattern(row, nextStart, counters);
int pattern = toPattern(counters);
recordPattern(row, nextStart, theCounters);
int pattern = toPattern(theCounters);
if (pattern < 0) {
throw NotFoundException();
}
decodedChar = patternToChar(pattern);
result.append(1, decodedChar);
lastStart = nextStart;
for(int i=0, e=counters.size(); i < e; ++i) {
nextStart += counters[i];
for(int i=0, e=theCounters.size(); i < e; ++i) {
nextStart += theCounters[i];
}
// Read off white space
nextStart = row->getNextSet(nextStart);
@ -119,31 +130,36 @@ Code93Reader::Range Code93Reader::findAsteriskPattern(Ref<BitArray> row) {
int width = row->getSize();
int rowOffset = row->getNextSet(0);
int counterPosition = 0;
vector<int> counters (6, 0);
{ // Arrays.fill(counters, 0);
int size = counters.size();
counters.resize(0);
counters.resize(size); }
vector<int>& theCounters (counters);
int patternStart = rowOffset;
bool isWhite = false;
int patternLength = counters.size();
int patternLength = theCounters.size();
int counterPosition = 0;
for (int i = rowOffset; i < width; i++) {
if (row->get(i) ^ isWhite) {
counters[counterPosition]++;
theCounters[counterPosition]++;
} else {
if (counterPosition == patternLength - 1) {
if (toPattern(counters) == ASTERISK_ENCODING) {
if (toPattern(theCounters) == ASTERISK_ENCODING) {
return Range(patternStart, i);
}
patternStart += counters[0] + counters[1];
patternStart += theCounters[0] + theCounters[1];
for (int y = 2; y < patternLength; y++) {
counters[y - 2] = counters[y];
theCounters[y - 2] = theCounters[y];
}
counters[patternLength - 2] = 0;
counters[patternLength - 1] = 0;
theCounters[patternLength - 2] = 0;
theCounters[patternLength - 1] = 0;
counterPosition--;
} else {
counterPosition++;
}
counters[counterPosition] = 1;
theCounters[counterPosition] = 1;
isWhite = !isWhite;
}
}

View file

@ -37,10 +37,15 @@ namespace zxing {
*/
class zxing::oned::Code93Reader : public OneDReader {
public:
Code93Reader();
Ref<Result> decodeRow(int rowNumber, Ref<BitArray> row);
private:
static Range findAsteriskPattern(Ref<BitArray> row);
std::string decodeRowResult;
std::vector<int> counters;
Range findAsteriskPattern(Ref<BitArray> row);
static int toPattern(std::vector<int>& counters);
static char patternToChar(int pattern);
static Ref<String> decodeExtended(std::string const& encoded);