Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

plot pictures from char buffer



  • Hello All. I hope you all are doing well.

    I have a raspberry pi and with openCV and a webcam it captures pictures, processes it and sends it using socket. In the socket server side, there is an application created by Qt. I got the data but Im having a problem to plot the image.
    Please, remember that as I using socket in armbian system, it sends the data as a char data type flux.

    This is what I tried (first sending a grayscale images)

    char buffer [307200]; //its big but the data that server gets takes packets with 1000 bytes size (308 packets to form an image of 640*480
     QImage *img;
    
        img = new QImage ((uchar *)buffer, 640, 480, QImage::Format_Grayscale8);
    
    
        ui->label->setPixmap(QPixmap::fromImage(*img));
    

    But it always shown a black pictures.



  • The first thing to check is that the data really isn't all zeros.
    Also, I've had better luck displaying Format_Indexed8 with a grayscale color table for 8 bit data.



  • from http://doc.qt.io/qt-5/qimage.html#QImage-3:

    data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.

    You are basically missing an argument to use http://doc.qt.io/qt-5/qimage.html#QImage-5

    QImage img(reinterpret_cast<uchar*>(buffer), 640, 480, 640,QImage::Format_Grayscale8);

    The below is an example showing white noise:

    int main(int argc, char *argv[])
    {
        QApplication a(argc,argv);
        uchar buffer [648*480];
        std::default_random_engine generator;
        std::uniform_int_distribution<quint16> distribution(0,0xff);
        for(auto i=std::begin(buffer);i!=std::end(buffer);++i)
            *i=distribution(generator);
        QImage testImg(buffer,640,480,640,QImage::Format_Grayscale8);
        QLabel result;
        result.setPixmap(QPixmap::fromImage(testImg));
        result.show();
        return a.exec();
    }
    


  • Thanks a lot! problem solved!


Log in to reply