C++生成二维码和扫瞄二维码
本次使用Qt实现生成二维码和扫瞄二维码
完整代码参考git: https://github.com/lzglocal/QRCODE.git
一. 创建工程项目
-
选择QT Widgets Application
-
类名和选择基类,我这里选择默认的QMainwindow
-
创建完工程后,双击qrcode_app.ui文件,编辑ui
添加三个PushButton 命名分别为generator_qrcode_pbtn、copy_pic_pbtn、qrcode_scan_pbtn
一个Label
一个TextLine
-
自己自由布局
-
添加qrc资源文件,和添加应用程序图标
二 .QRCODE库
-
生成二维码使用了nayuki 的QR Code码相关的cpp库:git地址:https://github.com/nayuki/QR-Code-generator.git
-
扫码二维码使用了qzxing的git库:git地址: https://github.com/ftylitak/qzxing.git
三. 添加生成二维码库
- git clone https://github.com/nayuki/QR-Code-generator.git
四. 生成二维码和复制二维码实现
参考QrCodeGeneratorDemo.cpp
-
Ui文件pushbutton三个按钮转到槽函数
-
编辑头文件qrcode_app.h
#ifndef QRCODE_APP_H
#define QRCODE_APP_H
#include <QMainWindow>
//#include <climits>
//#include <cstdint>
//#include <cstdlib>
#include <cstring>
//#include <iostream>
//#include <sstream>
//#include <string>
//#include <vector>
#include "qrcodegen.h"
#include <QImage>
#include <QClipboard>
#include <QFileDialog>
//#include <QDebug>
using std::uint8_t;
using qrcodegen::QrCode;
using qrcodegen::QrSegment;
QT_BEGIN_NAMESPACE
namespace Ui {
class Qrcode_app; }
QT_END_NAMESPACE
class Qrcode_app : public QMainWindow
{
Q_OBJECT
public:
Qrcode_app(QWidget *parent = nullptr);
~Qrcode_app();
private slots:
//点击生成二维码槽函数
void on_generator_qrcode_pbtn_clicked();
//点击复制二维码
void on_copy_pic_pbtn_clicked();
//点击选择二维码扫码
void on_qrcode_scan_pbtn_clicked();
private:
Ui::Qrcode_app *ui;
// QString toSvgString(const QrCode &qr, int border);
// void printQr(const QrCode &qr);
//生成二维码和复制二维码功能函数
void doBasicDemo(bool is_copy=false);
// void doVarietyDemo();
// void doSegmentDemo();
// void doMaskDemo();
};
#endif // QRCODE_APP_H
- 编辑源文件qrcode_app.cpp
#include "qrcode_app.h"
#include "ui_qrcode_app.h"
// Function prototypes
//static void doBasicDemo();
#include <QProcess>
#include <QDesktopServices>
#include <QMessageBox>
Qrcode_app::Qrcode_app(QWidget