From 630f560e89da9ee8c89c35a4aa423831a00db0c8 Mon Sep 17 00:00:00 2001 From: favoritas37 Date: Sat, 31 Oct 2015 19:14:59 +0200 Subject: [PATCH] bug fixing at qr code encoding. this tests fail --- .../qml/BarcodeEncoder/main.qml | 4 +++ source/imagehandler.cpp | 1 + source/qzxing.cpp | 3 ++ source/zxing/zxing/qrcode/decoder/Mode.h | 1 + source/zxing/zxing/qrcode/decoder/QRMode.cpp | 8 +++++ source/zxing/zxing/qrcode/encoder/Encoder.h | 4 +-- source/zxing/zxing/qrcode/encoder/QRCode.cpp | 30 ++++++++--------- source/zxing/zxing/qrcode/encoder/QRCode.h | 6 ++-- .../zxing/zxing/qrcode/encoder/QREncoder.cpp | 32 +++++++++---------- 9 files changed, 53 insertions(+), 36 deletions(-) diff --git a/examples/BarcodeEncoder/qml/BarcodeEncoder/main.qml b/examples/BarcodeEncoder/qml/BarcodeEncoder/main.qml index 2ff964c..411c60a 100644 --- a/examples/BarcodeEncoder/qml/BarcodeEncoder/main.qml +++ b/examples/BarcodeEncoder/qml/BarcodeEncoder/main.qml @@ -30,6 +30,10 @@ Rectangle { anchors.right: parent.right anchors.margins: 10 title: "Result barcode image" + + Image{ + id:resultImage + } } QZXing { diff --git a/source/imagehandler.cpp b/source/imagehandler.cpp index c939aaf..74826b0 100644 --- a/source/imagehandler.cpp +++ b/source/imagehandler.cpp @@ -42,3 +42,4 @@ void ImageHandler::save(QObject *imageObj, const QString &path, QImage img = extractQImage(imageObj, offsetX, offsetY, width, height); img.save(path); } + diff --git a/source/qzxing.cpp b/source/qzxing.cpp index 1e6668f..dbce1db 100644 --- a/source/qzxing.cpp +++ b/source/qzxing.cpp @@ -295,6 +295,9 @@ QImage QZXing::encodeData(const QString& data) for(int i=0; igetWidth(); i++) for(int j=0; jgetHeight(); j++) image.setPixel(i,j,bytes[i][j] ? 0 : 255); + + image.save("C:\\tmp.png"); + return image; } diff --git a/source/zxing/zxing/qrcode/decoder/Mode.h b/source/zxing/zxing/qrcode/decoder/Mode.h index 93f1556..ae8d67a 100644 --- a/source/zxing/zxing/qrcode/decoder/Mode.h +++ b/source/zxing/zxing/qrcode/decoder/Mode.h @@ -41,6 +41,7 @@ private: public: Mode(const Mode& mode); + Mode(); static Mode TERMINATOR; static Mode NUMERIC; diff --git a/source/zxing/zxing/qrcode/decoder/QRMode.cpp b/source/zxing/zxing/qrcode/decoder/QRMode.cpp index 5ba2ce3..994d0d3 100644 --- a/source/zxing/zxing/qrcode/decoder/QRMode.cpp +++ b/source/zxing/zxing/qrcode/decoder/QRMode.cpp @@ -43,6 +43,14 @@ Mode Mode::FNC1_FIRST_POSITION(0, 0, 0, 0x05, "FNC1_FIRST_POSITION"); Mode Mode::FNC1_SECOND_POSITION(0, 0, 0, 0x09, "FNC1_SECOND_POSITION"); Mode Mode::HANZI(8, 10, 12, 0x0D, "HANZI"); +Mode::Mode() : + characterCountBitsForVersions0To9_(0), + characterCountBitsForVersions10To26_(0), + characterCountBitsForVersions27AndHigher_(0), + bits_(0), + name_("") +{} + Mode::Mode(int cbv0_9, int cbv10_26, int cbv27, int bits, char const* name) : characterCountBitsForVersions0To9_(cbv0_9), characterCountBitsForVersions10To26_(cbv10_26), characterCountBitsForVersions27AndHigher_(cbv27), bits_(bits), name_(name) diff --git a/source/zxing/zxing/qrcode/encoder/Encoder.h b/source/zxing/zxing/qrcode/encoder/Encoder.h index 00e9aed..8086aad 100644 --- a/source/zxing/zxing/qrcode/encoder/Encoder.h +++ b/source/zxing/zxing/qrcode/encoder/Encoder.h @@ -48,14 +48,14 @@ public: */ static int getAlphanumericCode(int code); - static Mode* chooseMode(const QString& content); + static Mode chooseMode(const QString& content); /** * Choose the best mode by examining the content. Note that 'encoding' is used as a hint; * if it is Shift_JIS, and the input is only double-byte Kanji, then we return {@link Mode#KANJI}. */ private: - static Mode* chooseMode(const QString& content, const QString& encoding); + static Mode chooseMode(const QString& content, const QString& encoding); //static bool isOnlyDoubleByteKanji(const QString& content); diff --git a/source/zxing/zxing/qrcode/encoder/QRCode.cpp b/source/zxing/zxing/qrcode/encoder/QRCode.cpp index f45aaea..f95c1bf 100644 --- a/source/zxing/zxing/qrcode/encoder/QRCode.cpp +++ b/source/zxing/zxing/qrcode/encoder/QRCode.cpp @@ -6,7 +6,7 @@ namespace zxing { namespace qrcode { QRCode::QRCode() : - mode_ptr_(NULL), + mode_(), ecLevel_ptr_(NULL), version_ptr_(NULL), maskPattern_(-1), @@ -14,22 +14,22 @@ QRCode::QRCode() : QRCode::~QRCode() { - if(mode_ptr_) - delete mode_ptr_; +// if(mode_ptr_) +// delete mode_ptr_; - if(ecLevel_ptr_) - delete ecLevel_ptr_; +// if(ecLevel_ptr_) +// delete ecLevel_ptr_; - if(version_ptr_) - delete version_ptr_; +// if(version_ptr_) +// delete version_ptr_; - if(matrix_ptr_) - delete matrix_ptr_; +// if(matrix_ptr_) +// delete matrix_ptr_; } -Ref QRCode::getMode() const +Mode QRCode::getMode() const { - return mode_ptr_; + return mode_; } Ref QRCode::getECLevel() const @@ -56,8 +56,8 @@ const std::string QRCode::toString() { std::stringstream result; result << "<<\n"; - result << " mode: {unimpl}"; - result << mode_ptr_; +// result << " mode: {unimpl}"; +// result << mode_; result << "\n ecLevel: {unimpl}"; //result << ecLevel_; result << "\n version: "; @@ -74,9 +74,9 @@ const std::string QRCode::toString() return result.str(); } -void QRCode::setMode(Ref value) +void QRCode::setMode(const Mode& value) { - mode_ptr_ = value; + mode_ = value; } void QRCode::setECLevel(Ref value) diff --git a/source/zxing/zxing/qrcode/encoder/QRCode.h b/source/zxing/zxing/qrcode/encoder/QRCode.h index af67cd0..cd4f434 100644 --- a/source/zxing/zxing/qrcode/encoder/QRCode.h +++ b/source/zxing/zxing/qrcode/encoder/QRCode.h @@ -15,7 +15,7 @@ class QRCode : public Counted { private: - Ref mode_ptr_; + Mode mode_; Ref ecLevel_ptr_; Ref version_ptr_; int maskPattern_; @@ -26,13 +26,13 @@ public: QRCode(); ~QRCode(); - Ref getMode() const; + Mode getMode() const; Ref getECLevel() const; Ref getVersion() const; int getMaskPattern() const; Ref getMatrix() const; const std::string toString(); - void setMode(Ref value); + void setMode(const Mode &value); void setECLevel(Ref value); void setVersion(Ref version); void setMaskPattern(int value); diff --git a/source/zxing/zxing/qrcode/encoder/QREncoder.cpp b/source/zxing/zxing/qrcode/encoder/QREncoder.cpp index 1db7758..fa39b6a 100644 --- a/source/zxing/zxing/qrcode/encoder/QREncoder.cpp +++ b/source/zxing/zxing/qrcode/encoder/QREncoder.cpp @@ -50,14 +50,14 @@ Ref Encoder::encode(const QString& content, ErrorCorrectionLevel &ecLeve // Pick an encoding mode appropriate for the content. Note that this will not attempt to use // multiple modes / segments even if that were more efficient. Twould be nice. - Mode* mode = chooseMode(content, encoding); + Mode mode = chooseMode(content, encoding); // This will store the header information, like mode and // length, as well as "header" segments like an ECI segment. BitArray headerBits; // Append ECI segment if applicable - if (mode == &Mode::BYTE && DEFAULT_BYTE_MODE_ENCODING != encoding) { + if (mode == Mode::BYTE && DEFAULT_BYTE_MODE_ENCODING != encoding) { zxing::common::CharacterSetECI* eci = zxing::common::CharacterSetECI::getCharacterSetECIByName(encoding.toStdString().c_str()); if (eci != NULL) { @@ -66,34 +66,34 @@ Ref Encoder::encode(const QString& content, ErrorCorrectionLevel &ecLeve } // (With ECI in place,) Write the mode marker - appendModeInfo(*mode, headerBits); + appendModeInfo(mode, headerBits); // Collect data within the main segment, separately, to count its size if needed. Don't add it to // main payload yet. BitArray dataBits; - appendBytes(content, *mode, dataBits, encoding); + appendBytes(content, mode, dataBits, encoding); // Hard part: need to know version to know how many bits length takes. But need to know how many // bits it takes to know version. First we take a guess at version by assuming version will be // the minimum, 1: int provisionalBitsNeeded = headerBits.getSize() - + mode->getCharacterCountBits(Version::getVersionForNumber(1)) + + mode.getCharacterCountBits(Version::getVersionForNumber(1)) + dataBits.getSize(); Ref provisionalVersion = chooseVersion(provisionalBitsNeeded, ecLevel); // Use that guess to calculate the right version. I am still not sure this works in 100% of cases. int bitsNeeded = headerBits.getSize() - + mode->getCharacterCountBits(provisionalVersion) + + mode.getCharacterCountBits(provisionalVersion) + dataBits.getSize(); Ref version = chooseVersion(bitsNeeded, ecLevel); BitArray headerAndDataBits; headerAndDataBits.appendBitArray(headerBits); // Find "length" of main segment and write it - int numLetters = (*mode == Mode::BYTE) ? dataBits.getSizeInBytes() : content.length(); - appendLengthInfo(numLetters, *version, *mode, headerAndDataBits); + int numLetters = (mode == Mode::BYTE) ? dataBits.getSizeInBytes() : content.length(); + appendLengthInfo(numLetters, *version, mode, headerAndDataBits); // Put data together into the overall payload headerAndDataBits.appendBitArray(dataBits); @@ -112,7 +112,7 @@ Ref Encoder::encode(const QString& content, ErrorCorrectionLevel &ecLeve Ref qrCode(new QRCode); qrCode->setECLevel(Ref(&ecLevel)); - qrCode->setMode(Ref(mode)); + qrCode->setMode(mode); qrCode->setVersion(version); // Choose the mask pattern and set to "qrCode". @@ -142,7 +142,7 @@ int Encoder::getAlphanumericCode(int code) return -1; } -Mode* Encoder::chooseMode(const QString& content) +Mode Encoder::chooseMode(const QString& content) { return chooseMode(content, ""); } @@ -151,10 +151,10 @@ Mode* Encoder::chooseMode(const QString& content) * Choose the best mode by examining the content. Note that 'encoding' is used as a hint; * if it is Shift_JIS, and the input is only double-byte Kanji, then we return {@link Mode#KANJI}. */ -Mode* Encoder::chooseMode(const QString& content, const QString& encoding) +Mode Encoder::chooseMode(const QString& content, const QString& encoding) { if (encoding == "Shift_JIS") - return &(Mode::BYTE); + return Mode::BYTE; bool hasNumeric = false; bool hasAlphanumeric = false; @@ -165,16 +165,16 @@ Mode* Encoder::chooseMode(const QString& content, const QString& encoding) } else if (getAlphanumericCode(c) != -1) { hasAlphanumeric = true; } else { - return &Mode::BYTE; + return Mode::BYTE; } } if (hasAlphanumeric) { - return &Mode::ALPHANUMERIC; + return Mode::ALPHANUMERIC; } if (hasNumeric) { - return &Mode::NUMERIC; + return Mode::NUMERIC; } - return &Mode::BYTE; + return Mode::BYTE; } //bool Encoder::isOnlyDoubleByteKanji(const QString& content)