Skip to content

Commit

Permalink
Correction after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-fejcher committed Jul 8, 2024
1 parent e3d3569 commit 2876b5f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 32 deletions.
42 changes: 14 additions & 28 deletions src/SBarcodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,23 @@ bool SBarcodeGenerator::generate(const QString &inputString)
}
}

ZXing::MultiFormatWriter writer = ZXing::MultiFormatWriter(SCodes::toZXingFormat(m_format)).setMargin(
m_margin).setEccLevel(m_eccLevel);
ZXing::MultiFormatWriter writer = ZXing::MultiFormatWriter(SCodes::toZXingFormat(m_format))
.setMargin(m_margin)
.setEccLevel(m_eccLevel);

auto qrCodeMatrix = writer.encode(inputString.toStdString(), m_width, m_height);
_bitmap = ZXing::ToMatrix<uint8_t>(qrCodeMatrix);

QImage image(_bitmap.data(), m_width, m_height, QImage::Format_Grayscale8);
QImage image(m_width, m_height, QImage::Format_ARGB32);

for (int y = 0; y < m_height; ++y) {
for (int x = 0; x < m_width; ++x) {
if (qrCodeMatrix.get(x, y)) {
image.setPixelColor(x, y, m_foregroundColor);
} else {
image.setPixelColor(x, y, m_backgroundColor);
}
}
}

// Center images works only on QR codes.
if (m_format == SCodes::SBarcodeFormat::QRCode) {
Expand All @@ -55,30 +65,6 @@ bool SBarcodeGenerator::generate(const QString &inputString)

m_filePath = QDir::tempPath() + "/" + m_fileName + "." + m_extension;

// Save the final image with the QR code and center image
QFile file(m_filePath);
ZXing::MultiFormatWriter writer = ZXing::MultiFormatWriter(SCodes::toZXingFormat(m_format))
.setMargin(m_margin)
.setEccLevel(m_eccLevel);

auto qrCodeMatrix = writer.encode(inputString.toStdString(), m_width, m_height);
_bitmap = ZXing::ToMatrix<uchar>(qrCodeMatrix, m_backgroundColor.rgba(), m_foregroundColor.rgba());

m_filePath = QDir::tempPath() + "/" + m_fileName + "." + m_extension;

// Create an image with desired format
QImage image(m_width, m_height, QImage::Format_ARGB32);

for (int y = 0; y < m_height; ++y) {
for (int x = 0; x < m_width; ++x) {
if (qrCodeMatrix.get(x, y)) {
image.setPixelColor(x, y, m_foregroundColor);
} else {
image.setPixelColor(x, y, m_backgroundColor);
}
}
}

QFile file{m_filePath};
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
image.save(&file);
Expand Down
6 changes: 2 additions & 4 deletions src/SBarcodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,6 @@ public slots:

SCodes::SBarcodeFormat m_format = SCodes::SBarcodeFormat::Code128;

ZXing::Matrix<uint8_t> _bitmap = ZXing::Matrix<uint8_t>();

/*!
* \brief This method draws Rectangle and `imageRatio` smaller Image in the center of that Rectangle.
* \param QImage *parentImage - Image parent. It is used for Painter constructor.
Expand All @@ -237,8 +235,8 @@ public slots:
*/
void setEccLvel(int eccLevel);

QColor m_foregroundColor = "white";
QColor m_backgroundColor = "black";
QColor m_foregroundColor = "black";
QColor m_backgroundColor = "white";
};

#endif // SBARCODEGENERATOR_H

0 comments on commit 2876b5f

Please sign in to comment.