Fixed a bug I introduced while removing exceptions, which caused the

x coordinate of 1D barcodes to always be flipped, as if they had been
read upside down.


git-svn-id: https://zxing.googlecode.com/svn/trunk@1547 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
dswitkin@google.com 2010-08-18 18:31:49 +00:00
parent 439ff92bd9
commit f9bc448926

View file

@ -24,181 +24,183 @@
#include <limits.h> #include <limits.h>
namespace zxing { namespace zxing {
namespace oned { namespace oned {
using namespace std; using namespace std;
OneDReader::OneDReader() { OneDReader::OneDReader() {
} }
Ref<Result> OneDReader::decode(Ref<BinaryBitmap> image, DecodeHints hints) { Ref<Result> OneDReader::decode(Ref<BinaryBitmap> image, DecodeHints hints) {
Ref<Result> result = doDecode(image, hints); Ref<Result> result = doDecode(image, hints);
if (result.empty() && hints.getTryHarder() && image->isRotateSupported()) { if (result.empty() && hints.getTryHarder() && image->isRotateSupported()) {
Ref<BinaryBitmap> rotatedImage(image->rotateCounterClockwise()); Ref<BinaryBitmap> rotatedImage(image->rotateCounterClockwise());
result = doDecode(rotatedImage, hints); result = doDecode(rotatedImage, hints);
if (!result.empty()) { if (!result.empty()) {
/* /*
// Record that we found it rotated 90 degrees CCW / 270 degrees CW // Record that we found it rotated 90 degrees CCW / 270 degrees CW
Hashtable metadata = result.getResultMetadata(); Hashtable metadata = result.getResultMetadata();
int orientation = 270; int orientation = 270;
if (metadata != null && metadata.containsKey(ResultMetadataType.ORIENTATION)) { if (metadata != null && metadata.containsKey(ResultMetadataType.ORIENTATION)) {
// But if we found it reversed in doDecode(), add in that result here: // But if we found it reversed in doDecode(), add in that result here:
orientation = (orientation + orientation = (orientation +
((Integer) metadata.get(ResultMetadataType.ORIENTATION)).intValue()) % 360; ((Integer) metadata.get(ResultMetadataType.ORIENTATION)).intValue()) % 360;
} }
result.putMetadata(ResultMetadataType.ORIENTATION, new Integer(orientation)); result.putMetadata(ResultMetadataType.ORIENTATION, new Integer(orientation));
*/ */
// Update result points // Update result points
std::vector<Ref<ResultPoint> > points (result->getResultPoints()); std::vector<Ref<ResultPoint> > points (result->getResultPoints());
int height = rotatedImage->getHeight(); int height = rotatedImage->getHeight();
for (size_t i = 0; i < points.size(); i++) { for (size_t i = 0; i < points.size(); i++) {
points[i].reset(new OneDResultPoint(height - points[i]->getY() - 1, points[i]->getX())); points[i].reset(new OneDResultPoint(height - points[i]->getY() - 1, points[i]->getX()));
} }
} }
} }
if (result.empty()) { if (result.empty()) {
throw ReaderException(""); throw ReaderException("");
} }
return result; return result;
} }
Ref<Result> OneDReader::doDecode(Ref<BinaryBitmap> image, DecodeHints hints) { Ref<Result> OneDReader::doDecode(Ref<BinaryBitmap> image, DecodeHints hints) {
int width = image->getWidth(); int width = image->getWidth();
int height = image->getHeight(); int height = image->getHeight();
Ref<BitArray> row(new BitArray(width)); Ref<BitArray> row(new BitArray(width));
int middle = height >> 1; int middle = height >> 1;
bool tryHarder = hints.getTryHarder(); bool tryHarder = hints.getTryHarder();
int rowStep = (int)fmax(1, height >> (tryHarder ? 8 : 5)); int rowStep = (int)fmax(1, height >> (tryHarder ? 8 : 5));
int maxLines; int maxLines;
if (tryHarder) { if (tryHarder) {
maxLines = height; // Look at the whole image, not just the center maxLines = height; // Look at the whole image, not just the center
} else { } else {
maxLines = 15; // 15 rows spaced 1/32 apart is roughly the middle half of the image maxLines = 15; // 15 rows spaced 1/32 apart is roughly the middle half of the image
} }
for (int x = 0; x < maxLines; x++) { for (int x = 0; x < maxLines; x++) {
// Scanning from the middle out. Determine which row we're looking at next: // Scanning from the middle out. Determine which row we're looking at next:
int rowStepsAboveOrBelow = (x + 1) >> 1; int rowStepsAboveOrBelow = (x + 1) >> 1;
bool isAbove = (x & 0x01) == 0; // i.e. is x even? bool isAbove = (x & 0x01) == 0; // i.e. is x even?
int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow); int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow);
if (rowNumber < 0 || rowNumber >= height) { if (rowNumber < 0 || rowNumber >= height) {
// Oops, if we run off the top or bottom, stop // Oops, if we run off the top or bottom, stop
break; break;
} }
// Estimate black point for this row and load it: // Estimate black point for this row and load it:
try { try {
row = image->getBlackRow(rowNumber, row); row = image->getBlackRow(rowNumber, row);
} catch (ReaderException re) { } catch (ReaderException re) {
continue;
} catch (IllegalArgumentException re) {
continue; continue;
} } catch (IllegalArgumentException re) {
continue;
}
// While we have the image data in a BitArray, it's fairly cheap to reverse it in place to // While we have the image data in a BitArray, it's fairly cheap to reverse it in place to
// handle decoding upside down barcodes. // handle decoding upside down barcodes.
for (int attempt = 0; attempt < 2; attempt++) { for (int attempt = 0; attempt < 2; attempt++) {
if (attempt == 1) { // trying again? if (attempt == 1) {
row->reverse(); // reverse the row and continue row->reverse(); // reverse the row and continue
} }
// Look for a barcode // Look for a barcode
Ref<Result> result = decodeRow(rowNumber, row); Ref<Result> result = decodeRow(rowNumber, row);
// We found our barcode // We found our barcode
if (!result.empty()) { if (!result.empty()) {
// // But it was upside down, so note that if (attempt == 1) {
// result.putMetadata(ResultMetadataType.ORIENTATION, new Integer(180)); // But it was upside down, so note that
// // And remember to flip the result points horizontally. // result.putMetadata(ResultMetadataType.ORIENTATION, new Integer(180));
std::vector<Ref<ResultPoint> > points(result->getResultPoints()); // And remember to flip the result points horizontally.
// if there's exactly two points (which there should be), flip the x coordinate std::vector<Ref<ResultPoint> > points(result->getResultPoints());
// if there's not exactly 2, I don't know what do do with it // if there's exactly two points (which there should be), flip the x coordinate
if (points.size() == 2) { // if there's not exactly 2, I don't know what do do with it
Ref<ResultPoint> pointZero(new OneDResultPoint(width - points[0]->getX() - 1, if (points.size() == 2) {
points[0]->getY())); Ref<ResultPoint> pointZero(new OneDResultPoint(width - points[0]->getX() - 1,
points[0] = pointZero; points[0]->getY()));
points[0] = pointZero;
Ref<ResultPoint> pointOne(new OneDResultPoint(width - points[1]->getX() - 1, Ref<ResultPoint> pointOne(new OneDResultPoint(width - points[1]->getX() - 1,
points[1]->getY())); points[1]->getY()));
points[1] = pointOne; points[1] = pointOne;
result.reset(new Result(result->getText(), result->getRawBytes(), points, result.reset(new Result(result->getText(), result->getRawBytes(), points,
result->getBarcodeFormat())); result->getBarcodeFormat()));
}
} }
return result; return result;
} }
} }
} }
return Ref<Result>(); return Ref<Result>();
} }
unsigned int OneDReader::patternMatchVariance(int counters[], int countersSize, unsigned int OneDReader::patternMatchVariance(int counters[], int countersSize,
const int pattern[], int maxIndividualVariance) { const int pattern[], int maxIndividualVariance) {
int numCounters = countersSize; int numCounters = countersSize;
unsigned int total = 0; unsigned int total = 0;
unsigned 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];
} }
if (total < patternLength) { if (total < patternLength) {
// If we don't even have one pixel per unit of bar width, assume this is too small // If we don't even have one pixel per unit of bar width, assume this is too small
// to reliably match, so fail: // to reliably match, so fail:
return INT_MAX; return INT_MAX;
} }
// 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"
unsigned 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;
unsigned 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;
int variance = counter > scaledPattern ? counter - scaledPattern : scaledPattern - counter; int variance = counter > scaledPattern ? counter - scaledPattern : scaledPattern - counter;
if (variance > maxIndividualVariance) { if (variance > maxIndividualVariance) {
return INT_MAX; return INT_MAX;
} }
totalVariance += variance; totalVariance += variance;
} }
return totalVariance / total; return totalVariance / total;
} }
bool OneDReader::recordPattern(Ref<BitArray> row, int start, int counters[], int countersCount) { bool OneDReader::recordPattern(Ref<BitArray> row, int start, int counters[], int countersCount) {
int numCounters = countersCount;//sizeof(counters) / sizeof(int); int numCounters = countersCount;//sizeof(counters) / sizeof(int);
for (int i = 0; i < numCounters; i++) { for (int i = 0; i < numCounters; i++) {
counters[i] = 0; counters[i] = 0;
} }
int end = row->getSize(); int end = row->getSize();
if (start >= end) { if (start >= end) {
return false; return false;
} }
bool isWhite = !row->get(start); bool isWhite = !row->get(start);
int counterPosition = 0; int counterPosition = 0;
int i = start; int i = start;
while (i < end) { while (i < end) {
bool pixel = row->get(i); bool pixel = row->get(i);
if (pixel ^ isWhite) { // that is, exactly one is true if (pixel ^ isWhite) { // that is, exactly one is true
counters[counterPosition]++; counters[counterPosition]++;
} else { } else {
counterPosition++; counterPosition++;
if (counterPosition == numCounters) { if (counterPosition == numCounters) {
break; break;
} else { } else {
counters[counterPosition] = 1; counters[counterPosition] = 1;
isWhite ^= true; // isWhite = !isWhite; isWhite ^= true; // isWhite = !isWhite;
} }
} }
i++; i++;
} }
// If we read fully the last section of pixels and filled up our counters -- or filled // If we read fully the last section of pixels and filled up our counters -- or filled
// the last counter but ran off the side of the image, OK. Otherwise, a problem. // the last counter but ran off the side of the image, OK. Otherwise, a problem.
if (!(counterPosition == numCounters || (counterPosition == numCounters - 1 && i == end))) { if (!(counterPosition == numCounters || (counterPosition == numCounters - 1 && i == end))) {
return false; return false;
} }
return true; return true;
} }
OneDReader::~OneDReader() { OneDReader::~OneDReader() {
} }
} }
} }