qt5 生成二维码

生成二维码函数()
{
//第一种
//GetQRCode(QString::fromLocal8Bit(“www.baidu.com”),0,0,0);
//ui->widget->setPixmap(createQRCode(shared_qr_code));

//第二种
ui->label->setPixmap(createQRCode(QString::fromLocal8Bit("www.baidu.com")));

}

#include “LibQREncode/qrencode.h”
QPixmap MainWindow::createQRCode(const QString &text)
{
int margin = 2;
if (text.length() == 0)
{
return QPixmap();
}
//QRcode *qrcode = QRcode_encodeString(text.toLocal8Bit(), 2, QR_ECLEVEL_L, QR_MODE_8, 0);
QRcode *qrcode = QRcode_encodeString(text.toLocal8Bit(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);//(1原型,0大写);
if (qrcode == NULL) {
return QPixmap();
}
unsigned char *p, *q;
p = NULL;
q = NULL;
int x, y, bit;
int realwidth;
realwidth = qrcode->width;
QImage image = QImage(realwidth, realwidth, QImage::Format_Indexed8);
QRgb value;
value = qRgb(255, 255, 255);
image.setColor(0, value);
value = qRgb(0, 0, 0);
image.setColor(1, value);
image.setColor(2, value);
image.fill(0);
p = qrcode->data;
for (y = 0; ywidth; y++) {
bit = 7;
q += margin / 8;
bit = 7 - (margin % 8);
for (x = 0; xwidth; x++) {
if ((p & 1) << bit)
image.setPixel(x, y, 1);
else
image.setPixel(x, y, 0);
bit–;
if (bit < 0)
{
q++;
bit = 7;
}
p++;
}
}
return QPixmap::fromImage(image.scaledToWidth(200));
}
int QrcodeGenerator(void);
int QrcodeGeneratorTest(char
szSourceSring, unsigned char red, unsigned char green, unsigned char blue);
void MainWindow::GetQRCode(QString str_QRString,unsigned char red, unsigned char green, unsigned char blue)
{
char *szSourceSring;

//int str_length = str_QRString.length();
QByteArray ba = str_QRString.toLocal8Bit();//str_QRString.toLatin1(); // must
int str_length = ba.size();
szSourceSring = ba.data();

char cData[1024];
int i = 0;
for (; i < str_length; i++)
{
    cData[i] = szSourceSring[i * 1];
}
cData[i] = '\0';
qDebug()<<str_length<<QString(cData);
QrcodeGeneratorTest(cData, red, green, blue);

// QString exepath;//exe路径
// char filepath[256];
// char *pPath;
// GetModuleFileNameA(AfxGetInstanceHandle(), filepath, 256);
// pPath = strrchr(filepath, ‘\’);
// *pPath = 0;
// exepath = filepath;

// QRect rect;
// QString filebmppath(_T(“D:\test.bmp”));
//WIDGET加载 bitmap
}

二维码类的文件地址:https://download.csdn.net/download/lwbailw/11571528

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值