创建VC2010工程,静态库类型。
添加qrencode-3.0.3目录下所有的.c、.h文件到工程中,移除qrenc.c文件。
注释掉文件rscode.c中的modnn函数的inline前缀。
编译,生成QrEncode.lib。
创建一个QWidget工程;
关联lib库和连接,头文件;
#include <QtGui/QWidget>
#include "ui_app_qrencode.h"
#include "../../qrencode.h"
class app_qrencode : public QWidget
{
Q_OBJECT
public:
explicit app_qrencode(QWidget *parent = 0);
~app_qrencode();
void setString(QString str);
int getQRWidth() const;
bool saveImage(QString name, int size);
private:
void draw(QPainter &painter, int width, int height);
QString string;
QRcode *qr;
public slots:
void setImage();
protected:
void paintEvent(QPaintEvent *);
QSize sizeHint() const;
QSize minimumSizeHint() const;
private:
Ui::app_qrencodeClass ui;
};
具体实现:
#include "app_qrencode.h"
#include <QPainter>
#include <QImage>
app_qrencode::app_qrencode(QWidget *parent) : QWidget(parent)
{
ui.setupUi(this);
qr = NULL;
/* setString("Hello QR Code"); */
connect(ui.pushButton,SIGNAL(clicked()),this,SLOT(setImage()));
}
app_qrencode::~app_qrencode()
{
if(saveImage("123.png",50))
{
return;
}
if(qr != NULL)
{
QRcode_free(qr);
}
}
int app_qrencode::getQRWidth() const
{
if(qr != NULL)
{
return qr->width;
}
else
{
return 0;
}
}
void app_qrencode::setString(QString str)
{
string = str;
if(qr != NULL)
{
QRcode_free(qr);
}
qr = QRcode_encodeString(string.toStdString().c_str(),
1,
QR_ECLEVEL_L,
QR_MODE_8,
1);
update();
}
QSize app_qrencode::sizeHint() const
{
QSize s;
if(qr != NULL)
{
int qr_width = qr->width > 0 ? qr->width : 1;
s = QSize(qr_width * 4, qr_width * 4);
}
else
{
s = QSize(50, 50);
}
return s;
}
QSize app_qrencode::minimumSizeHint() const
{
QSize s;
if(qr != NULL)
{
int qr_width = qr->width > 0 ? qr->width : 1;
s = QSize(qr_width, qr_width);
}
else
{
s = QSize(50, 50);
}
return s;
}
bool app_qrencode::saveImage(QString fileName, int size)
{
if(size != 0 && !fileName.isEmpty())
{
QImage image(size, size, QImage::Format_Mono);
QPainter painter(&image);
QColor background(Qt::white);
painter.setBrush(background);
painter.setPen(Qt::NoPen);
painter.drawRect(0, 0, size, size);
if(qr != NULL)
{
draw(painter, size, size);
}
return image.save(fileName);
}
else
{
return false;
}
}
void app_qrencode::draw(QPainter &painter, int width, int height)
{
QColor foreground(Qt::black);
painter.setBrush(foreground);
const int qr_width = qr->width > 0 ? qr->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 = qr->data[y * qr_width + x];
if(b & 0x01)
{
QRectF r(x * scale_x, y * scale_y, scale_x, scale_y);
painter.drawRects(&r, 1);
}
}
}
}
void app_qrencode::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QColor background(Qt::white);
painter.setBrush(background);
painter.setPen(Qt::NoPen);
painter.drawRect(0, 0, width(), height());
if(qr != NULL)
{
draw(painter, width(), height());
}
}
void app_qrencode::setImage()
{
QString str = ui.lineEdit->text();
ui.label->hide();
ui.pushButton->hide();
ui.lineEdit->hide();
if (str.isEmpty())
{
return;
}
setString(str);
}
输入百度。扫描后就能打开百度网页!www.baidu.com。关闭程序会在目录下生成123.png图片。