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;
};


四、程序下载

https://download.csdn.net/download/u013083044/87529124

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的大海贼

联系博主,为您提供有价值的资源

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值