2016-11-06 150 views
0

所有我需要的是簡單(Q)字符串把它作爲像嵌入圖像:任何想法,如何將QR數據編碼成png圖像?

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

我用:#include <qrencode.h>(Linux的 - >的apt-get安裝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->dataPNG圖像

回答

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

設置單獨的像素更容易,如果你使用'QImage'代替FOF'QPixmap',即用'的QImage :: setPixel()的',而不是使用'的QPainter :: drawPoint ()'。根據'qr-> data'的格式,甚至可以直接將其轉換爲'QImage' –

相關問題