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

View file

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

View file

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