使用QT做一个小项目,需要使用到二维码功能。。网上搜了一些资料找到了 开源的qrencode。
下载地址:
https://fukuchi.org/works/qrencode/
我自己的:
https://pan.baidu.com/s/1TyDEa1gLrFoncTW_Z9fo6w
参考网上的教程,把代码下载下来,只保留.c和.h文件,删除qrencode.c文件,因为这是一个测试程序,里面已经包含了main函数。
新建一个QT工程,在工程目录下新建一个文件夹,把代码拷贝进去,然后再将所有的代码包含进来。
然后就可以在代码中调用了。
#include "qrencode.h"
QString strRocde = "hello";
QRcode *m_qrcode = QRcode_encodeString(strRocde.toStdString().c_str(),
1,
QR_ECLEVEL_H,
QR_MODE_8,
1);
//这是绘制函数
void LogWidget::draw(QPainter &painter, int width, int height)
{
QColor foreground(Qt::black);
painter.setBrush(foreground);
const int qr_width = m_qrcode->width > 0 ? m_qrcode->width : 1;
double scale_x = width / qr_width;
double scale_y = height / qr_width;
for( int y = 0; y < qr_width; y ++)
{
for(int x = 0; x < qr_width; x++)
{
unsigned char b = m_qrcode->data[y * qr_width + x];
if(b & 0x01)
{
QRectF r(x * scale_x, y * scale_y, scale_x, scale_y);
painter.drawRects(&r, 1);
}
}
}
if(1) //添加Logo的图片,绘制在二维码的中间bhavelogo
{
QPixmap picture(":/image/image/logoRitac.png");
int logoWidth = width*0.28;
int logoHeight = height * 0.28;
painter.drawPixmap(width/2-logoWidth/2,height/2-logoHeight/2,logoWidth,logoHeight,picture);
}
}
//重载的函数
void LogWidget::paintEvent(QEvent *e)
{
QPainter painter(w);
QColor background(Qt::white);
painter.setBrush(background);
painter.setPen(Qt::NoPen);
int paintWidth = int(w->width()/m_qrcode->width)*m_qrcode->width ;
int paintHeight = int(w->height()/m_qrcode->width)*m_qrcode->width;
painter.drawRect(0, 0, paintWidth, paintHeight);
if(m_qrcode != nullptr)
{
draw(painter, paintWidth,paintHeight);
}
}
在debug模式就可以正常使用了。。
但是坑爹的是,在release模式下编译的时候一大堆错误。晕死。
尝试了各种方法,都是无法解决。。最后试着用gcc编程库文件调用试试,发现是OK的。
编译方法:
d:\Qt\Qt5.9.7\Tools\mingw530_32\bin\mingw32-gcc.exe *.c -D HAVE_CONFIG_H -shared -o qrencode402.dll -Wl,--out-implib,qrencode402.lib
生成的文件
没有debug版本的,拷贝一个修改下名字就行了。。。以后移植到linux上,重新编译下库就行了。
然后包含外部库文件就行了。