【问题标题】:Any ideas, how to encode QR data into png image?任何想法,如何将 QR 数据编码为 png 图像?
【发布时间】:2017-03-19 19:40:36
【问题描述】:

我所需要的只是简单的 (Q)string 将其作为嵌入图像:

<img src="data:image/png;base64,iVBORw...">

我使用:#include &lt;qrencode.h&gt; (linux -> apt-get install libqrencode-dev)

这是我的代码:

QRcode *qr=QRcode_encodeString(QString("my test string").toStdString().c_str(), 1, QR_ECLEVEL_L, QR_MODE_8,1);
QByteArray *ba = new QByteArray();
for (unsigned int y=0; y<qr->width;y++)
{
    int yy=y*qr->width;
    for (unsigned int x=0; x<qr->width;x++)
    {
      int xx=yy+x;
      const unsigned char b=qr->data[xx];

      ///WHAT TO DO NOW??? IS IT CORRECT?
      ba->push_back(b);
      qDebug()<<"Char "<<b;   
      if(b &0x01)
      {
        qDebug()<<"Point +++";  
      }
    }
}

qDebug()<<ba->toBase64();

任何想法,如何将qr-&gt;data 编码为 png 图像?

【问题讨论】:

    标签: qt base64 png embed qr-code


    【解决方案1】:

    我做到了! :) 第一个版本,没有缩放

        #include<QString>
        #include<QDebug>
        #include<QByteArray>
        #include<QBuffer>
        #include<QImage>
        #include<QImageWriter>
        #include<QPixmap>
        #include<QPainter>
        #include<QColor>
        #include<QPointF>
        #include<QRectF>
    
    
    
        //ustawiam kolory
        QColor bialy = Qt::white;
        QColor czarny = Qt::black;
    
    //PNG    
            QByteArray ImageAsByteArray;
            QBuffer ImageBuffer(&ImageAsByteArray);
            ImageBuffer.open(QIODevice::WriteOnly);
    
    
    
    
        QRcode *qr=QRcode_encodeString(QString("afya.pl").toStdString().c_str(), 1, QR_ECLEVEL_L, QR_MODE_8,1);
    
        QPixmap p(qr->width,qr->width);
        QPainter pa;
        pa.begin(&p);
        pa.setRenderHints(QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing |QPainter::TextAntialiasing| QPainter::Antialiasing);
        pa.setPen(bialy);
        pa.setBrush(bialy);
        //czyścimy tło
        QPointF a=QPointF(0.0,0.0);
        QPointF b=QPointF(p.width(),p.height());
        pa.drawRect(QRectF(a,b));
    
    
        pa.setPen(czarny);
    
        for (unsigned int y=0; y<qr->width;y++)
        {
            int yy=y*qr->width;
            for (unsigned int x=0; x<qr->width;x++)
            {
            int xx=yy+x;
            const unsigned char b=qr->data[xx];
    
            if(b &0x01){
        a=QPointF(y,x);
        pa.drawPoint(a);
            }
            }
    
    
        }
        p.save(&ImageBuffer,"PNG");
    
    
        qDebug()<<ImageAsByteArray.toBase64();
        }
    

    【讨论】:

    • 如果您使用QImage 而不是QPixmap,即使用QImage::setPixel() 而不是使用QPainter::drawPoint(),则设置单个像素会更加容易。根据qr-&gt;data 的格式,甚至可以将其直接转换为QImage
    猜你喜欢
    • 2019-01-24
    • 2021-03-04
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    相关资源
    最近更新 更多