C++ port: Hints infrastructure was added in r1499. This changeset implements reader selection support.

git-svn-id: https://zxing.googlecode.com/svn/trunk@1505 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
flyashi 2010-08-04 13:35:44 +00:00
parent 8f5201949e
commit c3b1380ad8
12 changed files with 169 additions and 68 deletions

View file

@ -21,6 +21,33 @@
#include <zxing/common/IllegalArgumentException.h>
namespace zxing {
const DecodeHints DecodeHints::PRODUCT_HINT(
BARCODEFORMAT_UPC_E_HINT |
BARCODEFORMAT_UPC_A_HINT |
BARCODEFORMAT_EAN_8_HINT |
BARCODEFORMAT_EAN_13_HINT);
const DecodeHints DecodeHints::ONED_HINT(
BARCODEFORMAT_UPC_E_HINT |
BARCODEFORMAT_UPC_A_HINT |
BARCODEFORMAT_EAN_8_HINT |
BARCODEFORMAT_EAN_13_HINT |
BARCODEFORMAT_CODE_128_HINT |
BARCODEFORMAT_CODE_39_HINT |
BARCODEFORMAT_ITF_HINT);
const DecodeHints DecodeHints::DEFAULT_HINT(
BARCODEFORMAT_UPC_E_HINT |
BARCODEFORMAT_UPC_A_HINT |
BARCODEFORMAT_EAN_8_HINT |
BARCODEFORMAT_EAN_13_HINT |
BARCODEFORMAT_CODE_128_HINT |
BARCODEFORMAT_CODE_39_HINT |
BARCODEFORMAT_ITF_HINT |
// TODO: uncomment once this passes QA
// BARCODEFORMAT_DATA_MATRIX_HINT |
BARCODEFORMAT_QR_CODE_HINT);
DecodeHints::DecodeHints() {
hints = 0;
}
@ -29,10 +56,6 @@ 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;

View file

@ -1,3 +1,5 @@
#ifndef __DECODEHINTS_H_
#define __DECODEHINTS_H_
/*
* DecodeHintType.h
* zxing
@ -16,8 +18,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DECODEHINTS_H_
#define DECODEHINTS_H_
#include <zxing/BarcodeFormat.h>
@ -44,29 +44,13 @@ class DecodeHints {
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;
static const DecodeHints PRODUCT_HINT;
static const DecodeHints ONED_HINT;
static const DecodeHints DEFAULT_HINT;
DecodeHints();
DecodeHints(DecodeHintType init);
~DecodeHints();
void addFormat(BarcodeFormat toadd);
bool containsFormat(BarcodeFormat tocheck) const;
void setTryHarder(bool toset);

View file

@ -28,16 +28,67 @@
namespace zxing {
MultiFormatReader::MultiFormatReader() {
readers.push_back(new zxing::qrcode::QRCodeReader());
readers.push_back(new zxing::datamatrix::DataMatrixReader());
readers.push_back(new zxing::oned::MultiFormatUPCEANReader());
readers.push_back(new zxing::oned::MultiFormatOneDReader());
}
Ref<Result> MultiFormatReader::decode(Ref<BinaryBitmap> image, DecodeHints hints){
for (unsigned int i = 0; i < readers.size(); i++) {
Ref<Result> MultiFormatReader::decode(Ref<BinaryBitmap> image) {
setHints(DecodeHints::DEFAULT_HINT);
return decodeInternal(image);
}
Ref<Result> MultiFormatReader::decode(Ref<BinaryBitmap> image, DecodeHints hints) {
setHints(hints);
return decodeInternal(image);
}
Ref<Result> MultiFormatReader::decodeWithState(Ref<BinaryBitmap> image) {
// Make sure to set up the default state so we don't crash
if (readers_.size() == 0) {
setHints(DecodeHints::DEFAULT_HINT);
}
return decodeInternal(image);
}
void MultiFormatReader::setHints(DecodeHints hints) {
hints_ = hints;
readers_.clear();
bool tryHarder = hints.getTryHarder();
bool addOneDReader = hints.containsFormat(BarcodeFormat_UPC_E) ||
hints.containsFormat(BarcodeFormat_UPC_A) ||
hints.containsFormat(BarcodeFormat_EAN_8) ||
hints.containsFormat(BarcodeFormat_EAN_13) ||
hints.containsFormat(BarcodeFormat_CODE_128) ||
hints.containsFormat(BarcodeFormat_CODE_39) ||
hints.containsFormat(BarcodeFormat_ITF);
if (addOneDReader && !tryHarder) {
readers_.push_back(new zxing::oned::MultiFormatOneDReader(hints));
}
if (hints.containsFormat(BarcodeFormat_QR_CODE)) {
readers_.push_back(new zxing::qrcode::QRCodeReader());
}
if (hints.containsFormat(BarcodeFormat_DATA_MATRIX)) {
readers_.push_back(new zxing::datamatrix::DataMatrixReader());
}
//TODO: add PDF417 here once PDF417 reader is implemented
if (addOneDReader && tryHarder) {
readers_.push_back(new zxing::oned::MultiFormatOneDReader(hints));
}
if (readers_.size() == 0) {
if (!tryHarder) {
readers_.push_back(new zxing::oned::MultiFormatOneDReader(hints));
}
readers_.push_back(new zxing::qrcode::QRCodeReader());
if (tryHarder) {
readers_.push_back(new zxing::oned::MultiFormatOneDReader(hints));
}
}
}
Ref<Result> MultiFormatReader::decodeInternal(Ref<BinaryBitmap> image) {
for (unsigned int i = 0; i < readers_.size(); i++) {
try {
return readers[i]->decode(image, hints);
return readers_[i]->decode(image, hints_);
} catch (ReaderException re) {
// continue
}
@ -46,8 +97,8 @@ namespace zxing {
}
MultiFormatReader::~MultiFormatReader(){
for (unsigned int i = 0; i < readers.size(); i++) {
delete readers[i];
for (unsigned int i = 0; i < readers_.size(); i++) {
delete readers_[i];
}
}
}

View file

@ -1,9 +1,10 @@
#ifndef __MULTI_FORMAT_READER_H__
#define __MULTI_FORMAT_READER_H__
/*
* MultiFormatBarcodeReader.h
* ZXing
*
* Created by Lukasz Warchol on 10-01-26.
* Modified by Luiz Silva on 09/02/2010.
* Copyright 2010 ZXing authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -29,12 +30,20 @@ namespace zxing {
class MultiFormatReader : public Reader {
private:
std::vector<Reader*>readers;
Ref<Result> decodeInternal(Ref<BinaryBitmap> image);
std::vector<Reader*> readers_;
DecodeHints hints_;
public:
MultiFormatReader();
Ref<Result> decode(Ref<BinaryBitmap> image);
Ref<Result> decode(Ref<BinaryBitmap> image, DecodeHints hints);
Ref<Result> decodeWithState(Ref<BinaryBitmap> image);
void setHints(DecodeHints hints);
~MultiFormatReader();
};
}
#endif

View file

@ -25,8 +25,7 @@ namespace zxing {
Reader::~Reader() { }
Ref<Result> Reader::decode(Ref<BinaryBitmap> image) {
DecodeHints hints(hints.DEFAULT_HINTS);
return decode(image, hints);
return decode(image, DecodeHints::DEFAULT_HINT);
}
}

View file

@ -5,8 +5,7 @@
* Reader.h
* zxing
*
* Created by Christian Brunschen on 13/05/2008.
* Copyright 2008 ZXing authors All rights reserved.
* 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.
@ -31,7 +30,7 @@ namespace zxing {
protected:
Reader() {}
public:
Ref<Result> decode(Ref<BinaryBitmap> image);
virtual Ref<Result> decode(Ref<BinaryBitmap> image);
virtual Ref<Result> decode(Ref<BinaryBitmap> image, DecodeHints hints) = 0;
virtual ~Reader();
};

View file

@ -214,6 +214,7 @@ namespace zxing {
endPattern[0] = row->getSize() - endPattern[1];
endPattern[1] = row->getSize() - temp;
row->reverse();
return endPattern;
} catch (ReaderException re) {
row->reverse();

View file

@ -28,11 +28,28 @@
namespace zxing {
namespace oned {
MultiFormatOneDReader::MultiFormatOneDReader() : readers() {
readers.push_back(Ref<OneDReader>(new MultiFormatUPCEANReader()));
readers.push_back(Ref<OneDReader>(new Code39Reader()));
readers.push_back(Ref<OneDReader>(new Code128Reader()));
readers.push_back(Ref<OneDReader>(new ITFReader()));
MultiFormatOneDReader::MultiFormatOneDReader(DecodeHints hints) : readers() {
if (hints.containsFormat(BarcodeFormat_EAN_13) ||
hints.containsFormat(BarcodeFormat_EAN_8) ||
hints.containsFormat(BarcodeFormat_UPC_A) ||
hints.containsFormat(BarcodeFormat_UPC_E)) {
readers.push_back(Ref<OneDReader>(new MultiFormatUPCEANReader(hints)));
}
if (hints.containsFormat(BarcodeFormat_CODE_39)) {
readers.push_back(Ref<OneDReader>(new Code39Reader()));
}
if (hints.containsFormat(BarcodeFormat_CODE_128)) {
readers.push_back(Ref<OneDReader>(new Code128Reader()));
}
if (hints.containsFormat(BarcodeFormat_ITF)) {
readers.push_back(Ref<OneDReader>(new ITFReader()));
}
if (readers.size() == 0) {
readers.push_back(Ref<OneDReader>(new MultiFormatUPCEANReader(hints)));
readers.push_back(Ref<OneDReader>(new Code39Reader()));
readers.push_back(Ref<OneDReader>(new Code128Reader()));
readers.push_back(Ref<OneDReader>(new ITFReader()));
}
}
Ref<Result> MultiFormatOneDReader::decodeRow(int rowNumber, Ref<BitArray> row){

View file

@ -1,9 +1,10 @@
#ifndef __MULTI_FORMAT_ONED_READER_H__
#define __MULTI_FORMAT_ONED_READER_H__
/*
* MultiFormatOneDReader.h
* ZXing
*
* Created by Lukasz Warchol on 10-01-25.
* Copyright 2010 ZXing authors All rights reserved.
* 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.
@ -19,8 +20,6 @@
*/
#include <zxing/oned/OneDReader.h>
#include <zxing/common/BitArray.h>
#include <zxing/Result.h>
namespace zxing {
namespace oned {
@ -29,9 +28,11 @@ namespace zxing {
private:
std::vector<Ref<OneDReader> > readers;
public:
MultiFormatOneDReader();
MultiFormatOneDReader(DecodeHints hints);
Ref<Result> decodeRow(int rowNumber, Ref<BitArray> row);
};
}
}
#endif

View file

@ -22,6 +22,7 @@
#include <zxing/oned/EAN13Reader.h>
#include <zxing/oned/EAN8Reader.h>
#include <zxing/oned/UPCEReader.h>
#include <zxing/oned/UPCAReader.h>
#include <zxing/oned/OneDResultPoint.h>
#include <zxing/common/Array.h>
#include <zxing/ReaderException.h>
@ -30,13 +31,26 @@
namespace zxing {
namespace oned {
MultiFormatUPCEANReader::MultiFormatUPCEANReader() : readers() {
readers.push_back(Ref<OneDReader>(new EAN13Reader()));
// UPC-A is covered by EAN-13
readers.push_back(Ref<OneDReader>(new EAN8Reader()));
readers.push_back(Ref<OneDReader>(new UPCEReader()));
MultiFormatUPCEANReader::MultiFormatUPCEANReader(DecodeHints hints) : readers() {
if (hints.containsFormat(BarcodeFormat_EAN_13)) {
readers.push_back(Ref<OneDReader>(new EAN13Reader()));
} else if (hints.containsFormat(BarcodeFormat_UPC_A)) {
readers.push_back(Ref<OneDReader>(new UPCAReader()));
}
if (hints.containsFormat(BarcodeFormat_EAN_8)) {
readers.push_back(Ref<OneDReader>(new EAN8Reader()));
}
if (hints.containsFormat(BarcodeFormat_UPC_E)) {
readers.push_back(Ref<OneDReader>(new UPCEReader()));
}
if (readers.size() == 0) {
readers.push_back(Ref<OneDReader>(new EAN13Reader()));
// UPC-A is covered by EAN-13
readers.push_back(Ref<OneDReader>(new EAN8Reader()));
readers.push_back(Ref<OneDReader>(new UPCEReader()));
}
}
Ref<Result> MultiFormatUPCEANReader::decodeRow(int rowNumber, Ref<BitArray> row){
// Compute this location once and reuse it on multiple implementations
int size = readers.size();

View file

@ -1,8 +1,9 @@
#ifndef __MULTI_FORMAT_UPC_EAN_READER_H__
#define __MULTI_FORMAT_UPC_EAN_READER_H__
/*
* MultiFormatUPCEANReader.h
* ZXing
*
* Created by Lukasz Warchol on 10-01-25.
* Copyright 2010 ZXing authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -18,11 +19,7 @@
* limitations under the License.
*/
#include <zxing/oned/OneDReader.h>
#include <zxing/common/BitArray.h>
#include <zxing/Result.h>
namespace zxing {
namespace oned {
@ -31,9 +28,11 @@ namespace zxing {
private:
std::vector<Ref<OneDReader> > readers;
public:
MultiFormatUPCEANReader();
MultiFormatUPCEANReader(DecodeHints hints);
Ref<Result> decodeRow(int rowNumber, Ref<BitArray> row);
};
}
}
#endif

View file

@ -76,7 +76,7 @@ int test_image(Image& image, bool hybrid, string expected = "") {
binarizer = new GlobalHistogramBinarizer(source);
}
DecodeHints hints(hints.DEFAULT_HINTS);
DecodeHints hints(DecodeHints::DEFAULT_HINT);
hints.setTryHarder(tryHarder);
Ref<BinaryBitmap> binary(new BinaryBitmap(binarizer));
Ref<Result> result(decode(binary, hints));
@ -134,7 +134,11 @@ string get_expected(string imagefilename) {
textfilename.replace(dotpos+1, textfilename.length() - dotpos - 1, "txt");
char data[MAX_EXPECTED];
FILE *fp = fopen(textfilename.data(), "rb");
if (!fp) {
// could not open file
return "";
}
// get file size
fseek(fp, 0, SEEK_END);
int toread = ftell(fp);