QT-二维码生成器(中间头像显示)
前言
1、使用vs2017+qt5.13.2开发环境
2、生成二维码中间可以添加头像显示。
一、演示效果
二、配置说明
预处理内容:
_WINDOWS
UNICODE
_UNICODE
WIN32
_ENABLE_EXTENDED_ALIGNED_STORAGE
HAVE_CONFIG_H
QT_WIDGETS_LIB
QT_GUI_LIB
QT_CORE_LIB
三、关键程序
1.QRCode.cpp
#include "QRCode.h"
#include "QRcodeSrc/qrencode.h"
#include <QDebug>
#include <QPainter>
#include <QColor>
#include <QRectF>
#include <QImage>
#include <QFile>
#include <QPixmap>
QRCode::QRCode(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(slotBtnClicked()));
}
void QRCode::slotBtnClicked()
{
QString str = ui.lineEdit->text();
if (str.isEmpty())
return;
createQRcode(str, ui.label, QString(":/Resouce/logo.png"),0.2);
}
void QRCode::createQRcodePrivate(QString strInput, QLabel* label)
{
QRcode *qrcode;
qrcode = QRcode_encodeString(strInput.toStdString().c_str(), 2, QR_ECLEVEL_Q, QR_MODE_8, 1);
qint32 uLabelWidth = ui.label->width();
qint32 uLabelHeight = ui.label->height();
qint32 qrcode_width = qrcode->width > 0 ? qrcode->width : 1;
double scale_x = (double)uLabelWidth / (double)qrcode_width;
double scale_y = (double)uLabelHeight / (double)qrcode_width;
QImage mainImage = QImage(uLabelWidth, uLabelHeight, QImage::Format_ARGB32);
QPainter painter(&mainImage);
QColor background(Qt::white);
painter.setBrush(background);
painter.setPen(Qt::NoPen);
painter.drawRect(0, 0, uLabelWidth, uLabelHeight);
QColor foreground(Qt::black);
painter.setBrush(foreground);
for (qint32 y = 0; y < qrcode_width; y++)
{
for (qint32 x = 0; x < qrcode_width; x++)
{
unsigned char b = qrcode->data[y*qrcode_width + x];
if (b & 0x01)
{
QRectF r(x*scale_x, y*scale_y, scale_x, scale_y);
painter.drawRects(&r, 1);
}
}
}
QPixmap mainPixmap = QPixmap::fromImage(mainImage);
if (label != nullptr)
{
label->setPixmap(mainPixmap);
label->setVisible(true);
}
}
void QRCode::createQRcode(QString strInput, QLabel *label, QString logo, float scale)
{
createQRcodePrivate(strInput, label);
int nWidth = label->width();
int nHeight = label->height();
int nLogoWidth = nWidth * scale;
int nLogoHeight = nHeight * scale;
int nLogoX = (nWidth - nLogoWidth) / 2;
int nLogoY = (nWidth - nLogoHeight) / 2;
const QPixmap *pix = label->pixmap();
QFile file(logo);
file.open(QIODevice::ReadOnly);
QImage image;
if (file.isOpen())
{
QByteArray data = file.readAll();
image = QImage::fromData(data);
}
QPixmap temppix = QPixmap::fromImage(image);
QPixmap pix1 = temppix.scaled(QSize(nLogoWidth, nLogoHeight), Qt::KeepAspectRatio);
QPixmap pix2(nWidth, nHeight);
// 加个白色边框
QPainter painter1(&pix1);
painter1.setPen(QPen(Qt::white, 10));
painter1.drawRoundRect(QRect(0, 0, nLogoWidth, nLogoHeight), 20,20);
QPainter *painter = new QPainter(&pix2);
QColor background(Qt::white);
painter->setBrush(background);
painter->setPen(Qt::NoPen);
painter->drawRect(0, 0, nWidth, nHeight);
QColor foreground(Qt::black);
painter->setBrush(foreground);
painter->drawPixmap(0, 0, nWidth, nHeight, *pix);
painter->drawPixmap(nLogoX, nLogoY, nLogoWidth, nLogoHeight, pix1);
label->setPixmap(pix2);
delete painter;
}
2.QRCode.h
#pragma once
#include <QtWidgets/QMainWindow>
#include <QLabel>
#include "ui_QRCode.h"
class QRCode : public QMainWindow
{
Q_OBJECT
public:
QRCode(QWidget *parent = Q_NULLPTR);
public:
void createQRcode(QString strInput, QLabel *label, QString logo, float scale = 0.2);
private:
void createQRcodePrivate(QString strInput, QLabel *label);
public slots:
void slotBtnClicked();
private:
Ui::QRCodeClass ui;
};