有什么想法,如何将QR数据编码成png图像?

时间:2016-11-06 11:08:55

标签: qt base64 png embed qr-code

我只需要简单的(Q)字符串就可以将其作为嵌入式图像,如:

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

我使用:#include <qrencode.h>(linux - &gt; 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->data编码为 png 图像?

1 个答案:

答案 0 :(得分:0)

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

    #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();
    }