如何在本机C / C ++中使用Qt绘制QR码 [英] How to draw a QR code with Qt in native C/C++
问题描述
Qt中的QR
作为如何使用Qt扫描QR码,我想知道如何绘制 QR代码,但是我找不到如何执行此操作的示例。
As a companion question to How to scan for QR codes with Qt, I want to know how to draw a QR code from native C/C++ code in my Qt5 based desktop app, but I could not find an example of how to do this.
我知道 QtQR 存在,但它依赖于 python-qrtools 意见首先打败了使用Qt的目的。我想要一个灵活,高效且无依赖的解决方案,无论我决定将它带到哪里,都可以与我的应用程序一起编译。
I know QtQR exists, but it has dependencies on python-qrtools which in my opinion kind of defeats the purpose of using Qt in the first place. I want a nimble, efficient and dependency-free solution that will compile with my app wherever I decided to take it.
我该怎么做?
推荐答案
如果您觉得Fukuchi的库太大 [0] ,请考虑使用Nayuki的C ++ QR Code生成器库 [1] : https:// github .com / nayuki / QR-Code-generator / tree / master / cpp
If you feel that Fukuchi's library is too large[0] for you, consider looking at Nayuki's C++ QR Code generator library[1]: https://github.com/nayuki/QR-Code-generator/tree/master/cpp
Nayuki的库需要C ++ 11,并且可移植而无需Autotools。用法示例:
Nayuki's library requires C++11, and is portable without needing Autotools. Sample usage:
#include <string>
#include <vector>
#include "QrCode.hpp"
using namespace qrcodegen;
// Create the QR Code object
QrCode qr = QrCode::encodeText("Hello, world!", QrCode::Ecc::MEDIUM);
// Read the black & white pixels
for (int y = 0; y < qr.size; y++) {
for (int x = 0; x < qr.size; x++) {
int color = qr.getModule(x, y); // 0 for white, 1 for black
// You need to modify this part
draw_pixel_onto_QT(x, y, color);
}
}
[0]:福知:20个文件,在.c和.h主文件中约7200行(不包括构建和测试代码)。
[1]:Nayuki:6个文件,在主文件中约1400行.cpp和.hpp文件(不包括演示代码)。
编辑2016-12- OP制作的08版
我决定在获得许可的情况下将自己的改编内容添加到Qt中。这段代码可以在我的系统上编译并正常运行,而且我认为它应该足够独立以在其他地方工作,而无需进行太多调整。
EDIT 2016-12-08 by OP I decided, with permission, to add my own adaption to Qt. This code compiles and runs fine on my system, And I think it should be independent enough to work elsewhere without too many tweaks as well.
#include "QrCode.hpp"
void paintQR(QPainter &painter, const QSize sz, const QString &data, QColor fg)
{
// NOTE: At this point you will use the API to get the encoding and format you want, instead of my hardcoded stuff:
qrcodegen::QrCode qr = qrcodegen::QrCode::encodeText(data.toUtf8().constData(), qrcodegen::QrCode::Ecc::LOW);
const int s=qr.getSize()>0?qr.getSize():1;
const double w=sz.width();
const double h=sz.height();
const double aspect=w/h;
const double size=((aspect>1.0)?h:w);
const double scale=size/(s+2);
// NOTE: For performance reasons my implementation only draws the foreground parts in supplied color.
// It expects background to be prepared already (in white or whatever is preferred).
painter.setPen(Qt::NoPen);
painter.setBrush(fg);
for(int y=0; y<s; y++) {
for(int x=0; x<s; x++) {
const int color=qr.getModule(x, y); // 0 for white, 1 for black
if(0!=color) {
const double rx1=(x+1)*scale, ry1=(y+1)*scale;
QRectF r(rx1, ry1, scale, scale);
painter.drawRects(&r,1);
}
}
}
}
用法,请参见此 painter 类
这篇关于如何在本机C / C ++中使用Qt绘制QR码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!