OneDReader.cpp Example File
appdemos/qtws/QZXing/zxing/zxing/oned/OneDReader.cpp
#include <zxing/ZXing.h>
#include <zxing/oned/OneDReader.h>
#include <zxing/ReaderException.h>
#include <zxing/oned/OneDResultPoint.h>
#include <zxing/NotFoundException.h>
#include <math.h>
#include <limits.h>
#include <algorithm>
using std::vector;
using zxing::Ref;
using zxing::Result;
using zxing::NotFoundException;
using zxing::oned::OneDReader;
using zxing::BinaryBitmap;
using zxing::BitArray;
using zxing::DecodeHints;
Ref<Result> OneDReader::decode(Ref<BinaryBitmap> image, DecodeHints hints) {
try {
return doDecode(image, hints);
} catch (NotFoundException const& nfe) {
bool tryHarder = hints.getTryHarder();
if (tryHarder && image->isRotateSupported()) {
Ref<BinaryBitmap> rotatedImage(image->rotateCounterClockwise());
Ref<Result> result = doDecode(rotatedImage, hints);
ArrayRef< Ref<ResultPoint> >& points (result->getResultPoints());
if (points && !points->empty()) {
int height = rotatedImage->getHeight();
for (int i = 0; i < points->size(); i++) {
points[i].reset(new OneDResultPoint(height - points[i]->getY() - 1, points[i]->getX()));
}
}
return result;
} else {
throw nfe;
}
}
}
#include <typeinfo>
Ref<Result> OneDReader::doDecode(Ref<BinaryBitmap> image, DecodeHints hints) {
int width = image->getWidth();
int height = image->getHeight();
Ref<BitArray> row(new BitArray(width));
int middle = height >> 1;
bool tryHarder = hints.getTryHarder();
int rowStep = std::max(1, height >> (tryHarder ? 8 : 5));
using namespace std;
int maxLines;
if (tryHarder) {
maxLines = height;
} else {
maxLines = 15;
}
for (int x = 0; x < maxLines; x++) {
int rowStepsAboveOrBelow = (x + 1) >> 1;
bool isAbove = (x & 0x01) == 0;
int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow);
if (false) {
std::cerr << "rN "
<< rowNumber << " "
<< height << " "
<< middle << " "
<< rowStep << " "
<< isAbove << " "
<< rowStepsAboveOrBelow
<< std::endl;
}
if (rowNumber < 0 || rowNumber >= height) {
break;
}
try {
row = image->getBlackRow(rowNumber, row);
} catch (NotFoundException const& ignored) {
(void)ignored;
continue;
}
for (int attempt = 0; attempt < 2; attempt++) {
if (attempt == 1) {
row->reverse();
}
try {
Ref<Result> result = decodeRow(rowNumber, row, hints);
if (attempt == 1) {
ArrayRef< Ref<ResultPoint> > points(result->getResultPoints());
if (points) {
points[0] = Ref<ResultPoint>(new OneDResultPoint(width - points[0]->getX() - 1,
points[0]->getY()));
points[1] = Ref<ResultPoint>(new OneDResultPoint(width - points[1]->getX() - 1,
points[1]->getY()));
}
}
return result;
} catch (ReaderException const& re) {
(void)re;
continue;
}
}
}
throw NotFoundException();
}
int OneDReader::patternMatchVariance(vector<int>& counters,
vector<int> const& pattern,
int maxIndividualVariance) {
return patternMatchVariance(counters, &pattern[0], maxIndividualVariance);
}
int OneDReader::patternMatchVariance(vector<int>& counters,
int const pattern[],
int maxIndividualVariance) {
int numCounters = counters.size();
unsigned int total = 0;
unsigned int patternLength = 0;
for (int i = 0; i < numCounters; i++) {
total += counters[i];
patternLength += pattern[i];
}
if (total < patternLength) {
return INT_MAX;
}
int unitBarWidth = (total << INTEGER_MATH_SHIFT) / patternLength;
maxIndividualVariance = (maxIndividualVariance * unitBarWidth) >> INTEGER_MATH_SHIFT;
int totalVariance = 0;
for (int x = 0; x < numCounters; x++) {
int counter = counters[x] << INTEGER_MATH_SHIFT;
int scaledPattern = pattern[x] * unitBarWidth;
int variance = counter > scaledPattern ? counter - scaledPattern : scaledPattern - counter;
if (variance > maxIndividualVariance) {
return INT_MAX;
}
totalVariance += variance;
}
return totalVariance / total;
}
void OneDReader::recordPattern(Ref<BitArray> row,
int start,
vector<int>& counters) {
int numCounters = counters.size();
for (int i = 0; i < numCounters; i++) {
counters[i] = 0;
}
int end = row->getSize();
if (start >= end) {
throw NotFoundException();
}
bool isWhite = !row->get(start);
int counterPosition = 0;
int i = start;
while (i < end) {
if (row->get(i) ^ isWhite) {
counters[counterPosition]++;
} else {
counterPosition++;
if (counterPosition == numCounters) {
break;
} else {
counters[counterPosition] = 1;
isWhite = !isWhite;
}
}
i++;
}
if (!(counterPosition == numCounters || (counterPosition == numCounters - 1 && i == end))) {
throw NotFoundException();
}
}
OneDReader::~OneDReader() {}