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()<

// 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

你可能感兴趣的:(qt5 生成二维码)