ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Windows下使用qrencode生成二维码

2021-10-19 12:34:29  阅读:251  来源: 互联网

标签:qrPixmap qrencode Windows Width 二维码 scale qrCode painter


二维码介绍

参考:
https://coolshell.cn/articles/10590.html#jtss-tsina
https://www.cnblogs.com/magicsoar/p/4483032.html

qrencode介绍

QR码是当前最流行的二维码之一,它具有可靠性高,识别速度快等特点.而qrencode则是一款由C语言(完全兼容C++)写成的一个QR码生成与解码的函数库.它以GNU LGPL协议发布。

静态库和动态库的生成

参考:
https://blog.csdn.net/wzfgd/article/details/106230908

下载qrencode源码,使用cmake生成库文件。这里要注意选择的系统位数(32位和64位)和生成库文件的版本(debuge版和release版)。
这里我提供一下Windows下32位的库文件(链接:https://pan.baidu.com/s/1yHu2IxdR-5wYF41g0B81pA 提取码:8888 ),64位自行操作。

获取

 /**
 * @brief GernerateQRCode
 * 生成二维码函数
 * @param text  二维码内容
 * @param qrPixmap  二维码像素图
 * @param scale 二维码缩放比例
 */
void GernerateQRCode(const QString &text, QPixmap &qrPixmap, int scale)
{
       if(text.isEmpty())
    {
        return;
    }

    //二维码数据
    QRcode *qrCode = nullptr;

    //这里二维码版本传入参数是2,实际上二维码生成后,它的版本是根据二维码内容来决定的
    qrCode = QRcode_encodeString(text.toStdString().c_str(), 2,
                                 QR_ECLEVEL_Q, QR_MODE_8, 1);

    if(nullptr == qrCode)
    {
        return;
    }

    int qrCode_Width = qrCode->width > 0 ? qrCode->width : 1;
    int width = scale * qrCode_Width + 40;
    int height = scale * qrCode_Width + 40;

    QImage image(width, height, QImage::Format_ARGB32_Premultiplied);

    QPainter painter(&image);
    QColor background(Qt::white);
    painter.setBrush(background);
    painter.drawRect(0, 0, width, height);
    QColor foreground(Qt::black);
    painter.setBrush(foreground);
    for(int y = 0; y < qrCode_Width; ++y)
    {
        for(int x = 0; x < qrCode_Width; ++x)
        {
            unsigned char character = qrCode->data[y * qrCode_Width + x];
            if(character & 0x01)
            {
               QRect rect(x * scale +20, y * scale +20, scale, scale);
               painter.drawRects(&rect, 1);
            }
        }
    }
    painter.setBrush(foreground);
    painter.setFont(QFont("Arial",8));
    painter.drawText(12,15,"**智控科技");
    painter.setFont(QFont("Arial",5));
    painter.drawText(3,qrCode_Width+55,text);
    qrPixmap = QPixmap::fromImage(image);
    QRcode_free(qrCode);
}

调用

void slot_GenerateQRCode()
{
   QPixmap qrPixmap;
   GernerateQRCode(text, qrPixmap, 2);
   qrPixmap = qrPixmap.scaled(QSize(qrPixmap.width(), qrPixmap.height()),
                               Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
//     qrPixmap.save("./2021.png");
    ui->label_ShowQRCode->setPixmap(qrPixmap);
}

效果图

标签:qrPixmap,qrencode,Windows,Width,二维码,scale,qrCode,painter
来源: https://www.cnblogs.com/BruceMao/p/15424222.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有