Qt 生成QR code

Qt 生成QR code

QImage qt_qrencode(QString text, QPixmap logo, int width, int height)
{
	if (width == 0)
	{
		width = 300;
	}
	if (height == 0)
	{
		height = 300;
	}
	QImage image(QSize(width, height), QImage::Format_RGB32);
	QRecLevel level = QR_ECLEVEL_Q;
	QRencodeMode mode = QR_MODE_8;
	bool casesen = true;
	int margin = 10;
	double percent = 0.23;
	QColor foreground = QColor("black");
	QColor background = QColor("white");


	{
		QPainter painter(&image);
		QRcode *qrcode = QRcode_encodeString(text.toUtf8().data(), 7, (QRecLevel)level, (QRencodeMode)mode, casesen ? 1 : 0);
		if (0 != qrcode) {
			unsigned char *point = qrcode->data;
			painter.setPen(Qt::NoPen);
			painter.setBrush(background);
			painter.drawRect(0, 0, width, height);
			double scale = (width - 2.0 * margin) / qrcode->width;
			painter.setBrush(foreground);
			for (int y = 0; y < qrcode->width; y++) {
				for (int x = 0; x < qrcode->width; x++) {
					if (*point & 1) {
						QRectF r(margin + x * scale, margin + y * scale, scale, scale);
						painter.drawRects(&r, 1);
					}
					point++;
				}
			}
			point = NULL;
			QRcode_free(qrcode);


			if (!logo.isNull())
			{
				painter.setBrush(background);
				double icon_width = (width - 2.0 * margin) * percent;
				double icon_height = icon_width;
				double wrap_x = (width - icon_width) / 2.0;
				double wrap_y = (width - icon_height) / 2.0;
				QRectF wrap(wrap_x - 5, wrap_y - 5, icon_width + 10, icon_height + 10);
				painter.drawRoundRect(wrap, 50, 50);

				QRectF target(wrap_x, wrap_y, icon_width, icon_height);
				QRectF source(0, 0, logo.width(), logo.height());
				painter.drawPixmap(target, logo, source);
			}

		}
		qrcode = NULL;
	}
	
	
	return image;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值