Added the main function that will be used for encoding in qzxing.cpp + latest bug fixes

This commit is contained in:
favoritas37 2015-07-16 22:28:28 +03:00
parent 8dbdb08997
commit 95a2e44571
8 changed files with 35 additions and 29 deletions

View File

@ -127,6 +127,11 @@ public slots:
const double offsetX = 0, const double offsetY = 0, const double offsetX = 0, const double offsetY = 0,
const double width = 0, const double height = 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. * Get the prossecing time in millisecond of the last decode operation.
* Added mainly as a statistic measure. * Added mainly as a statistic measure.

View File

@ -9,6 +9,8 @@
#include "imagehandler.h" #include "imagehandler.h"
#include <QTime> #include <QTime>
#include <QUrl> #include <QUrl>
#include <zxing/qrcode/encoder/Encoder.h>
#include <zxing/qrcode/ErrorCorrectionLevel.h>
using namespace zxing; using namespace zxing;
@ -284,6 +286,18 @@ QString QZXing::decodeSubImageQML(const QUrl &imageUrl,
return decodeImage(img); return decodeImage(img);
} }
QImage QZXing::encodeData(const QString& data)
{
Ref<qrcode::QRCode> barcode = qrcode::Encoder::encode(data, qrcode::ErrorCorrectionLevel::L );
Ref<qrcode::ByteMatrix> bytesRef = barcode->getMatrix();
const std::vector< std::vector <char> >& bytes = bytesRef->getArray();
QImage image;
for(int i=0; i<bytesRef->getWidth(); ++i)
for(int j=0; j<bytesRef->getHeight(); ++i)
image.setPixel(i,j,bytes[i][j]);
return image;
}
int QZXing::getProcessTimeOfLastDecoding() int QZXing::getProcessTimeOfLastDecoding()
{ {
return processingTime; return processingTime;

View File

@ -39,7 +39,7 @@ BitArray::~BitArray() {
} }
int BitArray::getSize() const { int BitArray::getSize() const {
return size; return bits->size();
} }
void BitArray::setBulk(int i, int newBits) { void BitArray::setBulk(int i, int newBits) {
@ -198,6 +198,7 @@ void BitArray::ensureCapacity(int size)
newBits[i] = bits[i]; newBits[i] = bits[i];
} }
bits = newBits; bits = newBits;
} }
} }

View File

@ -7,6 +7,7 @@ namespace qrcode {
ByteMatrix::ByteMatrix(size_t width, size_t height) : ByteMatrix::ByteMatrix(size_t width, size_t height) :
width_(width), height_(height) width_(width), height_(height)
{ {
bytes_.resize(height_);
for(size_t i=0; i<height; ++i) for(size_t i=0; i<height; ++i)
bytes_[i].resize(width); bytes_[i].resize(width);
} }

View File

@ -30,7 +30,7 @@ private:
* Encode "bytes" with the error correction level "ecLevel". The encoding mode will be chosen * Encode "bytes" with the error correction level "ecLevel". The encoding mode will be chosen
* internally by chooseMode(). On success, store the result in "qrCode". * internally by chooseMode(). On success, store the result in "qrCode".
* *
* We recommend you to use QRCode.EC_LEVEL_L (the lowest level) for * We recommend you to use QRCode::EC_LEVEL_L (the lowest level) for
* "getECLevel" since our primary use is to show QR code on desktop screens. We don't need very * "getECLevel" since our primary use is to show QR code on desktop screens. We don't need very
* strong error correction for this purpose. * strong error correction for this purpose.
* *
@ -38,9 +38,9 @@ private:
* with which clients can specify the encoding mode. For now, we don't need the functionality. * with which clients can specify the encoding mode. For now, we don't need the functionality.
*/ */
public: public:
static Ref<QRCode> encode(const QString& content, Ref<ErrorCorrectionLevel> ecLevel); static Ref<QRCode> encode(const QString& content, ErrorCorrectionLevel &ecLevel);
static Ref<QRCode> encode(const QString& content, Ref<ErrorCorrectionLevel> ecLevel, const EncodeHint* hints); static Ref<QRCode> encode(const QString& content, ErrorCorrectionLevel &ecLevel, const EncodeHint* hints);
/** /**
* @return the code point of the table used in alphanumeric mode or * @return the code point of the table used in alphanumeric mode or

View File

@ -95,7 +95,7 @@ const int MatrixUtil::TYPE_INFO_POLY = 0x537;
const int MatrixUtil::TYPE_INFO_MASK_PATTERN = 0x5412; const int MatrixUtil::TYPE_INFO_MASK_PATTERN = 0x5412;
void MatrixUtil::buildMatrix(const BitArray& dataBits, void MatrixUtil::buildMatrix(const BitArray& dataBits,
ErrorCorrectionLevel& ecLevel, const ErrorCorrectionLevel& ecLevel,
Version& version, Version& version,
int maskPattern, int maskPattern,
ByteMatrix& matrix) ByteMatrix& matrix)

View File

@ -130,7 +130,7 @@ public:
// Build 2D matrix of QR Code from "dataBits" with "ecLevel", "version" and "getMaskPattern". On // Build 2D matrix of QR Code from "dataBits" with "ecLevel", "version" and "getMaskPattern". On
// success, store the result in "matrix" and return true. // success, store the result in "matrix" and return true.
static void buildMatrix(const BitArray& dataBits, static void buildMatrix(const BitArray& dataBits,
ErrorCorrectionLevel& ecLevel, const ErrorCorrectionLevel &ecLevel,
Version& version, Version& version,
int maskPattern, int maskPattern,
ByteMatrix& matrix); ByteMatrix& matrix);

View File

@ -1,21 +1,6 @@
#include "Encoder.h" #include "Encoder.h"
#include "MaskUtil.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 <zxing/common/CharacterSetECI.h> #include <zxing/common/CharacterSetECI.h>
#include <zxing/UnsupportedEncodingException.h> #include <zxing/UnsupportedEncodingException.h>
#include <zxing/WriterException.h> #include <zxing/WriterException.h>
@ -51,12 +36,12 @@ int Encoder::calculateMaskPenalty(const ByteMatrix& matrix)
+ MaskUtil::applyMaskPenaltyRule4(matrix); + MaskUtil::applyMaskPenaltyRule4(matrix);
} }
Ref<QRCode> Encoder::encode(const QString& content, Ref<ErrorCorrectionLevel> ecLevel) Ref<QRCode> Encoder::encode(const QString& content, ErrorCorrectionLevel &ecLevel)
{ {
return encode(content, ecLevel, NULL); return encode(content, ecLevel, NULL);
} }
Ref<QRCode> Encoder::encode(const QString& content, Ref<ErrorCorrectionLevel> ecLevel, const EncodeHint* hints) Ref<QRCode> Encoder::encode(const QString& content, ErrorCorrectionLevel &ecLevel, const EncodeHint* hints)
{ {
// Determine what character encoding has been specified by the caller, if any // Determine what character encoding has been specified by the caller, if any
QString encoding = hints == NULL ? "" : QString(hints->getCharacterSet().c_str()); QString encoding = hints == NULL ? "" : QString(hints->getCharacterSet().c_str());
@ -95,14 +80,14 @@ Ref<QRCode> Encoder::encode(const QString& content, Ref<ErrorCorrectionLevel> ec
int provisionalBitsNeeded = headerBits.getSize() int provisionalBitsNeeded = headerBits.getSize()
+ mode->getCharacterCountBits(Version::getVersionForNumber(1)) + mode->getCharacterCountBits(Version::getVersionForNumber(1))
+ dataBits.getSize(); + dataBits.getSize();
Ref<Version> provisionalVersion = chooseVersion(provisionalBitsNeeded, *ecLevel); Ref<Version> provisionalVersion = chooseVersion(provisionalBitsNeeded, ecLevel);
// Use that guess to calculate the right version. I am still not sure this works in 100% of cases. // Use that guess to calculate the right version. I am still not sure this works in 100% of cases.
int bitsNeeded = headerBits.getSize() int bitsNeeded = headerBits.getSize()
+ mode->getCharacterCountBits(provisionalVersion) + mode->getCharacterCountBits(provisionalVersion)
+ dataBits.getSize(); + dataBits.getSize();
Ref<Version> version = chooseVersion(bitsNeeded, *ecLevel); Ref<Version> version = chooseVersion(bitsNeeded, ecLevel);
BitArray headerAndDataBits; BitArray headerAndDataBits;
headerAndDataBits.appendBitArray(headerBits); headerAndDataBits.appendBitArray(headerBits);
@ -112,7 +97,7 @@ Ref<QRCode> Encoder::encode(const QString& content, Ref<ErrorCorrectionLevel> ec
// Put data together into the overall payload // Put data together into the overall payload
headerAndDataBits.appendBitArray(dataBits); headerAndDataBits.appendBitArray(dataBits);
zxing::qrcode::ECBlocks ecBlocks = version->getECBlocksForLevel(*ecLevel); zxing::qrcode::ECBlocks ecBlocks = version->getECBlocksForLevel(ecLevel);
int numDataBytes = version->getTotalCodewords() - ecBlocks.getTotalECCodewords(); int numDataBytes = version->getTotalCodewords() - ecBlocks.getTotalECCodewords();
// Terminate the bits properly. // Terminate the bits properly.
@ -126,18 +111,18 @@ Ref<QRCode> Encoder::encode(const QString& content, Ref<ErrorCorrectionLevel> ec
Ref<QRCode> qrCode(new QRCode); Ref<QRCode> qrCode(new QRCode);
qrCode->setECLevel(ecLevel); qrCode->setECLevel(Ref<ErrorCorrectionLevel>(&ecLevel));
qrCode->setMode(Ref<Mode>(mode)); qrCode->setMode(Ref<Mode>(mode));
qrCode->setVersion(version); qrCode->setVersion(version);
// Choose the mask pattern and set to "qrCode". // Choose the mask pattern and set to "qrCode".
int dimension = version->getDimensionForVersion(); int dimension = version->getDimensionForVersion();
Ref<ByteMatrix> matrix(new ByteMatrix(dimension, dimension)); Ref<ByteMatrix> matrix(new ByteMatrix(dimension, dimension));
int maskPattern = chooseMaskPattern(finalBits, *ecLevel, version, matrix); int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix);
qrCode->setMaskPattern(maskPattern); qrCode->setMaskPattern(maskPattern);
// Build the matrix and set it to "qrCode". // 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); qrCode->setMatrix(matrix);
return qrCode; return qrCode;