Qt 生成QR code
QImage qt_qrencode(QString text, QPixmap logo, int width, int height)
{
if (width == 0)
{
width = 300;
}
if (height == 0)
{
height = 300;
}
QImage image(QSize(width, height), QImage::Format_RGB32);
QRecLevel level = QR_ECLEVEL_Q;
QRencodeMode mode = QR_MODE_8;
bool casesen = true;
int margin = 10;
double percent = 0.23;
QColor foreground = QColor("black");
QColor background = QColor("white");
{
QPainter painter(&image);
QRcode *qrcode = QRcode_encodeString(text.toUtf8().data(), 7, (QRecLevel)level, (QRencodeMode)mode, casesen ? 1 : 0);
if (0 != qrcode) {
unsigned char *point = qrcode->data;
painter.setPen(Qt::NoPen);
painter.setBrush(background);
painter.drawRect(0, 0, width, height);
double scale = (width - 2.0 * margin) / qrcode->width;
painter.setBrush(foreground);
for (int y = 0; y < qrcode->width; y++) {
for (int x = 0; x < qrcode->width; x++) {
if (*point & 1) {
QRectF r(margin + x * scale, margin + y * scale, scale, scale);
painter.drawRects(&r, 1);
}
point++;
}
}
point = NULL;
QRcode_free(qrcode);
if (!logo.isNull())
{
painter.setBrush(background);
double icon_width = (width - 2.0 * margin) * percent;
double icon_height = icon_width;
double wrap_x = (width - icon_width) / 2.0;
double wrap_y = (width - icon_height) / 2.0;
QRectF wrap(wrap_x - 5, wrap_y - 5, icon_width + 10, icon_height + 10);
painter.drawRoundRect(wrap, 50, 50);
QRectF target(wrap_x, wrap_y, icon_width, icon_height);
QRectF source(0, 0, logo.width(), logo.height());
painter.drawPixmap(target, logo, source);
}
}
qrcode = NULL;
}
return image;
}