C++ port: add decode hints system

git-svn-id: https://zxing.googlecode.com/svn/trunk@1498 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
flyashi 2010-07-29 19:39:44 +00:00
parent f7392b3ffe
commit 7bba1c4680
14 changed files with 214 additions and 42 deletions

View file

@ -0,0 +1,80 @@
/*
* DecodeHintType.cpp
* zxing
*
* Copyright 2010 ZXing authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <zxing/DecodeHints.h>
#include <zxing/common/IllegalArgumentException.h>
namespace zxing {
DecodeHints::DecodeHints() {
hints = 0;
}
DecodeHints::DecodeHints(DecodeHintType init) {
hints = init;
}
DecodeHints::~DecodeHints() {
// if DecodeHintType requires a destructor in the future, call it here
}
void DecodeHints::addFormat(BarcodeFormat toadd) {
switch (toadd) {
case BarcodeFormat_QR_CODE: hints |= BARCODEFORMAT_QR_CODE_HINT; break;
case BarcodeFormat_DATA_MATRIX: hints |= BARCODEFORMAT_DATA_MATRIX_HINT; break;
case BarcodeFormat_UPC_E: hints |= BARCODEFORMAT_UPC_E_HINT; break;
case BarcodeFormat_UPC_A: hints |= BARCODEFORMAT_UPC_A_HINT; break;
case BarcodeFormat_EAN_8: hints |= BARCODEFORMAT_EAN_8_HINT; break;
case BarcodeFormat_EAN_13: hints |= BARCODEFORMAT_EAN_13_HINT; break;
case BarcodeFormat_CODE_128: hints |= BARCODEFORMAT_CODE_128_HINT; break;
case BarcodeFormat_CODE_39: hints |= BARCODEFORMAT_CODE_39_HINT; break;
case BarcodeFormat_ITF: hints |= BARCODEFORMAT_ITF_HINT; break;
default: throw IllegalArgumentException("Unrecognizd barcode format");
}
}
bool DecodeHints::containsFormat(BarcodeFormat tocheck) const {
DecodeHintType checkAgainst;
switch (tocheck) {
case BarcodeFormat_QR_CODE: checkAgainst = BARCODEFORMAT_QR_CODE_HINT; break;
case BarcodeFormat_DATA_MATRIX: checkAgainst = BARCODEFORMAT_DATA_MATRIX_HINT; break;
case BarcodeFormat_UPC_E: checkAgainst = BARCODEFORMAT_UPC_E_HINT; break;
case BarcodeFormat_UPC_A: checkAgainst = BARCODEFORMAT_UPC_A_HINT; break;
case BarcodeFormat_EAN_8: checkAgainst = BARCODEFORMAT_EAN_8_HINT; break;
case BarcodeFormat_EAN_13: checkAgainst = BARCODEFORMAT_EAN_13_HINT; break;
case BarcodeFormat_CODE_128: checkAgainst = BARCODEFORMAT_CODE_128_HINT; break;
case BarcodeFormat_CODE_39: checkAgainst = BARCODEFORMAT_CODE_39_HINT; break;
case BarcodeFormat_ITF: checkAgainst = BARCODEFORMAT_ITF_HINT; break;
default: throw IllegalArgumentException("Unrecognizd barcode format");
}
return (hints & checkAgainst);
}
void DecodeHints::setTryHarder(bool toset) {
if (toset) {
hints |= TRYHARDER_HINT;
} else {
hints &= ~TRYHARDER_HINT;
}
}
bool DecodeHints::getTryHarder() const {
return (hints & TRYHARDER_HINT);
}
} /* namespace */

View file

@ -0,0 +1,79 @@
/*
* DecodeHintType.h
* zxing
*
* Copyright 2010 ZXing authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DECODEHINTS_H_
#define DECODEHINTS_H_
#include <zxing/BarcodeFormat.h>
namespace zxing {
typedef unsigned int DecodeHintType;
class DecodeHints {
private:
static const DecodeHintType BARCODEFORMAT_QR_CODE_HINT = 1 << BarcodeFormat_QR_CODE;
static const DecodeHintType BARCODEFORMAT_DATA_MATRIX_HINT = 1 << BarcodeFormat_DATA_MATRIX;
static const DecodeHintType BARCODEFORMAT_UPC_E_HINT = 1 << BarcodeFormat_UPC_E;
static const DecodeHintType BARCODEFORMAT_UPC_A_HINT = 1 << BarcodeFormat_UPC_A;
static const DecodeHintType BARCODEFORMAT_EAN_8_HINT = 1 << BarcodeFormat_EAN_8;
static const DecodeHintType BARCODEFORMAT_EAN_13_HINT = 1 << BarcodeFormat_EAN_13;
static const DecodeHintType BARCODEFORMAT_CODE_128_HINT = 1 << BarcodeFormat_CODE_128;
static const DecodeHintType BARCODEFORMAT_CODE_39_HINT = 1 << BarcodeFormat_CODE_39;
static const DecodeHintType BARCODEFORMAT_ITF_HINT = 1 << BarcodeFormat_ITF;
static const DecodeHintType TRYHARDER_HINT = 1 << 31;
DecodeHintType hints;
public:
static const DecodeHintType BARCODEFORMAT_PRODUCT_HINT =
BARCODEFORMAT_UPC_E_HINT |
BARCODEFORMAT_UPC_A_HINT |
BARCODEFORMAT_EAN_8_HINT |
BARCODEFORMAT_EAN_13_HINT;
static const DecodeHintType BARCODEFORMAT_ONED_HINT =
BARCODEFORMAT_PRODUCT_HINT |
BARCODEFORMAT_CODE_128_HINT |
BARCODEFORMAT_CODE_39_HINT |
BARCODEFORMAT_ITF_HINT;
static const DecodeHintType BARCODEFORMAT_ANY_HINT =
BARCODEFORMAT_ONED_HINT |
// TODO: uncomment once this passes QA
// BARCODEFORMAT_DATA_MATRIX_HINT |
BARCODEFORMAT_QR_CODE_HINT;
static const DecodeHintType DEFAULT_HINTS = BARCODEFORMAT_ANY_HINT;
DecodeHints();
DecodeHints(DecodeHintType init);
~DecodeHints();
void addFormat(BarcodeFormat toadd);
bool containsFormat(BarcodeFormat tocheck) const;
void setTryHarder(bool toset);
bool getTryHarder() const;
};
}
#endif

View file

@ -34,10 +34,10 @@ namespace zxing {
readers.push_back(new zxing::oned::MultiFormatOneDReader()); readers.push_back(new zxing::oned::MultiFormatOneDReader());
} }
Ref<Result> MultiFormatReader::decode(Ref<BinaryBitmap> image){ Ref<Result> MultiFormatReader::decode(Ref<BinaryBitmap> image, DecodeHints hints){
for (unsigned int i = 0; i < readers.size(); i++) { for (unsigned int i = 0; i < readers.size(); i++) {
try { try {
return readers[i]->decode(image); return readers[i]->decode(image, hints);
} catch (ReaderException re) { } catch (ReaderException re) {
// continue // continue
} }
@ -50,4 +50,4 @@ namespace zxing {
delete readers[i]; delete readers[i];
} }
} }
} }

View file

@ -23,6 +23,7 @@
#include <zxing/Reader.h> #include <zxing/Reader.h>
#include <zxing/common/BitArray.h> #include <zxing/common/BitArray.h>
#include <zxing/Result.h> #include <zxing/Result.h>
#include <zxing/DecodeHints.h>
namespace zxing { namespace zxing {
class MultiFormatReader : public Reader { class MultiFormatReader : public Reader {
@ -32,7 +33,7 @@ namespace zxing {
public: public:
MultiFormatReader(); MultiFormatReader();
Ref<Result> decode(Ref<BinaryBitmap> image); Ref<Result> decode(Ref<BinaryBitmap> image, DecodeHints hints);
~MultiFormatReader(); ~MultiFormatReader();
}; };

View file

@ -24,4 +24,9 @@ namespace zxing {
Reader::~Reader() { } Reader::~Reader() { }
Ref<Result> Reader::decode(Ref<BinaryBitmap> image) {
DecodeHints hints(hints.DEFAULT_HINTS);
return decode(image, hints);
}
} }

View file

@ -23,6 +23,7 @@
#include <zxing/BinaryBitmap.h> #include <zxing/BinaryBitmap.h>
#include <zxing/Result.h> #include <zxing/Result.h>
#include <zxing/DecodeHints.h>
namespace zxing { namespace zxing {
@ -30,7 +31,8 @@ namespace zxing {
protected: protected:
Reader() {} Reader() {}
public: public:
virtual Ref<Result> decode(Ref<BinaryBitmap> image) = 0; Ref<Result> decode(Ref<BinaryBitmap> image);
virtual Ref<Result> decode(Ref<BinaryBitmap> image, DecodeHints hints) = 0;
virtual ~Reader(); virtual ~Reader();
}; };

View file

@ -31,7 +31,7 @@ DataMatrixReader::DataMatrixReader() :
decoder_() { decoder_() {
} }
Ref<Result> DataMatrixReader::decode(Ref<BinaryBitmap> image) { Ref<Result> DataMatrixReader::decode(Ref<BinaryBitmap> image, DecodeHints hints) {
#ifdef DEBUG #ifdef DEBUG
cout << "decoding image " << image.object_ << ":\n" << flush; cout << "decoding image " << image.object_ << ":\n" << flush;
#endif #endif

View file

@ -22,6 +22,7 @@
*/ */
#include <zxing/Reader.h> #include <zxing/Reader.h>
#include <zxing/DecodeHints.h>
#include <zxing/datamatrix/decoder/Decoder.h> #include <zxing/datamatrix/decoder/Decoder.h>
namespace zxing { namespace zxing {
@ -33,7 +34,7 @@ private:
public: public:
DataMatrixReader(); DataMatrixReader();
virtual Ref<Result> decode(Ref<BinaryBitmap> image); virtual Ref<Result> decode(Ref<BinaryBitmap> image, DecodeHints hints);
virtual ~DataMatrixReader(); virtual ~DataMatrixReader();
}; };

View file

@ -31,14 +31,16 @@ namespace zxing {
OneDReader::OneDReader() { OneDReader::OneDReader() {
} }
Ref<Result> OneDReader::decode(Ref<BinaryBitmap> image) { Ref<Result> OneDReader::decode(Ref<BinaryBitmap> image, DecodeHints hints) {
try {
return doDecode(image); try {
return doDecode(image, hints);
}catch (ReaderException re) { }catch (ReaderException re) {
if (false /*tryHarder && image.isRotateSupported()*/) { if (hints.getTryHarder() && image->isRotateSupported()) {
Ref<BinaryBitmap> rotatedImage(image->rotateCounterClockwise());
Ref<Result> result(doDecode(rotatedImage, hints));
/* /*
BinaryBitmap rotatedImage = image.rotateCounterClockwise();
Result result = doDecode(rotatedImage, hints);
// 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;
@ -48,34 +50,32 @@ namespace zxing {
((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 */
ResultPoint[] points = result.getResultPoints(); // Update result points
int height = rotatedImage.getHeight(); std::vector<Ref<ResultPoint> > points (result->getResultPoints());
for (int i = 0; i < points.length; i++) { int height = rotatedImage->getHeight();
points[i] = new ResultPoint(height - points[i].getY() - 1, points[i].getX()); for (size_t i = 0; i < points.size(); i++) {
points[i].reset(new OneDResultPoint(height - points[i]->getY() - 1, points[i]->getX()));
} }
return result; return result;
*/
} else { } else {
throw re; throw re;
} }
} }
} }
Ref<Result> OneDReader::doDecode(Ref<BinaryBitmap> image){ 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));
// BitArray row = new BitArray(width);
int middle = height >> 1; int middle = height >> 1;
bool tryHarder = true;//hints != null && hints.containsKey(DecodeHintType.TRY_HARDER); bool tryHarder = hints.getTryHarder();
int rowStep = (int)fmax(1, height >> (tryHarder ? 7 : 4)); 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 = 9; // Nine rows spaced 1/16 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++) {
@ -95,7 +95,7 @@ namespace zxing {
}catch (ReaderException re) { }catch (ReaderException re) {
continue; continue;
}catch (IllegalArgumentException re) { }catch (IllegalArgumentException re) {
continue; 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
@ -112,17 +112,18 @@ namespace zxing {
// // But it was upside down, so note that // // But it was upside down, so note that
// result.putMetadata(ResultMetadataType.ORIENTATION, new Integer(180)); // result.putMetadata(ResultMetadataType.ORIENTATION, new Integer(180));
// // And remember to flip the result points horizontally. // // And remember to flip the result points horizontally.
std::vector<Ref<ResultPoint> > points(result->getResultPoints()); std::vector<Ref<ResultPoint> > points(result->getResultPoints());
if (points.size() == 2) { // if there's exactly two points (which there should be), flip the x coordinate
Ref<ResultPoint> pointZero(new OneDResultPoint(width - points[0]->getX() - 1, points[0]->getY())); // if there's not exactly 2, I don't know what do do with it
points[0] = pointZero; if (points.size() == 2) {
Ref<ResultPoint> pointZero(new OneDResultPoint(width - points[0]->getX() - 1, points[0]->getY()));
points[0] = pointZero;
Ref<ResultPoint> pointOne(new OneDResultPoint(width - points[1]->getX() - 1, points[1]->getY())); Ref<ResultPoint> pointOne(new OneDResultPoint(width - points[1]->getX() - 1, points[1]->getY()));
points[1] = pointOne; points[1] = pointOne;
result.reset(new Result(result->getText(),result->getRawBytes(),points,result->getBarcodeFormat())); result.reset(new Result(result->getText(),result->getRawBytes(),points,result->getBarcodeFormat()));
} }
} }
return result; return result;
} catch (ReaderException re) { } catch (ReaderException re) {

View file

@ -23,6 +23,7 @@
#include <zxing/Reader.h> #include <zxing/Reader.h>
#include <zxing/common/BitArray.h> #include <zxing/common/BitArray.h>
#include <zxing/BinaryBitmap.h> #include <zxing/BinaryBitmap.h>
#include <zxing/DecodeHints.h>
namespace zxing { namespace zxing {
namespace oned { namespace oned {
@ -30,12 +31,12 @@ namespace zxing {
private: private:
static const int INTEGER_MATH_SHIFT = 8; static const int INTEGER_MATH_SHIFT = 8;
Ref<Result> doDecode(Ref<BinaryBitmap> image); Ref<Result> doDecode(Ref<BinaryBitmap> image, DecodeHints hints);
public: public:
static const int PATTERN_MATCH_RESULT_SCALE_FACTOR = 1 << INTEGER_MATH_SHIFT; static const int PATTERN_MATCH_RESULT_SCALE_FACTOR = 1 << INTEGER_MATH_SHIFT;
OneDReader(); OneDReader();
virtual Ref<Result> decode(Ref<BinaryBitmap> image); virtual Ref<Result> decode(Ref<BinaryBitmap> image, DecodeHints hints);
virtual Ref<Result> decodeRow(int rowNumber, Ref<BitArray> row) = 0; virtual Ref<Result> decodeRow(int rowNumber, Ref<BitArray> row) = 0;
static unsigned int patternMatchVariance(int counters[], int countersSize, const int pattern[], int maxIndividualVariance); static unsigned int patternMatchVariance(int counters[], int countersSize, const int pattern[], int maxIndividualVariance);

View file

@ -32,8 +32,8 @@ namespace zxing {
Ref<Result> UPCAReader::decodeRow(int rowNumber, Ref<BitArray> row, int startGuardRange[]){ Ref<Result> UPCAReader::decodeRow(int rowNumber, Ref<BitArray> row, int startGuardRange[]){
return maybeReturnResult(ean13Reader.decodeRow(rowNumber, row, startGuardRange)); return maybeReturnResult(ean13Reader.decodeRow(rowNumber, row, startGuardRange));
} }
Ref<Result> UPCAReader::decode(Ref<BinaryBitmap> image){ Ref<Result> UPCAReader::decode(Ref<BinaryBitmap> image, DecodeHints hints){
return maybeReturnResult(ean13Reader.decode(image)); return maybeReturnResult(ean13Reader.decode(image, hints));
} }
int UPCAReader::decodeMiddle(Ref<BitArray> row, int startRange[], int startRangeLen, std::string& resultString){ int UPCAReader::decodeMiddle(Ref<BitArray> row, int startRange[], int startRangeLen, std::string& resultString){

View file

@ -19,6 +19,7 @@
*/ */
#include <zxing/oned/EAN13Reader.h> #include <zxing/oned/EAN13Reader.h>
#include <zxing/DecodeHints.h>
namespace zxing { namespace zxing {
namespace oned { namespace oned {
@ -35,7 +36,7 @@ namespace zxing {
Ref<Result> decodeRow(int rowNumber, Ref<BitArray> row); //throws ReaderException Ref<Result> decodeRow(int rowNumber, Ref<BitArray> row); //throws ReaderException
Ref<Result> decodeRow(int rowNumber, Ref<BitArray> row, int startGuardRange[]); //throws ReaderException Ref<Result> decodeRow(int rowNumber, Ref<BitArray> row, int startGuardRange[]); //throws ReaderException
Ref<Result> decode(Ref<BinaryBitmap> image); Ref<Result> decode(Ref<BinaryBitmap> image, DecodeHints hints);
BarcodeFormat getBarcodeFormat(); BarcodeFormat getBarcodeFormat();
}; };

View file

@ -30,8 +30,8 @@ namespace zxing {
QRCodeReader::QRCodeReader() :decoder_() { QRCodeReader::QRCodeReader() :decoder_() {
} }
//TODO: see if any of the other files in the qrcode tree need tryHarder
Ref<Result> QRCodeReader::decode(Ref<BinaryBitmap> image) { Ref<Result> QRCodeReader::decode(Ref<BinaryBitmap> image, DecodeHints hints) {
#ifdef DEBUG #ifdef DEBUG
cout << "decoding image " << image.object_ << ":\n" << flush; cout << "decoding image " << image.object_ << ":\n" << flush;
#endif #endif

View file

@ -23,6 +23,7 @@
#include <zxing/Reader.h> #include <zxing/Reader.h>
#include <zxing/qrcode/decoder/Decoder.h> #include <zxing/qrcode/decoder/Decoder.h>
#include <zxing/DecodeHints.h>
namespace zxing { namespace zxing {
namespace qrcode { namespace qrcode {
@ -33,7 +34,7 @@ namespace zxing {
public: public:
QRCodeReader(); QRCodeReader();
virtual Ref<Result> decode(Ref<BinaryBitmap> image); virtual Ref<Result> decode(Ref<BinaryBitmap> image, DecodeHints hints);
virtual ~QRCodeReader(); virtual ~QRCodeReader();
}; };