diff --git a/source/QZXing.h b/source/QZXing.h index 131502d..5c05d6a 100644 --- a/source/QZXing.h +++ b/source/QZXing.h @@ -127,6 +127,11 @@ public slots: const double offsetX = 0, const double offsetY = 0, const double width = 0, const double height = 0); + /** + * The main encoding function. Currently supports only Qr code encoding + */ + QImage encodeData(const QString& data); + /** * Get the prossecing time in millisecond of the last decode operation. * Added mainly as a statistic measure. diff --git a/source/qzxing.cpp b/source/qzxing.cpp index 8fba26f..3685b11 100644 --- a/source/qzxing.cpp +++ b/source/qzxing.cpp @@ -9,6 +9,8 @@ #include "imagehandler.h" #include #include +#include +#include using namespace zxing; @@ -284,6 +286,18 @@ QString QZXing::decodeSubImageQML(const QUrl &imageUrl, return decodeImage(img); } +QImage QZXing::encodeData(const QString& data) +{ + Ref barcode = qrcode::Encoder::encode(data, qrcode::ErrorCorrectionLevel::L ); + Ref bytesRef = barcode->getMatrix(); + const std::vector< std::vector >& bytes = bytesRef->getArray(); + QImage image; + for(int i=0; igetWidth(); ++i) + for(int j=0; jgetHeight(); ++i) + image.setPixel(i,j,bytes[i][j]); + return image; +} + int QZXing::getProcessTimeOfLastDecoding() { return processingTime; diff --git a/source/zxing/zxing/common/BitArray.cpp b/source/zxing/zxing/common/BitArray.cpp index 452f474..703d1df 100644 --- a/source/zxing/zxing/common/BitArray.cpp +++ b/source/zxing/zxing/common/BitArray.cpp @@ -39,7 +39,7 @@ BitArray::~BitArray() { } int BitArray::getSize() const { - return size; + return bits->size(); } void BitArray::setBulk(int i, int newBits) { @@ -198,6 +198,7 @@ void BitArray::ensureCapacity(int size) newBits[i] = bits[i]; } bits = newBits; + } } diff --git a/source/zxing/zxing/qrcode/encoder/ByteMatrix.cpp b/source/zxing/zxing/qrcode/encoder/ByteMatrix.cpp index 34f0058..72add93 100644 --- a/source/zxing/zxing/qrcode/encoder/ByteMatrix.cpp +++ b/source/zxing/zxing/qrcode/encoder/ByteMatrix.cpp @@ -7,6 +7,7 @@ namespace qrcode { ByteMatrix::ByteMatrix(size_t width, size_t height) : width_(width), height_(height) { + bytes_.resize(height_); for(size_t i=0; i encode(const QString& content, Ref ecLevel); + static Ref encode(const QString& content, ErrorCorrectionLevel &ecLevel); - static Ref encode(const QString& content, Ref ecLevel, const EncodeHint* hints); + static Ref encode(const QString& content, ErrorCorrectionLevel &ecLevel, const EncodeHint* hints); /** * @return the code point of the table used in alphanumeric mode or diff --git a/source/zxing/zxing/qrcode/encoder/MatrixUtil.cpp b/source/zxing/zxing/qrcode/encoder/MatrixUtil.cpp index 9bbc8cc..89d1729 100644 --- a/source/zxing/zxing/qrcode/encoder/MatrixUtil.cpp +++ b/source/zxing/zxing/qrcode/encoder/MatrixUtil.cpp @@ -95,7 +95,7 @@ const int MatrixUtil::TYPE_INFO_POLY = 0x537; const int MatrixUtil::TYPE_INFO_MASK_PATTERN = 0x5412; void MatrixUtil::buildMatrix(const BitArray& dataBits, - ErrorCorrectionLevel& ecLevel, + const ErrorCorrectionLevel& ecLevel, Version& version, int maskPattern, ByteMatrix& matrix) diff --git a/source/zxing/zxing/qrcode/encoder/MatrixUtil.h b/source/zxing/zxing/qrcode/encoder/MatrixUtil.h index 9d596ac..8ed33e2 100644 --- a/source/zxing/zxing/qrcode/encoder/MatrixUtil.h +++ b/source/zxing/zxing/qrcode/encoder/MatrixUtil.h @@ -130,7 +130,7 @@ public: // Build 2D matrix of QR Code from "dataBits" with "ecLevel", "version" and "getMaskPattern". On // success, store the result in "matrix" and return true. static void buildMatrix(const BitArray& dataBits, - ErrorCorrectionLevel& ecLevel, + const ErrorCorrectionLevel &ecLevel, Version& version, int maskPattern, ByteMatrix& matrix); diff --git a/source/zxing/zxing/qrcode/encoder/QREncoder.cpp b/source/zxing/zxing/qrcode/encoder/QREncoder.cpp index 30f4692..2eefc60 100644 --- a/source/zxing/zxing/qrcode/encoder/QREncoder.cpp +++ b/source/zxing/zxing/qrcode/encoder/QREncoder.cpp @@ -1,21 +1,6 @@ #include "Encoder.h" #include "MaskUtil.h" -//import com.google.zxing.EncodeHintType; -//import com.google.zxing.WriterException; -//import com.google.zxing.common.BitArray; -//import com.google.zxing.common.CharacterSetECI; -//import com.google.zxing.common.reedsolomon.GenericGF; -//import com.google.zxing.common.reedsolomon.ReedSolomonEncoder; -//import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; -//import com.google.zxing.qrcode.decoder.Mode; -//import com.google.zxing.qrcode.decoder.Version; - -//import java.io.UnsupportedEncodingException; -//import java.util.ArrayList; -//import java.util.Collection; -//import java.util.Map; - #include #include #include @@ -51,12 +36,12 @@ int Encoder::calculateMaskPenalty(const ByteMatrix& matrix) + MaskUtil::applyMaskPenaltyRule4(matrix); } -Ref Encoder::encode(const QString& content, Ref ecLevel) +Ref Encoder::encode(const QString& content, ErrorCorrectionLevel &ecLevel) { return encode(content, ecLevel, NULL); } -Ref Encoder::encode(const QString& content, Ref ecLevel, const EncodeHint* hints) +Ref Encoder::encode(const QString& content, ErrorCorrectionLevel &ecLevel, const EncodeHint* hints) { // Determine what character encoding has been specified by the caller, if any QString encoding = hints == NULL ? "" : QString(hints->getCharacterSet().c_str()); @@ -95,14 +80,14 @@ Ref Encoder::encode(const QString& content, Ref ec int provisionalBitsNeeded = headerBits.getSize() + mode->getCharacterCountBits(Version::getVersionForNumber(1)) + dataBits.getSize(); - Ref provisionalVersion = chooseVersion(provisionalBitsNeeded, *ecLevel); + 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) + dataBits.getSize(); - Ref version = chooseVersion(bitsNeeded, *ecLevel); + Ref version = chooseVersion(bitsNeeded, ecLevel); BitArray headerAndDataBits; headerAndDataBits.appendBitArray(headerBits); @@ -112,7 +97,7 @@ Ref Encoder::encode(const QString& content, Ref ec // Put data together into the overall payload headerAndDataBits.appendBitArray(dataBits); - zxing::qrcode::ECBlocks ecBlocks = version->getECBlocksForLevel(*ecLevel); + zxing::qrcode::ECBlocks ecBlocks = version->getECBlocksForLevel(ecLevel); int numDataBytes = version->getTotalCodewords() - ecBlocks.getTotalECCodewords(); // Terminate the bits properly. @@ -126,18 +111,18 @@ Ref Encoder::encode(const QString& content, Ref ec Ref qrCode(new QRCode); - qrCode->setECLevel(ecLevel); + qrCode->setECLevel(Ref(&ecLevel)); qrCode->setMode(Ref(mode)); qrCode->setVersion(version); // Choose the mask pattern and set to "qrCode". int dimension = version->getDimensionForVersion(); Ref matrix(new ByteMatrix(dimension, dimension)); - int maskPattern = chooseMaskPattern(finalBits, *ecLevel, version, matrix); + int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix); qrCode->setMaskPattern(maskPattern); // Build the matrix and set it to "qrCode". - MatrixUtil::buildMatrix(*finalBits, *ecLevel, *version, maskPattern, *matrix); + MatrixUtil::buildMatrix(*finalBits, ecLevel, *version, maskPattern, *matrix); qrCode->setMatrix(matrix); return qrCode;