0
点赞
收藏
分享

微信扫一扫

vue系列——vscode,node.js vue开发环境搭建

yeamy 03-07 23:31 阅读 2
//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
//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;

	
}
//调用
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

举报

相关推荐

0 条评论