diff --git a/source/qzxing.cpp b/source/qzxing.cpp index dbce1db..e45a550 100644 --- a/source/qzxing.cpp +++ b/source/qzxing.cpp @@ -11,6 +11,7 @@ #include #include #include +#include using namespace zxing; @@ -288,15 +289,23 @@ QString QZXing::decodeSubImageQML(const QUrl &imageUrl, 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(bytesRef->getWidth(), bytesRef->getHeight(), QImage::Format_ARGB32); - for(int i=0; igetWidth(); i++) - for(int j=0; jgetHeight(); j++) - image.setPixel(i,j,bytes[i][j] ? 0 : 255); + QImage image; + try { + Ref barcode = qrcode::Encoder::encode(data, qrcode::ErrorCorrectionLevel::L ); + Ref bytesRef = barcode->getMatrix(); + const std::vector< std::vector >& bytes = bytesRef->getArray(); + QImage image(bytesRef->getWidth(), bytesRef->getHeight(), QImage::Format_ARGB32); + for(int i=0; igetWidth(); i++) + for(int j=0; jgetHeight(); j++) + image.setPixel(i, j, bytes[i][j] ? + qRgb(0,0,0) : + qRgb(255,255,255)); - image.save("C:\\tmp.png"); + image = image.scaled(240, 240); + bool success = image.save("tmp.bmp","BMP"); + } catch (std::exception& e) { + std::cout << "Error: " << e.what() << std::endl; + } return image; } diff --git a/source/zxing/zxing/common/reedsolomon/ReedSolomonEncoder.cpp b/source/zxing/zxing/common/reedsolomon/ReedSolomonEncoder.cpp index 61c7fd3..93df07c 100644 --- a/source/zxing/zxing/common/reedsolomon/ReedSolomonEncoder.cpp +++ b/source/zxing/zxing/common/reedsolomon/ReedSolomonEncoder.cpp @@ -46,7 +46,7 @@ void ReedSolomonEncoder::encode(std::vector &toEncode, int ecBytes) ArrayRef infoCoefficients(dataBytes); //memcpy(infoCoefficients.operator ->(), toEncode.data(), dataBytes); //to-do optimize the following loop - for(int i=0; i< dataBytes; ++i) + for(int i=0; i< dataBytes; i++) infoCoefficients[i] = toEncode[i]; Ref info(new GenericGFPoly(field_, infoCoefficients)); diff --git a/source/zxing/zxing/qrcode/encoder/ByteMatrix.cpp b/source/zxing/zxing/qrcode/encoder/ByteMatrix.cpp index 062939f..93d490a 100644 --- a/source/zxing/zxing/qrcode/encoder/ByteMatrix.cpp +++ b/source/zxing/zxing/qrcode/encoder/ByteMatrix.cpp @@ -49,8 +49,8 @@ void ByteMatrix::set(size_t x, size_t y, bool value) void ByteMatrix::clear(const char value) { - for (size_t y = 0; y < height_; ++y) { - for (size_t x = 0; x < width_; ++x) { + for (size_t y = 0; y < height_; y++) { + for (size_t x = 0; x < width_; x++) { bytes_[y][x] = value; } } @@ -59,8 +59,8 @@ void ByteMatrix::clear(const char value) const std::string ByteMatrix::toString() const { std::stringstream result;// = new StringBuilder(2 * width * height + 2); - for (size_t y = 0; y < height_; ++y) { - for (size_t x = 0; x < width_; ++x) { + for (size_t y = 0; y < height_; y++) { + for (size_t x = 0; x < width_; x++) { switch (bytes_[y][x]) { case 0: result << " 0"; diff --git a/source/zxing/zxing/qrcode/encoder/MatrixUtil.cpp b/source/zxing/zxing/qrcode/encoder/MatrixUtil.cpp index 89d1729..487beab 100644 --- a/source/zxing/zxing/qrcode/encoder/MatrixUtil.cpp +++ b/source/zxing/zxing/qrcode/encoder/MatrixUtil.cpp @@ -226,7 +226,7 @@ int MatrixUtil::findMSBSet(int value) { int numDigits = 0; while (value != 0) { - value >>= 1; + value = ((unsigned int)value) >> (unsigned int)1; ++numDigits; } return numDigits; diff --git a/source/zxing/zxing/qrcode/encoder/QRCode.cpp b/source/zxing/zxing/qrcode/encoder/QRCode.cpp index f95c1bf..46bd1fd 100644 --- a/source/zxing/zxing/qrcode/encoder/QRCode.cpp +++ b/source/zxing/zxing/qrcode/encoder/QRCode.cpp @@ -14,17 +14,6 @@ QRCode::QRCode() : QRCode::~QRCode() { -// if(mode_ptr_) -// delete mode_ptr_; - -// if(ecLevel_ptr_) -// delete ecLevel_ptr_; - -// if(version_ptr_) -// delete version_ptr_; - -// if(matrix_ptr_) -// delete matrix_ptr_; } Mode QRCode::getMode() const diff --git a/source/zxing/zxing/qrcode/encoder/QREncoder.cpp b/source/zxing/zxing/qrcode/encoder/QREncoder.cpp index fa39b6a..b4d2e61 100644 --- a/source/zxing/zxing/qrcode/encoder/QREncoder.cpp +++ b/source/zxing/zxing/qrcode/encoder/QREncoder.cpp @@ -107,7 +107,7 @@ Ref Encoder::encode(const QString& content, ErrorCorrectionLevel &ecLeve Ref finalBits(interleaveWithECBytes(headerAndDataBits, version->getTotalCodewords(), numDataBytes, - 1));//ecBlocks->getNumBlocks()); + ecBlocks.getECBlocks().size())); Ref qrCode(new QRCode); @@ -153,12 +153,15 @@ Mode Encoder::chooseMode(const QString& content) */ Mode Encoder::chooseMode(const QString& content, const QString& encoding) { - if (encoding == "Shift_JIS") + if (encoding == "Shift_JIS") + { + std::cout << "DEBUG: Shift_JIS detected...be aware!" << std::endl; return Mode::BYTE; + } bool hasNumeric = false; bool hasAlphanumeric = false; - for (int i = 0; i < content.size(); ++i) { + for (int i = 0; i < content.size(); i++) { char c = content.at(i).toLatin1(); if (c >= '0' && c <= '9') { hasNumeric = true; @@ -256,7 +259,7 @@ void Encoder::terminateBits(int numDataBytes, BitArray& bits) } // Append termination bits. See 8.4.8 of JISX0510:2004 (p.24) for details. // If the last byte isn't 8-bit aligned, we'll add padding bits. - int numBitsInLastByte = bits.getSize() % 8; + int numBitsInLastByte = bits.getSize() & 7;//% 8; if (numBitsInLastByte > 0) { for (int i = numBitsInLastByte; i < 8; i++) { bits.appendBit(false); @@ -265,7 +268,7 @@ void Encoder::terminateBits(int numDataBytes, BitArray& bits) // If we have more space, we'll fill the space with padding patterns defined in 8.4.9 (p.24). int bitSizeInBytes = bits.getSizeInBytes(); int numPaddingBytes = numDataBytes - bitSizeInBytes; - for (int i = 0; i < numPaddingBytes; ++i) { + for (int i = 0; i < numPaddingBytes; i++) { bits.appendBits((i & 0x01) == 0 ? 0xEC : 0x11, 8); } if (bits.getSize() != capacity) { @@ -358,9 +361,9 @@ BitArray* Encoder::interleaveWithECBytes(const BitArray& bits, int maxNumEcBytes = 0; // Since, we know the number of reedsolmon blocks, we can initialize the vector with the number. - QList< BlockPair > blocks; + std::vector< BlockPair > blocks; - for (int i = 0; i < numRSBlocks; ++i) { + for (int i = 0; i < numRSBlocks; i++) { std::vector numDataBytesInBlock; std::vector numEcBytesInBlock; getNumDataBytesAndNumECBytesForBlockID( @@ -385,8 +388,8 @@ BitArray* Encoder::interleaveWithECBytes(const BitArray& bits, BitArray* result = new BitArray; // First, place data blocks. - for (int i = 0; i < maxNumDataBytes; ++i) { - for (QList< BlockPair >::iterator it=blocks.begin(); it != blocks.end(); ++it) { + for (int i = 0; i < maxNumDataBytes; i++) { + for (std::vector< BlockPair >::iterator it=blocks.begin(); it != blocks.end(); it++) { ArrayRef dataBytes = it->getDataBytes(); if (i < dataBytes.array_->size()) { result->appendBits(dataBytes[i], 8); ///????? are we sure? @@ -394,8 +397,8 @@ BitArray* Encoder::interleaveWithECBytes(const BitArray& bits, } } // Then, place error correction blocks. - for (int i = 0; i < maxNumEcBytes; ++i) { - for (QList< BlockPair >::iterator it=blocks.begin(); it != blocks.end(); ++it) { + for (int i = 0; i < maxNumEcBytes; i++) { + for (std::vector< BlockPair >::iterator it=blocks.begin(); it != blocks.end(); it++) { ArrayRef ecBytes = it->getErrorCorrectionBytes(); if (i < ecBytes.array_->size()) { result->appendBits(ecBytes[i], 8); @@ -418,8 +421,9 @@ ArrayRef Encoder::generateECBytes(const std::vector& dataBytes, int { int numDataBytes = dataBytes.size(); std::vector toEncode; + toEncode.resize(numDataBytes + numEcBytesInBlock); for (int i = 0; i < numDataBytes; i++) - toEncode.push_back(dataBytes[i] & 0xFF); + toEncode[i] = dataBytes[i] & 0xFF; zxing::ReedSolomonEncoder encoder(GenericGF::QR_CODE_FIELD_256); encoder.encode(toEncode, numEcBytesInBlock); @@ -540,7 +544,7 @@ void Encoder::append8BitBytes(const QString& content, BitArray& bits, const QStr // throw new WriterException(uee); // } - for (int i=0; i