windows下 Qt /C++使用QRencode生成二维码

这里写目录标题

简介

公司需要,实现代码创建一个二维码,找了下资料,使用QRencode比较简单,功能也比较齐全。

前提

需要我们编译完整的库,然后,将库引入工程才能正常使用
编译的过程详情参考 https://blog.csdn.net/amxld/article/details/113725075
已生成的windows平台的动态库,静态库,头文件包详情下载
https://download.csdn.net/download/amxld/15110962

引入库

首先在我们的pro文件中引入

 LIBS += $$PWD/lib/qrencoded.lib
 INCLUDEPATH += $$PWD/include

在编译目录下加入qrencoded.dll(必须)
在这里插入图片描述
基本工作做完后,直接上代码即可
这里直接使用代码,看是否能生成二维码,其他功能,在二维码上加图片什么的都可以自己实现

  QPainter painter(this);
    //NOTE: I have hardcoded some parameters here that would make more sense as variables.
    QRcode *qr = QRcode_encodeString(data.toStdString().c_str(), 1, QR_ECLEVEL_L, QR_MODE_8, 0);
    if(0!=qr)
    {
    
    

        QColor fg("black");
        QColor bg("white");
        painter.setBrush(bg);
        painter.setPen(Qt::NoPen);
        painter.drawRect(0,0,width(),height());
        painter.setBrush(fg);
        const int s=qr->width>0?qr->width:1;
        const double w=width();
        const double h=height();
        const double aspect=w/h;
        const double scale=((aspect>1.0)?h:w)/s;
        for(int y=0;y<s;y++){
    
    
            const int yy=y*s;
            for(int x=0;x<s;x++){
    
    
                const int xx=yy+x;
                const unsigned char b=qr->data[xx];
                if(b &0x01){
    
    
                    const double rx1=x*scale, ry1=y*scale;
                    QRectF r(rx1, ry1, scale, scale);
                    painter.drawRects(&r,1);
                }
            }
        }
        QRcode_free(qr);
    }
    else
    {
    
    
        qDebug()<<"QR FAIL: "<< ("errno");
    }

附结果图
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/amxld/article/details/113725352