1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| const char* url = "http://www.baidu.com"; QRcode* qrcode = QRcode_encodeString(url, 2, QR_ECLEVEL_Q, QR_MODE_8, 0); qint32 temp_width = 400; qint32 temp_height = 400; qint32 qrcode_width = qMax(qrcode->width, 1); double scale_x = (double)temp_width / (double)qrcode_width; double scale_y = (double)temp_height / (double)qrcode_width; QImage img = QImage(temp_width, temp_height, QImage::Format_ARGB32);
QPainter painter(&img); painter.setBrush(Qt::white); painter.setPen(Qt::NoPen); painter.drawRect(0, 0, temp_width, temp_height);
painter.setBrush(Qt::black); for( qint32 y = 0; y < qrcode_width; y ++) { for(qint32 x = 0; x < qrcode_width; x++) { unsigned char b = qrcode->data[y * qrcode_width + x]; if(b & 0x01) { QRectF r(x * scale_x, y * scale_y, scale_x, scale_y); painter.drawRects(&r, 1); } } }
QPixmap pixmap = QPixmap::fromImage(img); ui->qrcodeLabel->setPixmap(pixmap); delete qrcode;
|