Changeset View
Changeset View
Standalone View
Standalone View
src/lib/qrcodebarcode.cpp
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | SPDX-FileCopyrightText: 2010-2016 Sune Vuorela <sune@vuorela.dk> | 2 | SPDX-FileCopyrightText: 2010-2016 Sune Vuorela <sune@vuorela.dk> | ||
3 | 3 | | |||
4 | SPDX-License-Identifier: MIT | 4 | SPDX-License-Identifier: MIT | ||
5 | */ | 5 | */ | ||
6 | 6 | | |||
7 | #include "qrcodebarcode.h" | 7 | #include "qrcodebarcode.h" | ||
8 | #include <qrencode.h> | 8 | #include <qrencode.h> | ||
9 | #include <QColor> | 9 | #include <QColor> | ||
10 | 10 | | |||
11 | using namespace Prison; | 11 | using namespace Prison; | ||
12 | 12 | | |||
13 | QRCodeBarcode::QRCodeBarcode() : AbstractBarcode(AbstractBarcode::TwoDimensions) {} | 13 | QRCodeBarcode::QRCodeBarcode() : AbstractBarcode(AbstractBarcode::TwoDimensions) {} | ||
14 | QRCodeBarcode::~QRCodeBarcode() = default; | 14 | QRCodeBarcode::~QRCodeBarcode() = default; | ||
15 | 15 | | |||
16 | QImage QRCodeBarcode::paintImage(const QSizeF& size) { | 16 | QImage QRCodeBarcode::paintImage(const QSizeF& size) { | ||
17 | const int width = qRound(qMin(size.width(),size.height())); | 17 | Q_UNUSED(size); | ||
18 | if(data().size()==0 || width==0) { | | |||
19 | return QImage(); | | |||
20 | } | | |||
21 | const QByteArray trimmedData(data().trimmed().toUtf8()); | 18 | const QByteArray trimmedData(data().trimmed().toUtf8()); | ||
22 | QRcode* code = QRcode_encodeString8bit(trimmedData.constData(), 0, QR_ECLEVEL_Q); | 19 | QRcode* code = QRcode_encodeString8bit(trimmedData.constData(), 0, QR_ECLEVEL_Q); | ||
23 | if(!code) { | 20 | if(!code) { | ||
24 | return QImage(); | 21 | return QImage(); | ||
25 | } | 22 | } | ||
26 | const int margin = 2; | 23 | const int margin = 2; | ||
27 | /*32 bit colors, 8 bit pr byte*/ | 24 | /*32 bit colors, 8 bit pr byte*/ | ||
28 | uchar* img = new uchar[4 *sizeof(char*)*(2*margin + code->width)*(2*margin* + code->width)]; | 25 | uchar* img = new uchar[4 *sizeof(char*)*(2*margin + code->width)*(2*margin* + code->width)]; | ||
Show All 30 Lines | 54 | } else { | |||
59 | for(int i =0 ; i<4 ; i++) { | 56 | for(int i =0 ; i<4 ; i++) { | ||
60 | *p = background[i]; | 57 | *p = background[i]; | ||
61 | p++; | 58 | p++; | ||
62 | } | 59 | } | ||
63 | } | 60 | } | ||
64 | } | 61 | } | ||
65 | } | 62 | } | ||
66 | } | 63 | } | ||
67 | QImage tmp(img,code->width+2*margin,code->width+2*margin,QImage::Format_ARGB32); | 64 | | ||
68 | setMinimumSize(QSizeF(tmp.width()*4,tmp.height()*4)); | 65 | const auto result = QImage(img, code->width+2*margin, code->width+2*margin, QImage::Format_ARGB32).copy(); // deep copy as we are going to delete img | ||
69 | QImage ret = tmp.convertToFormat(QImage::Format_ARGB32).scaled(qMax(tmp.width()*4,width),qMax(tmp.height()*4,width)); //4 is determined by trial and error. | 66 | setMinimumSize(QSizeF(result.width(), result.height())); | ||
70 | delete[] img; | 67 | delete[] img; | ||
71 | QRcode_free(code); | 68 | QRcode_free(code); | ||
72 | return ret; | 69 | return result; | ||
73 | } | 70 | } |