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

相关推荐
YOU OU3 小时前
Redis基础常识与命令
数据库·redis·缓存
2401_841495644 小时前
【操作系统】进程同步与互斥实验报告
c++·算法·操作系统·进程·并发·同步·互斥
fqbqrr4 小时前
2607C++,soui与安卓
c++·soui
Database_Cool_5 小时前
OLTP 和 OLAP 区别详解:分析型数据库和事务型数据库怎么选(附阿里云 AnalyticDB MySQL 选型指南)
数据库·mysql·阿里云
爱写代码的阿森6 小时前
鸿蒙三方库 | harmony-utils之KvUtil键值型数据库操作详解
数据库·华为·harmonyos·鸿蒙·huawei
Database_Cool_6 小时前
单机 MySQL 迁移到分布式数据库方便吗?阿里云 PolarDB-X 100% MySQL 协议兼容零改造平滑迁移
数据库·分布式·mysql
姜太小白7 小时前
【MySQL】 索引优化实战:解决 WHERE 等值 + IS NULL 查询,TEXT 字段报错 1167 的完整指南
数据库·mysql
窝子面7 小时前
手搓最简前后端协作-node
javascript·数据库
吳所畏惧7 小时前
宝塔面板Redis密码修改指南:SSH命令修改 vs 面板UI界面修改,哪个更靠谱?
运维·服务器·数据库·redis·缓存·ssh