Fixing some issues in C++ port regarding 1D barcode readers

git-svn-id: https://zxing.googlecode.com/svn/trunk@1454 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
luizcroc 2010-06-23 03:05:34 +00:00
parent 6943578c18
commit dc9fcff5cb
10 changed files with 33 additions and 29 deletions

View file

@ -28,7 +28,7 @@ LuminanceSource::LuminanceSource() {
LuminanceSource::~LuminanceSource() { LuminanceSource::~LuminanceSource() {
} }
unsigned char* LuminanceSource::copyMatrix() const { unsigned char* LuminanceSource::copyMatrix() {
int width = getWidth(); int width = getWidth();
int height = getHeight(); int height = getHeight();
unsigned char* matrix = new unsigned char[width*height]; unsigned char* matrix = new unsigned char[width*height];

View file

@ -30,11 +30,11 @@ public:
LuminanceSource(); LuminanceSource();
virtual ~LuminanceSource(); virtual ~LuminanceSource();
virtual int getWidth() const = 0; virtual int getWidth() = 0;
virtual int getHeight() const = 0; virtual int getHeight() = 0;
virtual unsigned char getPixel(int x, int y) const = 0; virtual unsigned char getPixel(int x, int y) = 0;
virtual unsigned char* copyMatrix() const; virtual unsigned char* copyMatrix();
}; };
} }

View file

@ -24,6 +24,7 @@
#include <zxing/ReaderException.h> #include <zxing/ReaderException.h>
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <sstream>
namespace zxing { namespace zxing {
namespace oned { namespace oned {
@ -166,10 +167,10 @@ namespace zxing {
counters[counterPosition]++; counters[counterPosition]++;
} else { } else {
if (counterPosition == patternLength - 1) { if (counterPosition == patternLength - 1) {
int bestVariance = MAX_AVG_VARIANCE; unsigned int bestVariance = MAX_AVG_VARIANCE;
int bestMatch = -1; int bestMatch = -1;
for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) { for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) {
int variance = patternMatchVariance(counters, sizeof(counters)/sizeof(int), CODE_PATTERNS[startCode], MAX_INDIVIDUAL_VARIANCE); unsigned int variance = patternMatchVariance(counters, sizeof(counters)/sizeof(int), CODE_PATTERNS[startCode], MAX_INDIVIDUAL_VARIANCE);
if (variance < bestVariance) { if (variance < bestVariance) {
bestVariance = variance; bestVariance = variance;
bestMatch = startCode; bestMatch = startCode;
@ -204,7 +205,7 @@ namespace zxing {
int Code128Reader::decodeCode(Ref<BitArray> row, int counters[], int countersCount, int rowOffset){ int Code128Reader::decodeCode(Ref<BitArray> row, int counters[], int countersCount, int rowOffset){
recordPattern(row, rowOffset, counters, countersCount); recordPattern(row, rowOffset, counters, countersCount);
int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept unsigned int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
int bestMatch = -1; int bestMatch = -1;
for (int d = 0; d < CODE_PATTERNS_LENGTH; d++) { for (int d = 0; d < CODE_PATTERNS_LENGTH; d++) {
int pattern[countersLength]; int pattern[countersLength];
@ -213,7 +214,7 @@ namespace zxing {
pattern[ind] = CODE_PATTERNS[d][ind]; pattern[ind] = CODE_PATTERNS[d][ind];
} }
// memcpy(pattern, CODE_PATTERNS[d], countersLength); // memcpy(pattern, CODE_PATTERNS[d], countersLength);
int variance = patternMatchVariance(counters, countersCount, pattern, MAX_INDIVIDUAL_VARIANCE); unsigned int variance = patternMatchVariance(counters, countersCount, pattern, MAX_INDIVIDUAL_VARIANCE);
if (variance < bestVariance) { if (variance < bestVariance) {
bestVariance = variance; bestVariance = variance;
bestMatch = d; bestMatch = d;
@ -251,7 +252,7 @@ namespace zxing {
bool isNextShifted = false; bool isNextShifted = false;
std::string tmpResultString; std::string tmpResultString;
std::stringstream tmpResultSStr; // used if its Code 128C
int lastStart = startPatternInfo[0]; int lastStart = startPatternInfo[0];
int nextStart = startPatternInfo[1]; int nextStart = startPatternInfo[1];
@ -373,11 +374,11 @@ namespace zxing {
} }
break; break;
case CODE_CODE_C: case CODE_CODE_C:
// the code read in this case is the number encoded directly
if (code < 100) { if (code < 100) {
if (code < 10) { if (code < 10)
tmpResultString.append(1, '0'); tmpResultSStr << '0';
} tmpResultSStr << code;
tmpResultString.append(1, code);
} else { } else {
if (code != CODE_STOP) { if (code != CODE_STOP) {
lastCharacterWasPrintable = false; lastCharacterWasPrintable = false;
@ -437,6 +438,9 @@ namespace zxing {
throw ReaderException(""); throw ReaderException("");
} }
if (codeSet == CODE_CODE_C)
tmpResultString.append(tmpResultSStr.str());
// Need to pull out the check digits from string // Need to pull out the check digits from string
int resultLength = tmpResultString.length(); int resultLength = tmpResultString.length();
// Only bother if the result had at least one character, and if the checksum digit happened to // Only bother if the result had at least one character, and if the checksum digit happened to

View file

@ -27,7 +27,7 @@ namespace zxing {
class Code128Reader : public OneDReader { class Code128Reader : public OneDReader {
private: private:
static const int MAX_AVG_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.25f); static const unsigned int MAX_AVG_VARIANCE = (unsigned int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.25f);
static const int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.7f); static const int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.7f);
static const int CODE_SHIFT = 98; static const int CODE_SHIFT = 98;

View file

@ -237,7 +237,7 @@ namespace zxing {
* @throws ReaderException if the quiet zone cannot be found, a ReaderException is thrown. * @throws ReaderException if the quiet zone cannot be found, a ReaderException is thrown.
*/ */
void ITFReader::validateQuietZone(Ref<BitArray> row, int startPattern){ void ITFReader::validateQuietZone(Ref<BitArray> row, int startPattern){
#pragma mark needs some corrections //#pragma mark needs some corrections
// int quietCount = narrowLineWidth * 10; // expect to find this many pixels of quiet zone // int quietCount = narrowLineWidth * 10; // expect to find this many pixels of quiet zone
// //
// for (int i = startPattern - 1; quietCount > 0 && i >= 0; i--) { // for (int i = startPattern - 1; quietCount > 0 && i >= 0; i--) {
@ -335,7 +335,7 @@ namespace zxing {
* @throws ReaderException if digit cannot be decoded * @throws ReaderException if digit cannot be decoded
*/ */
int ITFReader::decodeDigit(int counters[], int countersLen){ int ITFReader::decodeDigit(int counters[], int countersLen){
int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept unsigned int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
int bestMatch = -1; int bestMatch = -1;
int max = PATTERNS_LEN; int max = PATTERNS_LEN;
for (int i = 0; i < max; i++) { for (int i = 0; i < max; i++) {
@ -343,7 +343,7 @@ namespace zxing {
for(int ind = 0; ind<countersLen; ind++){ for(int ind = 0; ind<countersLen; ind++){
pattern[ind] = PATTERNS[i][ind]; pattern[ind] = PATTERNS[i][ind];
} }
int variance = patternMatchVariance(counters, countersLen, pattern, MAX_INDIVIDUAL_VARIANCE); unsigned int variance = patternMatchVariance(counters, countersLen, pattern, MAX_INDIVIDUAL_VARIANCE);
if (variance < bestVariance) { if (variance < bestVariance) {
bestVariance = variance; bestVariance = variance;
bestMatch = i; bestMatch = i;

View file

@ -27,7 +27,7 @@ namespace zxing {
class ITFReader : public OneDReader { class ITFReader : public OneDReader {
private: private:
static const int MAX_AVG_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.42f); static const unsigned int MAX_AVG_VARIANCE = (unsigned int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.42f);
static const int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.8f); static const int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.8f);
// Stores the actual narrow line width of the image being decoded. // Stores the actual narrow line width of the image being decoded.

View file

@ -122,10 +122,10 @@ namespace zxing {
throw ReaderException(""); throw ReaderException("");
} }
int OneDReader::patternMatchVariance(int counters[], int countersSize, const int pattern[], int maxIndividualVariance) { unsigned int OneDReader::patternMatchVariance(int counters[], int countersSize, const int pattern[], int maxIndividualVariance) {
int numCounters = countersSize; int numCounters = countersSize;
int total = 0; unsigned int total = 0;
int patternLength = 0; unsigned int patternLength = 0;
for (int i = 0; i < numCounters; i++) { for (int i = 0; i < numCounters; i++) {
total += counters[i]; total += counters[i];
patternLength += pattern[i]; patternLength += pattern[i];
@ -138,10 +138,10 @@ namespace zxing {
// We're going to fake floating-point math in integers. We just need to use more bits. // We're going to fake floating-point math in integers. We just need to use more bits.
// Scale up patternLength so that intermediate values below like scaledCounter will have // Scale up patternLength so that intermediate values below like scaledCounter will have
// more "significant digits" // more "significant digits"
int unitBarWidth = (total << INTEGER_MATH_SHIFT) / patternLength; unsigned int unitBarWidth = (total << INTEGER_MATH_SHIFT) / patternLength;
maxIndividualVariance = (maxIndividualVariance * unitBarWidth) >> INTEGER_MATH_SHIFT; maxIndividualVariance = (maxIndividualVariance * unitBarWidth) >> INTEGER_MATH_SHIFT;
int totalVariance = 0; unsigned int totalVariance = 0;
for (int x = 0; x < numCounters; x++) { for (int x = 0; x < numCounters; x++) {
int counter = counters[x] << INTEGER_MATH_SHIFT; int counter = counters[x] << INTEGER_MATH_SHIFT;
int scaledPattern = pattern[x] * unitBarWidth; int scaledPattern = pattern[x] * unitBarWidth;

View file

@ -38,7 +38,7 @@ namespace zxing {
virtual Ref<Result> decode(Ref<BinaryBitmap> image); virtual Ref<Result> decode(Ref<BinaryBitmap> image);
virtual Ref<Result> decodeRow(int rowNumber, Ref<BitArray> row) = 0; virtual Ref<Result> decodeRow(int rowNumber, Ref<BitArray> row) = 0;
static int patternMatchVariance(int counters[], int countersSize, const int pattern[], int maxIndividualVariance); static unsigned int patternMatchVariance(int counters[], int countersSize, const int pattern[], int maxIndividualVariance);
static void recordPattern(Ref<BitArray> row, int start, int counters[], int countersCount); static void recordPattern(Ref<BitArray> row, int start, int counters[], int countersCount);
virtual ~OneDReader(); virtual ~OneDReader();
}; };

View file

@ -239,7 +239,7 @@ namespace zxing {
// int UPCEANReader::decodeDigit(Ref<BitArray> row, int counters[], int countersLen, int rowOffset, int** patterns/*[][]*/, int paterns1Len, int paterns2Len) // int UPCEANReader::decodeDigit(Ref<BitArray> row, int counters[], int countersLen, int rowOffset, int** patterns/*[][]*/, int paterns1Len, int paterns2Len)
int UPCEANReader::decodeDigit(Ref<BitArray> row, int counters[], int countersLen, int rowOffset, UPC_EAN_PATTERNS patternType){ int UPCEANReader::decodeDigit(Ref<BitArray> row, int counters[], int countersLen, int rowOffset, UPC_EAN_PATTERNS patternType){
recordPattern(row, rowOffset, counters, countersLen); recordPattern(row, rowOffset, counters, countersLen);
int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept unsigned int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
int bestMatch = -1; int bestMatch = -1;
int max = 0; int max = 0;
@ -252,7 +252,7 @@ namespace zxing {
pattern[j] = L_PATTERNS[i][j]; pattern[j] = L_PATTERNS[i][j];
} }
int variance = patternMatchVariance(counters, countersLen, pattern, MAX_INDIVIDUAL_VARIANCE); unsigned int variance = patternMatchVariance(counters, countersLen, pattern, MAX_INDIVIDUAL_VARIANCE);
if (variance < bestVariance) { if (variance < bestVariance) {
bestVariance = variance; bestVariance = variance;
bestMatch = i; bestMatch = i;
@ -267,7 +267,7 @@ namespace zxing {
pattern[j] = L_AND_G_PATTERNS[i][j]; pattern[j] = L_AND_G_PATTERNS[i][j];
} }
int variance = patternMatchVariance(counters, countersLen, pattern, MAX_INDIVIDUAL_VARIANCE); unsigned int variance = patternMatchVariance(counters, countersLen, pattern, MAX_INDIVIDUAL_VARIANCE);
if (variance < bestVariance) { if (variance < bestVariance) {
bestVariance = variance; bestVariance = variance;
bestMatch = i; bestMatch = i;

View file

@ -32,7 +32,7 @@ namespace zxing {
class UPCEANReader : public OneDReader { class UPCEANReader : public OneDReader {
private: private:
static const int MAX_AVG_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.42f); static const unsigned int MAX_AVG_VARIANCE = (unsigned int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.42f);
static const int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.7f); static const int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.7f);
static int* findStartGuardPattern(Ref<BitArray> row); //throws ReaderException static int* findStartGuardPattern(Ref<BitArray> row); //throws ReaderException