2015-03-07 20:40:45 +08:00
|
|
|
#include "qrwidget.h"
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QBuffer>
|
|
|
|
#include <QImage>
|
|
|
|
#include "qrcodewindow.h"
|
|
|
|
#include "qrencode.h"
|
|
|
|
|
|
|
|
QRWidget::QRWidget(QWidget *parent) : QWidget(parent), data("0")
|
2015-03-08 04:29:39 +08:00
|
|
|
//Note: The encoding fails with empty string so I just default to something else.
|
|
|
|
//Use the setQRData() call to change this.
|
2015-03-07 20:40:45 +08:00
|
|
|
{
|
|
|
|
//size of the qimage might be problematic in the future, but it works for me
|
|
|
|
size.setWidth(480);
|
|
|
|
size.setHeight(480);
|
|
|
|
image = new QImage(size, QImage::Format_RGB32);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QRWidget::setQRData(QString data)
|
|
|
|
{
|
2015-03-08 04:29:39 +08:00
|
|
|
this->data = "tox:" + data;
|
2015-03-07 20:40:45 +08:00
|
|
|
paintImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString QRWidget::getImageAsText()
|
|
|
|
{
|
|
|
|
paintImage();
|
|
|
|
QByteArray ba;
|
|
|
|
QBuffer buffer(&ba);
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
image->save(&buffer, "PNG"); // writes the image in PNG format inside the buffer
|
|
|
|
|
|
|
|
QString iconBase64 = QString::fromLatin1(ba.toBase64().data());
|
2015-03-08 04:29:39 +08:00
|
|
|
QString base64Image = "<img width=\"300\" heigth=\"300\" src=\"data:image/png;base64," + iconBase64 +"\" />";
|
|
|
|
qDebug() << base64Image;
|
2015-03-07 20:40:45 +08:00
|
|
|
|
|
|
|
return QString(base64Image);
|
|
|
|
}
|
|
|
|
|
|
|
|
//http://stackoverflow.com/questions/21400254/how-to-draw-a-qr-code-with-qt-in-native-c-c
|
|
|
|
void QRWidget::paintImage()
|
|
|
|
{
|
|
|
|
QPainter painter(image);
|
|
|
|
//NOTE: I have hardcoded some parameters here that would make more sense as variables.
|
2015-03-08 04:29:39 +08:00
|
|
|
// ECLEVEL_M is much faster recognizable by barcodescanner any any other type
|
|
|
|
QRcode *qr = QRcode_encodeString(data.toStdString().c_str(), 1, QR_ECLEVEL_M, QR_MODE_8, 0);
|
2015-03-07 20:40:45 +08:00
|
|
|
|
|
|
|
if(0 != qr)
|
|
|
|
{
|
|
|
|
QColor fg("black");
|
|
|
|
QColor bg("white");
|
|
|
|
painter.setBrush(bg);
|
|
|
|
painter.setPen(Qt::NoPen);
|
|
|
|
painter.drawRect(0, 0, size.width(), size.height());
|
|
|
|
painter.setBrush(fg);
|
2015-03-08 04:29:39 +08:00
|
|
|
const int s = qr->width > 0 ? qr->width : 1;
|
2015-03-07 20:40:45 +08:00
|
|
|
const double w = width();
|
|
|
|
const double h = height();
|
|
|
|
const double aspect = w / h;
|
|
|
|
const double scale = ((aspect > 1.0) ? h : w) / s;
|
|
|
|
|
|
|
|
for(int y = 0; y < s; y++)
|
|
|
|
{
|
|
|
|
const int yy = y * s;
|
|
|
|
for(int x = 0; x < s; x++)
|
|
|
|
{
|
|
|
|
const int xx = yy + x;
|
|
|
|
const unsigned char b = qr->data[xx];
|
|
|
|
if(b & 0x01)
|
|
|
|
{
|
|
|
|
const double rx1 = x * scale,
|
|
|
|
ry1 = y * scale;
|
|
|
|
QRectF r(rx1, ry1, scale, scale);
|
|
|
|
painter.drawRects(&r, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QRcode_free(qr);
|
|
|
|
painter.save();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QColor error("red");
|
|
|
|
painter.setBrush(error);
|
|
|
|
painter.drawRect(0, 0, width(), height());
|
|
|
|
qDebug() << "QR FAIL: " << strerror(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
qr = 0;
|
|
|
|
}
|