使用QRencode做二维码QR码生成

cpp 复制代码
//qr.h
#ifndef QR_H
#define QR_H

#include <qrencode.h>
#include <qimage.h>
#include <qstring.h>

class QR
{
public:
	QR();
	//生产二维码
	 QImage produceQR(const QString &info);

public :
	static QImage produceQrTest(const QString &info);
};

#endif // QR_H
cpp 复制代码
//qr.cpp
#include "qr.h"

#include <QPainter>
#include <QImage>

QR::QR()
{

}

QImage  QR::produceQrTest(const QString &info)
{
	
	//放置二维码
	QImage dst;
	//绘制方块大小
	int scale = 4;
	//将字符串转字符集合,同时定义编码格式为UTF8
	QByteArray info_date = info.toUtf8();
	//调用libqrencode库进行编码
	QRcode* qr = QRcode_encodeString(info_date.constData(), 0, QR_ECLEVEL_Q, QR_MODE_8, 1);

	//绘制
	if (qr && qr->width > 0)
	{
		//设置图像大小
		int img_width = qr->width * scale;
		//创建画布
		dst = QImage(img_width, img_width, QImage::Format_Mono);
		//创建油漆工
		QPainter painter(&dst);
		//填充白色背景
		painter.fillRect(0, 0, img_width, img_width, Qt::white);
		//设置画笔
		painter.setPen(Qt::NoPen);
		//设置黑色刷子
		painter.setBrush(Qt::black);
		//绘制二维码
		for (int y = 0; y < qr->width; y++)
		{
			for (int x = 0; x < qr->width; x++)
			{
				//绘制黑块
				if (qr->data[y*qr->width + x] & 1)
				{
					QRect r(x*scale, y*scale, scale, scale);
					painter.drawRect(r);
				}

			}
		}
		QRcode_free(qr);
	}

	return dst;

	
}
cpp 复制代码
//调用
QImage qr = QR::produceQrTest(qstr);

int x = ui->label_QRCode->size().width() - 20;
int y = ui->label_QRCode->size().height() - 20;
QSize size = QSize(x, y);

m_QR_img = qr.scaled(size, Qt::KeepAspectRatio);

ui->label_QRCode->setPixmap(QPixmap::fromImage(m_QR_img));

QRencode库


推荐一个零声学院项目课,个人觉得老师讲得不错,分享给大家:
零声白金学习卡(含基础架构/高性能存储/golang云原生/音视频/Linux内核)
https://xxetb.xet.tech/s/3Zqhgt

相关推荐
银发控、1 小时前
MySQL联合索引
数据库·mysql
予枫的编程笔记1 小时前
【MySQL修炼篇】从踩坑到精通:事务隔离级别的3大异常(脏读/幻读/不可重复读)解决方案
数据库·mysql·后端开发·数据库事务·事务隔离级别·rr级别·脏读幻读不可重复读
一起养小猫2 小时前
Flutter for OpenHarmony 实战:记账应用数据统计与可视化
开发语言·jvm·数据库·flutter·信息可视化·harmonyos
世界尽头与你2 小时前
(修复方案)CVE-2023-22047: Oracle PeopleSoft Enterprise PeopleTools 未授权访问漏洞
数据库·安全·oracle·渗透测试
韩立学长2 小时前
【开题答辩实录分享】以《智能大学宿舍管理系统的设计与实现》为例进行选题答辩实录分享
数据库·spring boot·后端
Henry Zhu1233 小时前
数据库(五):反规范化
数据库
Mr_Xuhhh3 小时前
MySQL函数详解:日期、字符串、数学及其他常用函数
java·数据库·sql
Bella的成长园地3 小时前
面试中关于 c++ async 的高频面试问题有哪些?
c++·面试
彷徨而立3 小时前
【C/C++】什么是 运行时库?运行时库 /MT 和 /MD 的区别?
c语言·c++
qq_417129253 小时前
C++中的桥接模式变体
开发语言·c++·算法