使用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

相关推荐
xrkhy6 小时前
微服务之ShardingSphere
数据库·微服务·oracle
JIngJaneIL7 小时前
停车场管理|停车预约管理|基于Springboot的停车场管理系统设计与实现(源码+数据库+文档)
java·数据库·spring boot·后端·论文·毕设·停车场管理系统
煎蛋学姐7 小时前
SSM儿童福利院管理系统ys9w2d07(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·ssm 框架·儿童福利院管理系统
sg_knight7 小时前
MySQL 空间索引(SPATIAL)详解:地理位置数据的高效查询利器
数据库·mysql·database·索引·关系型数据库·空间索引·spatial
梦子yumeko8 小时前
第五章Langchain4j之基于内存和redis实现聊天持久化
数据库·redis·缓存
deng-c-f9 小时前
配置(4):VScode c/c++编译环境的配置:c_cpp_properties.json
c语言·c++·vscode
IndulgeCui9 小时前
【金仓数据库产品体验官】KSQL Developer Linux版安装使用体验
linux·运维·数据库
应用市场10 小时前
Godot C++开发指南:正确获取节点的Forward/Up/Right方向向量
c++·游戏引擎·godot
一马平川的大草原10 小时前
基于n8n实现数据库多表数据同步
数据库·数据同步·dify·n8n
小-黯10 小时前
OpenGL使用C++ 绘制三角形
c++·opengl·xmake