当前位置:网站首页>QT | string generation QR code function

QT | string generation QR code function

2022-07-24 07:51:00 InfoQ

Recent knowledge points encountered in sorting out development projects , Found a particularly interesting feature : Use string to generate QR code .

null

Function realization

development environment :WIN10 Environmental Science  + VS2017 + Qt 5.14.2 64 Bit development environment

Resource package data

To be honest, my development environment is really troublesome ~

Want to use Qt Use QR code function , You must add  "qrencode" library . For my current environment cmake Compiled a dll library

null
The compiled file contains two :lib and src, If it is inconsistent with my development environment , Self compiling , It's also very fast ( I find camke What a good tool ~)

Configuration properties

1: Header file settings

null

2: Static library settings

null
null
explain : Set according to the red area in the above three pictures .

You can find that when I set it, there is a common point , All use relative paths .

Generally, many people are working on large-scale projects , Will adopt relative path , Even if everyone puts the project on a different disk , There will be no data reading problem .

So that's it , Then let me move on to some digressions about configuration ~ I hope it will help you !

Digression : In that case , We generated exe It will also be put into a separate directory , Suppose we call it bin file

null
stay VS By default exe It will be based on debug perhaps release The environment is generated into the corresponding folder . Currently, to merge the two environments, you need to modify the general operations in the configuration properties

null
Last , It also needs to be set in the code , take exe Set the generation directory of to the current path , Otherwise, when relative paths are used in the program , Unable to find the corresponding file !

QString qExePath = QCoreApplication::applicationDirPath();
QDir::setCurrent(qExePath);

These two sentences had better be put in main.cpp in , Make the whole project effective .

QR code operation

Next is our play , How to use string to generate QR code

1: establish QR Operation classes are used to draw QImage graphics

Defining classes :QORCodeOperation

#include <QPainter>
#include <xstring>
class CQRCodeOperation
{
public:
 CQRCodeOperation();
 ~CQRCodeOperation();
 QImage GeneratedGraphics(std::string sData, QSize nsize); // Generate graphics
private:
 QPixmap m_imgIcon;
};

function (GeneratedGraphics)

Pass in the specified string and the width and height of the QR code , Returns the of the graph QImage value .

In general, we will QIamge Assign values to the QLable On display .

Parameters (m_imgIcon)

QPximap A member variable of type , Mainly drawing graphics , At the beginning of the display effect, you can see that a picture is added in the middle of the QR code , Mainly used to display pictures .

2: Generate QR code graphics QImage

2.1: Definition QImage Objects store generated content
QImage image(nsize, QImage::Format_RGB32);
image.fill(QColor(&quot;#000000&quot;));

QImage Store according to the settings nsize The width and height of the size construct an image , The program will automatically align the data according to the image format , Use 32 position RGB Format image (0xffrrggbb)
2.2: Construct drawing pointer
QPainter painter(&image);
if (!painter.isActive())
{
 return image;
}

structure QPainter Draw pointer , Generally speaking, when it comes in QSize The data is 0 when , It is impossible to carry out subsequent operations , That is to say isActiva = false
2.3: Get... From string QRcode Class instance
QRcode *qrCode = QRcode_encodeString(sData.c_str(), 1, QR_ECLEVEL_L, QR_MODE_8, 1);
2.4: Set point brush and background brush
QColor colorForPoint(&quot;#FFB6C1&quot;);
QColor colorForBackground(&quot;#ffffff&quot;);

painter.setBrush(colorForBackground);
painter.setPen(Qt::NoPen);
painter.drawRect(0, 0, image.width(), image.height());

painter.setBrush(colorForPoint);
2.5: Drawing graphics
const double &&s = (qrCode->width > 0) ? (qrCode->width) : (1);
const double &&aspect = image.width() / image.height();
const double &&scale = ((aspect > 1.0) ? image.height() : image.width()) / s;

for (int y = 0; y < s; ++y)
{
 const int &&yy = static_cast<int>(y * s);
 for (int x = 0; x < s; ++x)
 {
 const int &&xx = yy + x;
 const unsigned char &b = qrCode->data[xx];

 if (b & 0x01)
 {
 const double rx1 = x * scale, ry1 = y * scale;
 QRectF r(rx1, ry1, scale, scale);
 painter.drawRects(&r, 1);
 }
 }
}

Here, in order to be lazy, the ternary operator is used to judge the width value .
2.6: Release QRcode The pointer
QRcode_free(qrCode);
2.7: Add QR code graphics
In fact, the picture we show in the middle is relatively small , Just cover it directly on the QR code

painter.setRenderHint(QPainter::Antialiasing, true); // Anti-Aliasing
int nLeft = (nsize.width() - 30) / 2;
int nTop = (nsize.height() - 30) / 2;
QRect rectPng(nLeft, nTop,30,30);
painter.drawPixmap(rectPng, m_imgIcon);
2.8: Finish drawing
painter.end();

So far, the specific QR code drawing has been completed , Externally, we only need to return the data that stores the drawing QImage That's all right. .

3: QR code function call

CQRCodeOperation dlg;
QImage img = dlg.GeneratedGraphics(sText, QSize(250, 250));
ui.labPng->setPixmap(QPixmap::fromImage(img));

summary

Here, the function of string generation QR code is explained , It's not very difficult , As long as the environment is configured successfully, the general functions are completed , The rest is based on your idea , How do you want to show .

I am a good citizen of China , focus C++ Developing programs ~
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/205/202207240749474183.html