From 4c7d60a47387c3b95ce82e35a6c61ba3243f64f5 Mon Sep 17 00:00:00 2001 From: flyashi Date: Wed, 4 Aug 2010 13:35:44 +0000 Subject: [PATCH] C++ port: Hints infrastructure was added in r1499. This changeset implements reader selection support. git-svn-id: http://zxing.googlecode.com/svn/trunk@1505 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- cpp/core/src/zxing/DecodeHints.cpp | 31 +++++++-- cpp/core/src/zxing/DecodeHints.h | 28 ++------ cpp/core/src/zxing/MultiFormatReader.cpp | 69 ++++++++++++++++--- cpp/core/src/zxing/MultiFormatReader.h | 17 +++-- cpp/core/src/zxing/Reader.cpp | 3 +- cpp/core/src/zxing/Reader.h | 5 +- cpp/core/src/zxing/oned/ITFReader.cpp | 1 + .../src/zxing/oned/MultiFormatOneDReader.cpp | 27 ++++++-- .../src/zxing/oned/MultiFormatOneDReader.h | 11 +-- .../zxing/oned/MultiFormatUPCEANReader.cpp | 26 +++++-- .../src/zxing/oned/MultiFormatUPCEANReader.h | 11 ++- cpp/magick/src/main.cpp | 8 ++- 12 files changed, 169 insertions(+), 68 deletions(-) diff --git a/cpp/core/src/zxing/DecodeHints.cpp b/cpp/core/src/zxing/DecodeHints.cpp index 5a9da76f..6947de55 100644 --- a/cpp/core/src/zxing/DecodeHints.cpp +++ b/cpp/core/src/zxing/DecodeHints.cpp @@ -21,6 +21,33 @@ #include 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; diff --git a/cpp/core/src/zxing/DecodeHints.h b/cpp/core/src/zxing/DecodeHints.h index cd1fe258..897939dd 100644 --- a/cpp/core/src/zxing/DecodeHints.h +++ b/cpp/core/src/zxing/DecodeHints.h @@ -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 @@ -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); diff --git a/cpp/core/src/zxing/MultiFormatReader.cpp b/cpp/core/src/zxing/MultiFormatReader.cpp index a4631888..66b4b132 100644 --- a/cpp/core/src/zxing/MultiFormatReader.cpp +++ b/cpp/core/src/zxing/MultiFormatReader.cpp @@ -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 MultiFormatReader::decode(Ref image, DecodeHints hints){ - for (unsigned int i = 0; i < readers.size(); i++) { + Ref MultiFormatReader::decode(Ref image) { + setHints(DecodeHints::DEFAULT_HINT); + return decodeInternal(image); + } + + Ref MultiFormatReader::decode(Ref image, DecodeHints hints) { + setHints(hints); + return decodeInternal(image); + } + + Ref MultiFormatReader::decodeWithState(Ref 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 MultiFormatReader::decodeInternal(Ref 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]; } } } diff --git a/cpp/core/src/zxing/MultiFormatReader.h b/cpp/core/src/zxing/MultiFormatReader.h index 44d4ce24..53d6b57d 100644 --- a/cpp/core/src/zxing/MultiFormatReader.h +++ b/cpp/core/src/zxing/MultiFormatReader.h @@ -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::vectorreaders; + Ref decodeInternal(Ref image); + + std::vector readers_; + DecodeHints hints_; + public: MultiFormatReader(); + Ref decode(Ref image); Ref decode(Ref image, DecodeHints hints); - + Ref decodeWithState(Ref image); + void setHints(DecodeHints hints); ~MultiFormatReader(); }; } + +#endif diff --git a/cpp/core/src/zxing/Reader.cpp b/cpp/core/src/zxing/Reader.cpp index 8326051b..d64f8251 100755 --- a/cpp/core/src/zxing/Reader.cpp +++ b/cpp/core/src/zxing/Reader.cpp @@ -25,8 +25,7 @@ namespace zxing { Reader::~Reader() { } Ref Reader::decode(Ref image) { - DecodeHints hints(hints.DEFAULT_HINTS); - return decode(image, hints); + return decode(image, DecodeHints::DEFAULT_HINT); } } diff --git a/cpp/core/src/zxing/Reader.h b/cpp/core/src/zxing/Reader.h index 8f16c98c..05c741b4 100755 --- a/cpp/core/src/zxing/Reader.h +++ b/cpp/core/src/zxing/Reader.h @@ -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 decode(Ref image); + virtual Ref decode(Ref image); virtual Ref decode(Ref image, DecodeHints hints) = 0; virtual ~Reader(); }; diff --git a/cpp/core/src/zxing/oned/ITFReader.cpp b/cpp/core/src/zxing/oned/ITFReader.cpp index 7db44f9e..3848d6e1 100644 --- a/cpp/core/src/zxing/oned/ITFReader.cpp +++ b/cpp/core/src/zxing/oned/ITFReader.cpp @@ -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(); diff --git a/cpp/core/src/zxing/oned/MultiFormatOneDReader.cpp b/cpp/core/src/zxing/oned/MultiFormatOneDReader.cpp index ea444714..904601eb 100644 --- a/cpp/core/src/zxing/oned/MultiFormatOneDReader.cpp +++ b/cpp/core/src/zxing/oned/MultiFormatOneDReader.cpp @@ -28,11 +28,28 @@ namespace zxing { namespace oned { - MultiFormatOneDReader::MultiFormatOneDReader() : readers() { - readers.push_back(Ref(new MultiFormatUPCEANReader())); - readers.push_back(Ref(new Code39Reader())); - readers.push_back(Ref(new Code128Reader())); - readers.push_back(Ref(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(new MultiFormatUPCEANReader(hints))); + } + if (hints.containsFormat(BarcodeFormat_CODE_39)) { + readers.push_back(Ref(new Code39Reader())); + } + if (hints.containsFormat(BarcodeFormat_CODE_128)) { + readers.push_back(Ref(new Code128Reader())); + } + if (hints.containsFormat(BarcodeFormat_ITF)) { + readers.push_back(Ref(new ITFReader())); + } + if (readers.size() == 0) { + readers.push_back(Ref(new MultiFormatUPCEANReader(hints))); + readers.push_back(Ref(new Code39Reader())); + readers.push_back(Ref(new Code128Reader())); + readers.push_back(Ref(new ITFReader())); + } } Ref MultiFormatOneDReader::decodeRow(int rowNumber, Ref row){ diff --git a/cpp/core/src/zxing/oned/MultiFormatOneDReader.h b/cpp/core/src/zxing/oned/MultiFormatOneDReader.h index 0e5ab530..33421126 100644 --- a/cpp/core/src/zxing/oned/MultiFormatOneDReader.h +++ b/cpp/core/src/zxing/oned/MultiFormatOneDReader.h @@ -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 -#include -#include namespace zxing { namespace oned { @@ -29,9 +28,11 @@ namespace zxing { private: std::vector > readers; public: - MultiFormatOneDReader(); + MultiFormatOneDReader(DecodeHints hints); Ref decodeRow(int rowNumber, Ref row); }; } } + +#endif diff --git a/cpp/core/src/zxing/oned/MultiFormatUPCEANReader.cpp b/cpp/core/src/zxing/oned/MultiFormatUPCEANReader.cpp index 24aa5e6f..a11243c8 100644 --- a/cpp/core/src/zxing/oned/MultiFormatUPCEANReader.cpp +++ b/cpp/core/src/zxing/oned/MultiFormatUPCEANReader.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -30,13 +31,26 @@ namespace zxing { namespace oned { - MultiFormatUPCEANReader::MultiFormatUPCEANReader() : readers() { - readers.push_back(Ref(new EAN13Reader())); - // UPC-A is covered by EAN-13 - readers.push_back(Ref(new EAN8Reader())); - readers.push_back(Ref(new UPCEReader())); + MultiFormatUPCEANReader::MultiFormatUPCEANReader(DecodeHints hints) : readers() { + if (hints.containsFormat(BarcodeFormat_EAN_13)) { + readers.push_back(Ref(new EAN13Reader())); + } else if (hints.containsFormat(BarcodeFormat_UPC_A)) { + readers.push_back(Ref(new UPCAReader())); + } + if (hints.containsFormat(BarcodeFormat_EAN_8)) { + readers.push_back(Ref(new EAN8Reader())); + } + if (hints.containsFormat(BarcodeFormat_UPC_E)) { + readers.push_back(Ref(new UPCEReader())); + } + if (readers.size() == 0) { + readers.push_back(Ref(new EAN13Reader())); + // UPC-A is covered by EAN-13 + readers.push_back(Ref(new EAN8Reader())); + readers.push_back(Ref(new UPCEReader())); + } } - + Ref MultiFormatUPCEANReader::decodeRow(int rowNumber, Ref row){ // Compute this location once and reuse it on multiple implementations int size = readers.size(); diff --git a/cpp/core/src/zxing/oned/MultiFormatUPCEANReader.h b/cpp/core/src/zxing/oned/MultiFormatUPCEANReader.h index 2c5b196b..b0bae4fc 100644 --- a/cpp/core/src/zxing/oned/MultiFormatUPCEANReader.h +++ b/cpp/core/src/zxing/oned/MultiFormatUPCEANReader.h @@ -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 -#include -#include namespace zxing { namespace oned { @@ -31,9 +28,11 @@ namespace zxing { private: std::vector > readers; public: - MultiFormatUPCEANReader(); + MultiFormatUPCEANReader(DecodeHints hints); Ref decodeRow(int rowNumber, Ref row); }; } } + +#endif diff --git a/cpp/magick/src/main.cpp b/cpp/magick/src/main.cpp index ceb9cfca..1aba19fe 100644 --- a/cpp/magick/src/main.cpp +++ b/cpp/magick/src/main.cpp @@ -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 binary(new BinaryBitmap(binarizer)); Ref 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); -- 2.20.1