本帖最后由 stm1024 于 2023-12-4 22:17 编辑  
 
二维码基本信息 QR Code码,是由Denso公司于1994年9月研制的一种矩阵二维码符号,它具有一维条码及其它二维条码所具有的信息容量大、可靠性高、可表示汉字及图象多种文字信息、保密防伪性强等优点。 1、符号规格从版本1(21×21模块)到版本40(177×177 模块),每提高一个版本,每边增加4个模块。 2、数据类型与容量(参照最大规格符号版本40-L级):  
- 数字数据:7,089个字符
 - 字母数据: 4,296个字符
 - 8位字节数据: 2,953个字符
 - 汉字数据:1,817个字符
 
 
 
  
 
 3、数据表示方法: 深色模块表二进制"1",浅色模块表示二进制"0"。  
 4、纠错能力:  
- L级:约可纠错7%的数据码字
 - M级:约可纠错15%的数据码字
 - Q级:约可纠错25%的数据码字
 - H级:约可纠错30%的数据码字
 
 
 
  
5、结构链接(可选) 可用1-16个QR Code码符号表示一组信息。每一符号表示100个字符的信息。  
嵌入式Linux+Qt环境下使用QRCode Github上有很多关于QRCode生成的库,比较好用的是这个: https://github.com/nayuki/QR-Code-generator 提供了QR Code码的生成库,包含了C、C++、Java、JavaScript 、Python 、Rust 等语言的版本,代码简洁清晰,无需额外的配置,而且隐藏了很多二维码的技术细节,使用方便快捷。  
 下载以后软件包中内容如下: 
 
 
由于我们使用的是QT,因此,需要使用cpp文件夹下面的版本,该文件夹打开之后可以看到有五个文件:  
 
 
实际上需要用的是第二个和第三个,亦即qrcodegen的cpp源文件和hpp头文件。另外一个名为QrCodeGeneratorDemo.cpp的文件,实际上是一个示例测试程序,告诉你该怎么使用这个类库,有没有它都不影响程序的功能。 打开QTCreator,新建一个QWidget的窗口程序,然后将前面提到的qrcodegen.cpp和qrcodegen.hpp文件放入该项目下,并在QTCreator中加入对该文件的引用,最后项目文件夹和项目组织如下: 
 
界面设计: 
 
三个控件,分别是一个pushButton,一个lineEdit和一个Label。 在widget.h中加入必要的头文件引用,命名空间, 以及函数声明等: - #ifndef WIDGET_H
 
 - #define WIDGET_H
 
  
- #include <QWidget>
 
 - #include <QPainter>
 
 - #include <QDebug>
 
 - #include "qrcodegen.hpp"
 
  
- using namespace qrcodegen;
 
  
- QT_BEGIN_NAMESPACE
 
 - namespace Ui { class Widget; }
 
 - QT_END_NAMESPACE
 
  
- class Widget : public QWidget
 
 - {
 
 -     Q_OBJECT
 
  
- public:
 
 -     Widget(QWidget *parent = nullptr);
 
 -     ~Widget();
 
  
- private slots:
 
 -     void on_pb_gen_clicked();
 
  
- private:
 
 -     Ui::Widget *ui;
 
  
-     /*
 
 -      * draw image by with the qrCode class
 
 -      * qr: QrCode class instance with the encoded data
 
 -      * blocksize: the block size of the white/black module
 
 -      * margin:the margin of the QRCode data to the image border
 
 -      * return: the QImage the represent the QRCode
 
 -      */
 
 -     QImage drawImage(QrCode qr,int blockSize=3,int margin=0);
 
 - };
 
 - #endif // WIDGET_H
 
 
  复制代码其中槽函数void on_pb_gen_clicked()是一会自动生成的。 代码中主要是添加了一个名为drawImage的函数,它是将QRCode的编码值,转化为我们通常看到的黑白QRCode图片形式,以下是widget.cpp中的实现: - <font size="2">QImage Widget::drawImage(QrCode qr,int blockSize,int margin)
 
 - {
 
 -     QImage img;
 
 -     if(blockSize<1)
 
 -         blockSize=1;
 
 -     if(margin<0)
 
 -         margin=0;
 
 -     img=QImage(qr.getSize()*blockSize+2*margin,qr.getSize()*blockSize+2*margin,QImage::Format_RGB888);
 
 -     QPainter painter(&img);
 
 -     //draw white background
 
 -     painter.setPen(Qt::NoPen);
 
 -     painter.setBrush(QColor(Qt::white));
 
 -     QRect rect = QRect(0,0,img.width(),img.height());
 
 -     painter.drawRect(rect);
 
 -     //prepare the module block(only need to draw modules with '1' bit)
 
 -     painter.setBrush(QColor(Qt::black));
 
 -     QRect rectDraw=QRect(0,0,blockSize,blockSize);
 
  
-     for (int y = 0; y < qr.getSize(); y++)
 
 -     {
 
 -         for (int x = 0; x < qr.getSize(); x++)
 
 -         {
 
 -             if(qr.getModule(x, y))
 
 -             {
 
 -                 rectDraw.moveLeft(margin+x*blockSize);
 
 -                 rectDraw.moveTop(margin+y*blockSize);
 
 -                 painter.drawRect(rectDraw);
 
 -             }
 
 -         }
 
 -     }
 
 -     return img;
 
 - }
 
 - </font>
 
  复制代码这个函数的作用就是使用QrCode类的实例,绘制其对应的二维码图形,第一个参数就是带有QR编码的实例,第二个参数是图片所使用色块大小,也就是表示module的小方块,而第三个参数则是图片的边框宽度。 生成图片以后,显示很简单,总的思路就是从lineEdit中获取字符串,调用drawImage生成图片,然后用QLabel显示图片,pushButton的槽函数如下: - void Widget::on_pb_gen_clicked()
 
 - {
 
 -     if(ui->le_input->text()=="")
 
 -         return;
 
 -     const QrCode qr = QrCode::encodeText(ui->le_input->text().toStdString().c_str(),QrCode::Ecc::MEDIUM);
 
 -     qDebug()<<"QRCode Version ="<<qr.getVersion()
 
 -             <<"QRCode Size ="<<qr.getSize()
 
 -             <<"QRCode Mask ="<<qr.getMask();
 
 -     drawImage(qr,1);    ui->lb_qrImg->setPixmap(QPixmap::fromImage(drawImage(qr,4,1)));
 
 - }
 
  复制代码然后再虚拟机环境中编译,最后下载到ELF-1开发板上,修改权限并运行: 
 可以看到,在控制台中还会显示QRCode的相关信息。 
 
 
 
 |