mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
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:
parent
f7392b3ffe
commit
7bba1c4680
80
cpp/core/src/zxing/DecodeHints.cpp
Normal file
80
cpp/core/src/zxing/DecodeHints.cpp
Normal 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 */
|
79
cpp/core/src/zxing/DecodeHints.h
Normal file
79
cpp/core/src/zxing/DecodeHints.h
Normal 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
|
|
@ -34,10 +34,10 @@ namespace zxing {
|
|||
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++) {
|
||||
try {
|
||||
return readers[i]->decode(image);
|
||||
return readers[i]->decode(image, hints);
|
||||
} catch (ReaderException re) {
|
||||
// continue
|
||||
}
|
||||
|
@ -50,4 +50,4 @@ namespace zxing {
|
|||
delete readers[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include <zxing/Reader.h>
|
||||
#include <zxing/common/BitArray.h>
|
||||
#include <zxing/Result.h>
|
||||
#include <zxing/DecodeHints.h>
|
||||
|
||||
namespace zxing {
|
||||
class MultiFormatReader : public Reader {
|
||||
|
@ -32,7 +33,7 @@ namespace zxing {
|
|||
public:
|
||||
MultiFormatReader();
|
||||
|
||||
Ref<Result> decode(Ref<BinaryBitmap> image);
|
||||
Ref<Result> decode(Ref<BinaryBitmap> image, DecodeHints hints);
|
||||
|
||||
~MultiFormatReader();
|
||||
};
|
||||
|
|
|
@ -24,4 +24,9 @@ namespace zxing {
|
|||
|
||||
Reader::~Reader() { }
|
||||
|
||||
Ref<Result> Reader::decode(Ref<BinaryBitmap> image) {
|
||||
DecodeHints hints(hints.DEFAULT_HINTS);
|
||||
return decode(image, hints);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
|
||||
#include <zxing/BinaryBitmap.h>
|
||||
#include <zxing/Result.h>
|
||||
#include <zxing/DecodeHints.h>
|
||||
|
||||
namespace zxing {
|
||||
|
||||
|
@ -30,7 +31,8 @@ namespace zxing {
|
|||
protected:
|
||||
Reader() {}
|
||||
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();
|
||||
};
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ DataMatrixReader::DataMatrixReader() :
|
|||
decoder_() {
|
||||
}
|
||||
|
||||
Ref<Result> DataMatrixReader::decode(Ref<BinaryBitmap> image) {
|
||||
Ref<Result> DataMatrixReader::decode(Ref<BinaryBitmap> image, DecodeHints hints) {
|
||||
#ifdef DEBUG
|
||||
cout << "decoding image " << image.object_ << ":\n" << flush;
|
||||
#endif
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
*/
|
||||
|
||||
#include <zxing/Reader.h>
|
||||
#include <zxing/DecodeHints.h>
|
||||
#include <zxing/datamatrix/decoder/Decoder.h>
|
||||
|
||||
namespace zxing {
|
||||
|
@ -33,7 +34,7 @@ private:
|
|||
|
||||
public:
|
||||
DataMatrixReader();
|
||||
virtual Ref<Result> decode(Ref<BinaryBitmap> image);
|
||||
virtual Ref<Result> decode(Ref<BinaryBitmap> image, DecodeHints hints);
|
||||
virtual ~DataMatrixReader();
|
||||
|
||||
};
|
||||
|
|
|
@ -31,14 +31,16 @@ namespace zxing {
|
|||
OneDReader::OneDReader() {
|
||||
}
|
||||
|
||||
Ref<Result> OneDReader::decode(Ref<BinaryBitmap> image) {
|
||||
try {
|
||||
return doDecode(image);
|
||||
Ref<Result> OneDReader::decode(Ref<BinaryBitmap> image, DecodeHints hints) {
|
||||
|
||||
try {
|
||||
return doDecode(image, hints);
|
||||
}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
|
||||
Hashtable metadata = result.getResultMetadata();
|
||||
int orientation = 270;
|
||||
|
@ -48,34 +50,32 @@ namespace zxing {
|
|||
((Integer) metadata.get(ResultMetadataType.ORIENTATION)).intValue()) % 360;
|
||||
}
|
||||
result.putMetadata(ResultMetadataType.ORIENTATION, new Integer(orientation));
|
||||
// Update result points
|
||||
ResultPoint[] points = result.getResultPoints();
|
||||
int height = rotatedImage.getHeight();
|
||||
for (int i = 0; i < points.length; i++) {
|
||||
points[i] = new ResultPoint(height - points[i].getY() - 1, points[i].getX());
|
||||
*/
|
||||
// Update result points
|
||||
std::vector<Ref<ResultPoint> > points (result->getResultPoints());
|
||||
int height = rotatedImage->getHeight();
|
||||
for (size_t i = 0; i < points.size(); i++) {
|
||||
points[i].reset(new OneDResultPoint(height - points[i]->getY() - 1, points[i]->getX()));
|
||||
}
|
||||
return result;
|
||||
*/
|
||||
} else {
|
||||
throw re;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ref<Result> OneDReader::doDecode(Ref<BinaryBitmap> image){
|
||||
Ref<Result> OneDReader::doDecode(Ref<BinaryBitmap> image, DecodeHints hints){
|
||||
int width = image->getWidth();
|
||||
int height = image->getHeight();
|
||||
Ref<BitArray> row(new BitArray(width));
|
||||
// BitArray row = new BitArray(width);
|
||||
|
||||
int middle = height >> 1;
|
||||
bool tryHarder = true;//hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
|
||||
int rowStep = (int)fmax(1, height >> (tryHarder ? 7 : 4));
|
||||
bool tryHarder = hints.getTryHarder();
|
||||
int rowStep = (int)fmax(1, height >> (tryHarder ? 8 : 5));
|
||||
int maxLines;
|
||||
if (tryHarder) {
|
||||
maxLines = height; // Look at the whole image, not just the center
|
||||
} 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++) {
|
||||
|
@ -95,7 +95,7 @@ namespace zxing {
|
|||
}catch (ReaderException re) {
|
||||
continue;
|
||||
}catch (IllegalArgumentException re) {
|
||||
continue;
|
||||
continue;
|
||||
}
|
||||
|
||||
// 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
|
||||
// result.putMetadata(ResultMetadataType.ORIENTATION, new Integer(180));
|
||||
// // And remember to flip the result points horizontally.
|
||||
std::vector<Ref<ResultPoint> > points(result->getResultPoints());
|
||||
if (points.size() == 2) {
|
||||
Ref<ResultPoint> pointZero(new OneDResultPoint(width - points[0]->getX() - 1, points[0]->getY()));
|
||||
points[0] = pointZero;
|
||||
std::vector<Ref<ResultPoint> > points(result->getResultPoints());
|
||||
// if there's exactly two points (which there should be), flip the x coordinate
|
||||
// if there's not exactly 2, I don't know what do do with it
|
||||
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()));
|
||||
points[1] = pointOne;
|
||||
Ref<ResultPoint> pointOne(new OneDResultPoint(width - points[1]->getX() - 1, points[1]->getY()));
|
||||
points[1] = pointOne;
|
||||
|
||||
result.reset(new Result(result->getText(),result->getRawBytes(),points,result->getBarcodeFormat()));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return result;
|
||||
} catch (ReaderException re) {
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include <zxing/Reader.h>
|
||||
#include <zxing/common/BitArray.h>
|
||||
#include <zxing/BinaryBitmap.h>
|
||||
#include <zxing/DecodeHints.h>
|
||||
|
||||
namespace zxing {
|
||||
namespace oned {
|
||||
|
@ -30,12 +31,12 @@ namespace zxing {
|
|||
private:
|
||||
static const int INTEGER_MATH_SHIFT = 8;
|
||||
|
||||
Ref<Result> doDecode(Ref<BinaryBitmap> image);
|
||||
Ref<Result> doDecode(Ref<BinaryBitmap> image, DecodeHints hints);
|
||||
public:
|
||||
static const int PATTERN_MATCH_RESULT_SCALE_FACTOR = 1 << INTEGER_MATH_SHIFT;
|
||||
|
||||
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;
|
||||
|
||||
static unsigned int patternMatchVariance(int counters[], int countersSize, const int pattern[], int maxIndividualVariance);
|
||||
|
|
|
@ -32,8 +32,8 @@ namespace zxing {
|
|||
Ref<Result> UPCAReader::decodeRow(int rowNumber, Ref<BitArray> row, int startGuardRange[]){
|
||||
return maybeReturnResult(ean13Reader.decodeRow(rowNumber, row, startGuardRange));
|
||||
}
|
||||
Ref<Result> UPCAReader::decode(Ref<BinaryBitmap> image){
|
||||
return maybeReturnResult(ean13Reader.decode(image));
|
||||
Ref<Result> UPCAReader::decode(Ref<BinaryBitmap> image, DecodeHints hints){
|
||||
return maybeReturnResult(ean13Reader.decode(image, hints));
|
||||
}
|
||||
|
||||
int UPCAReader::decodeMiddle(Ref<BitArray> row, int startRange[], int startRangeLen, std::string& resultString){
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
*/
|
||||
|
||||
#include <zxing/oned/EAN13Reader.h>
|
||||
#include <zxing/DecodeHints.h>
|
||||
|
||||
namespace zxing {
|
||||
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, int startGuardRange[]); //throws ReaderException
|
||||
Ref<Result> decode(Ref<BinaryBitmap> image);
|
||||
Ref<Result> decode(Ref<BinaryBitmap> image, DecodeHints hints);
|
||||
|
||||
BarcodeFormat getBarcodeFormat();
|
||||
};
|
||||
|
|
|
@ -30,8 +30,8 @@ namespace zxing {
|
|||
|
||||
QRCodeReader::QRCodeReader() :decoder_() {
|
||||
}
|
||||
|
||||
Ref<Result> QRCodeReader::decode(Ref<BinaryBitmap> image) {
|
||||
//TODO: see if any of the other files in the qrcode tree need tryHarder
|
||||
Ref<Result> QRCodeReader::decode(Ref<BinaryBitmap> image, DecodeHints hints) {
|
||||
#ifdef DEBUG
|
||||
cout << "decoding image " << image.object_ << ":\n" << flush;
|
||||
#endif
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
|
||||
#include <zxing/Reader.h>
|
||||
#include <zxing/qrcode/decoder/Decoder.h>
|
||||
#include <zxing/DecodeHints.h>
|
||||
|
||||
namespace zxing {
|
||||
namespace qrcode {
|
||||
|
@ -33,7 +34,7 @@ namespace zxing {
|
|||
|
||||
public:
|
||||
QRCodeReader();
|
||||
virtual Ref<Result> decode(Ref<BinaryBitmap> image);
|
||||
virtual Ref<Result> decode(Ref<BinaryBitmap> image, DecodeHints hints);
|
||||
virtual ~QRCodeReader();
|
||||
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue