This commit is contained in:
zhuzichu
2023-08-08 15:44:10 +08:00
parent b7acae1470
commit ebad24e809
9 changed files with 166 additions and 2 deletions

View File

@ -83,6 +83,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC
Qt::CorePrivate
Qt::QuickPrivate
Qt::QmlPrivate
ZXing
)
#安装

53
src/FluQRCode.cpp Normal file
View File

@ -0,0 +1,53 @@
#include "FluQRCode.h"
#include "BarcodeFormat.h"
#include "BitMatrix.h"
#include "MultiFormatWriter.h"
using namespace ZXing;
FluQRCode::FluQRCode(QQuickItem* parent) : QQuickPaintedItem(parent)
{
color(QColor(0,0,0,255));
size(100);
setWidth(_size);
setHeight(_size);
connect(this,&FluQRCode::textChanged,this,[=]{update();});
connect(this,&FluQRCode::colorChanged,this,[=]{update();});
connect(this,&FluQRCode::sizeChanged,this,[=]{
setWidth(_size);
setHeight(_size);
update();
});
}
void FluQRCode::paint(QPainter* painter)
{
if(_text.isEmpty()){
return;
}
if(_text.length()>1108){
return;
}
painter->save();
painter->eraseRect(boundingRect());
auto format = ZXing::BarcodeFormatFromString("QRCode");
auto writer = MultiFormatWriter(format);
writer.setMargin(0);
writer.setEncoding(ZXing::CharacterSet::UTF8);
auto matrix = writer.encode(_text.toUtf8().constData(), 0, 0);
auto bitmap = ToMatrix<uint8_t>(matrix);
auto image = QImage(bitmap.data(), bitmap.width(), bitmap.height(), bitmap.width(), QImage::Format::Format_Grayscale8).copy();
QImage rgbImage = image.convertToFormat(QImage::Format_ARGB32);
for (int y = 0; y < rgbImage.height(); ++y) {
for (int x = 0; x < rgbImage.width(); ++x) {
QRgb pixel = rgbImage.pixel(x, y);
if (qRed(pixel) == 0 && qGreen(pixel) == 0 && qBlue(pixel) == 0) {
rgbImage.setPixelColor(x, y, _color);
}
}
}
painter->drawImage(QRect(0, 0, static_cast<int>(width()), static_cast<int>(height())), rgbImage);
painter->restore();
}

22
src/FluQRCode.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef FLUQRCODE_H
#define FLUQRCODE_H
#include <QQuickItem>
#include <QQuickPaintedItem>
#include <QPainter>
#include "stdafx.h"
class FluQRCode : public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY_AUTO(QString,text)
Q_PROPERTY_AUTO(QColor,color)
Q_PROPERTY_AUTO(int,size);
QML_NAMED_ELEMENT(FluQRCode)
public:
explicit FluQRCode(QQuickItem *parent = nullptr);
void paint(QPainter* painter) override;
};
#endif // FLUQRCODE_H