当前位置:网站首页>QT method for generating QR code pictures
QT method for generating QR code pictures
2022-06-11 04:32:00 【Y-J-L】
QRCode Introduction to QR code
QR The code is Japan DENSO WAVE INCORPORATED The company in 60 An open two-dimensional code developed in the s , At present, the company has fully disclosed QR Code related standards , If you can't do it, just QR Code related patent rights , And vigorously promote QR Code is used in all walks of life , Even now QR Code has been widely used , Many international standardization organizations incorporate it into the standard . The following is an introduction to the use of in the development process QR Code may involve the main parameters :

The size of the QR code :21 Code element ×21 Code element ~177 Code element ×177 Code element ( On each side 4 The symbols are incremented in units ), The size of the QR code is determined by the version of the code ; The amount of information that a two-dimensional code can contain is related to the size of the code .
QR Code version :QR Code setting 1 To 40 Different versions of ( species ), Each version has an inherent symbol structure ( Number of symbols ).( The symbol refers to the composition of QR The square black and white dot of the size .)“ Symbol structure ” It refers to the number of symbols in the two-dimensional code . From version 1(21 Code element ×21 Code element ) Start , In the vertical and horizontal direction respectively with 4 The symbols are incremented in units , Up to the version 40(177 Code element ×177 Code element ).

QR Code error correction level :QR The code has “ Error correction function ”. Even if the code gets dirty or broken , It can also recover data automatically . this “ Error correction ability ” Have 4 A level : Level L、 Level M、 Level Q、 Level H, Users can choose the appropriate level according to the use environment .
How to select QR Code version ?
How to choose the right QR Code or view QR The specific parameters and introduction of the code can be referred to QR Its official website :QRcode.com|DENSO WAVE
The main method
This article mainly explains the use of open source QR code Library in Qt Generate two-dimensional code in the program , We mainly use an open source QR Encoding Library qrencode. We can qrencode Submit to the project for use by adding an external library , Can also be qrencode Add the source code to the project to compile and use , We use the second method , By way of qrencode The source code of is transplanted to our Qt Program to achieve two-dimensional code display .
step
(1) download qrencode Source code
We went to the OpenPKG Project: Download download qrencode Open source program for , What I'm doing is qrencode-4.1.1.tar.gz

qrencode Download at :OpenPKG Project: Download
(2) take qrencode Transplant the source code to the project
1) Create a new one Qt engineering , take qrencode-4.1.1.tar.gz Unzip all the files in the root directory .c and .h File copy to Qt In Engineering . As shown in the figure :

2) take qrencode In the source config.h.in Document modified to config.h Join the project , Will just be added to Qt In the project qrenc.c Move the file out of the project , Because this file is the main function of the source code , And Qt Conflicts in , Will cause the program to exit abnormally . After adding, the project looks like this :

(3) Modify migration file
1) stay QT Of .pro Add global... To the file Macro definition
DEFINES += HAVE_CONFIG_H2) stay config.h The document ends with a new definition of macro
#define MAJOR_VERSION 1
#define MICRO_VERSION 1
#define MINOR_VERSION 1
#define VERSION 1(4) Use
I created an inheritance here QWidget Of Widget main window , stay widget.h Add the following two sentences to the function declaration
void GenerateQRcode(QString tempstr, QLabel *label);
void GenerateQRcode(QString tempstr, QLabel *label, const QString &logo, float scale);stay widget.c The following program has been added to , These two programs are overloaded functions , It provides an interface for generating and drawing two-dimensional code pictures , The first function is to generate the QR code separately , The second function can add pictures in the middle of the QR code .
/*trmpst: The information contained in the QR code
* label: Display QR code QLabel Control
* */
void Widget::GenerateQRcode(QString tempstr, QLabel *label)
{
QRcode *qrcode; // QR code data
// take QString Turn into const char * |2-QR Code version is 2 | QR_ECLEVEL_Q Fault tolerance level |QR_MODE_8 Octet data |1- Case sensitive
qrcode = QRcode_encodeString(tempstr.toStdString().c_str(), 2, QR_ECLEVEL_Q, QR_MODE_8, 1);
qint32 temp_width=label->width(); // Used to display the QR code QLabel size , It is also the size of the QR code image displayed later
qint32 temp_height=label->height();
qint32 qrcode_width = qrcode->width > 0 ? qrcode->width : 1; // Generated QR code width and height ( The width of the square = Height )
double scale_x = (double)temp_width / (double)qrcode_width; // Scale of QR code picture
double scale_y =(double) temp_height /(double) qrcode_width;
QImage mainimg=QImage(temp_width,temp_height,QImage::Format_ARGB32);
QPainter painter(&mainimg);
QColor background(Qt::white);
painter.setBrush(background);
painter.setPen(Qt::NoPen);
painter.drawRect(0, 0, temp_width, temp_height);
QColor foreground(Qt::black);
painter.setBrush(foreground);
for( qint32 y = 0; y < qrcode_width; y ++)//qrcode->data Is a saved qrcode_width*qrcode_width A one-dimensional array of data
{ // Here is to divide this one-dimensional array into each row qrcode_width Data , In the form of a two-dimensional array
for(qint32 x = 0; x < qrcode_width; x++)
{
unsigned char b = qrcode->data[y * qrcode_width + x];
if(b & 0x01)
{// According to the black and white dots in the QR code (1/0), stay QLabel Draw a two-dimensional code with a zoom ratio on the
QRectF r(x * scale_x, y * scale_y, scale_x, scale_y);
painter.drawRects(&r, 1);
}
}
}
QPixmap mainmap=QPixmap::fromImage(mainimg);
label->setPixmap(mainmap);
label->setVisible(true);
}
/*trmpst: The information contained in the QR code
* label: Display QR code QLabel Control
* logo: The picture displayed in the middle of the QR code
* scale: Zoom ratio of the middle picture
* */
void Widget::GenerateQRcode(QString tempstr, QLabel *label, const QString &logo, float scale)
{
GenerateQRcode(tempstr, label);
int width = label->width();
int height = label->height();
int logo_width = width *scale;
int logo_height = height * scale;
int logo_x = (width - logo_width) / 2;
int logo_y = (width - logo_height) / 2;
const QPixmap *pix = label->pixmap();
QPixmap temppix(logo);
QPixmap pix1 = temppix.scaled(QSize(logo_width, logo_height), Qt::KeepAspectRatio);
QPixmap pix2(width, height);
QPainter *painter = new QPainter(&pix2);
QColor background(Qt::white);
painter->setBrush(background);
painter->setPen(Qt::NoPen);
painter->drawRect(0, 0, width, height);
QColor foreground(Qt::black);
painter->setBrush(foreground);
painter->drawPixmap(0,0, width, height, *pix);
painter->drawPixmap(logo_x,logo_y, logo_width, logo_height, pix1);
label->setPixmap(pix2);
delete painter;
}
Take the following procedure as an example
QLabel *lab1 = new QLabel(this);
lab1->setGeometry(10,100,200,200);
QLabel *lab2 = new QLabel(this);
lab2->setGeometry(300,100,200,200);
GenerateQRcode("123456789", lab1, ":Imag/jiasu.png",0.2);
GenerateQRcode("123456789", lab2);effect :

Reference resources :blog.csdn.net/kangshuaibing/article
边栏推荐
- Vulkan official example interpretation shadows (rasterization)
- Collation of construction data of Meizhou plant tissue culture laboratory
- 用万用表检测数码管
- Unity MonoSingleton
- Unity的URP的RenderFeature相关编程内容梳理
- 数据类型的转换和条件控制语句
- Carbon path first, Huawei digital energy injects new momentum into the green development of Guangxi
- [server data recovery] data recovery case of RAID5 crash of buddy storage
- JVM (7): dynamic link, method call, four method call instructions, distinguishing between non virtual methods and virtual methods, and the use of invokedynamic instructions
- MySQL stored procedure
猜你喜欢

精益产品开发体系最佳实践及原则

司马炎爷爷 告诉你什么叫做内卷!

Unity 可缩放地图的制作

PHP话费充值通道网站完整运营源码/全解密无授权/对接免签约支付接口

The future has come and the 5g advanced era has begun

L'avenir est venu, l'ère 5G - Advanced s'ouvre

JVM (1): introduction, structure, operation and lifecycle

Redis持久化(少年一贯快马扬帆,道阻且长不转弯)

PostgreSQL数据库复制——后台一等公民进程WalReceiver 收发逻辑

Project architecture evolution
随机推荐
把所有单词拆分成单个字词,删选适合公司得产品词库
MySQL锁总结
国际琪貨:做正大主帐户风险有那些
JVM(1):介绍、结构、运行和生命周期
数字电影的KDM是什么?
碳路先行,华为数字能源为广西绿色发展注入新动能
The third small class discussion on the fundamentals of information and communication
Guanghetong successfully won the bidding for China Unicom Yanfei CAT1 customized module with the largest share
Production of unity scalable map
Data type conversion and conditional control statements
Seven easy-to-use decorators
Unity 伤害值的显示
Analysis of zero time technology | discover lightning loan attack
Guanghetong "carbon" seeking green sharing 5g/lte module solution for two rounds of travel
福州口罩洁净厂房建设知识概述
司马炎爷爷 告诉你什么叫做内卷!
无刷电机调试经验与可靠性设计
Sql优化
Unity 消息框架 NotificationCenter
Unity 物品模型旋转展示