Congratulations to our 2022 Qt Champions!

Convert Qbytearray to QImage



  • Hi All,

    How to convert Qbytearray data to Qimage, its in the below format

    "\xFF\xD8\xFF\xE0\x00\x10JFIF\x00\x01\x01\x01\x00`\x00`\x00\x00\xFF\xDB\x00""C\x00\b\x06\x06\x07\x06\x05\b\x07\x07\x07\t\t\b\n\f\x14\r\f\x0B\x0B\f\x19\x12\x13\x0F\x14\x1D\x1A\x1F\x1E\x1D\x1A\x1C\x1C $.' \",#\x1C\x1C(7),01444\x1F'9=82<.342\xFF\xC0\x00\x0B\b\x02\b\x02\b\x01\x01\x11\x00\xFF\xC4\x00\x1F\x00\x00\x01\x05\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\b\t\n\x0B\xFF\xC4\x00\xB5\x10\x00\x02\x01\x03\x03\x02\x04\x03\x05\x05\x04\x04\x00\x00\x01}\x01\x02\x03\x00\x04\x11\x05\x12!1A\x06\x13Qa\x07\"q\x14""2\x81\x91\xA1\b#B\xB1\xC1\x15R\xD1\xF0$3br\x82\t\n\x16\x17\x18\x19\x1A%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\x83\x84\x85\x86\x87\x88\x89\x8A\x92\x93\x94\x95\x96\x97\x98\x99\x9A\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFF\xDA\x00\b\x01\x01\x00\x00?\x00\xF0\n(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A(\xA2\x8A*\xF5\x9E\x8D\xAAj0\x99\xACt\xDB\xCB\x98\x83m/\x04\f\xEA\x0F\\d\x0E\xBC\x8F\xCE\xAC\x7F\xC2/\xE2\x0F\xFA\x01j\x7F\xF8\t'\xF8U\xDB?\x01x\xA6\xFA#$:4\xEA\xA1\xB6\xE2r\xB0\xB6~\x8E""A\xC7=zU\x9F\xF8V\x9E/
    

    then

      QImage image;
      qDebug()<<"img data::"<<image.loadFromData(socket->readAll(),"PNG");
    

    returning False here;



  • Hi,
    your data are not in png format...
    Try it without forcing an image format so the image load function can guess the format from the data



  • Hi @Gerd,
    Thanks for replying.

    then also returning false.

    qDebug()<<"img data::"<<image.loadFromData(socket->readAll());
    

  • Moderators

    @sharath said in Convert Qbytearray to QImage:

    socket->readAll()

    The data is apparently coming from a Tcp/Udp socket,
    how do you know you have all the needed data ?



  • hi @J-Hilk ,

    Thanks for reply.

    So how can i convert Bytearray data to QImage that are coming from Tcp socket?


  • Lifetime Qt Champion

    @sharath said in Convert Qbytearray to QImage:

    So how can i convert Bytearray data to QImage that are coming from Tcp socket?

    You have to make sure that all data for the is read from the socket. So you have to add some kind of protocol on your socket which helps you to know this.



  • hi @Christian-Ehrlicher,

    Thanks for your reply.

    Is there any example code?


  • Lifetime Qt Champion

    @sharath

    Developing own protocols is not easy, it requires lot's of experience to reinvent what is already there, and mostly don't pay off.

    So could you tell us what you really want to achive? We have dozens of file transfer protocols, HTTP being the most known one. So why not use one of the established protocols?

    Regards



  • @aha_1980 i need to achieve live camera streaming like producer and consumer concept. In my case embedded application is producer and my mobile application is consumer using TCP.
    How can i pass each frames from embedded application through TCP socket and receive those frames and display that frames on my mobile application.
    If you give me some Example links that would really appreciated.

    Thanks,
    Sharath


  • Lifetime Qt Champion

    As we already said you need to define a protocol. But for video streaming there are already working protocols which you can use -> search in the internet for it.



  • @Christian-Ehrlicher I got to know that GStreamer is best fit for this. but there is no examples how to use it in Qt?
    How can i send my imagedata to gst_pipeline? please provide some sample, so that i can start with it quickly.


  • Lifetime Qt Champion

    By following the documentation like written in your other thread.



  • @SGaist I didn't understand how to use that.
    For each Frame should i repeat this step?

    QImage img("images/qt-logo.png"); //in my case its frames 
    img = img.convertToFormat(QImage::Format_ARGB32);
    QByteArray ba(reinterpret_cast<const char *>(img.bits()), img.sizeInBytes());
    QBuffer buffer(&ba);
    buffer.open(QIODevice::ReadOnly);
    player = new QMediaPlayer;
    player->setMedia(QUrl("gst-pipeline: appsrc blocksize=4294967295 ! \
        video/x-raw,format=BGRx,framerate=30/1,width=200,height=147 ! \
        coloreffects preset=heat ! videoconvert ! video/x-raw,format=I420 ! jpegenc ! rtpjpegpay ! \
        udpsink host=127.0.0.1 port=5000"), &buffer);
    player->play();
    


  • Hi All,
    I need to transmit some black and white photos from my client to server,
    used QTcpServer and QTcpSocket.
    At the client i transfer the BMP data into QImage ,then QByteArray obj,and at the server i turn it back into QImage.
    I`m sure server has recive complete QByteArray,
    because i still have some color BMP photos that can be correctly turned into QImage and show on my server UI.
    So my question is why the Blacke&White ones strongly distort,but the color ones unaffected?

            ///Client transfer code
            //QImage save to QByteArray
    	QByteArray qbtImageArray;
    	QBuffer qBuffer(&qbtImageArray);
    	qImg->save(&qBuffer, "bmp");
    
    	//Data length and index(for identify)
    	NetImageDataHead objNet;
    	objNet.nImageSize = qbtImageArray.size();
    	objNet.nImageIndex = pNetDeviceParams->nIndex;
    
            //Append head and data
    	QByteArray qbtMsgArray;
    	qbtMsgArray.append((char *)&objNet, sizeof(NetImageDataHead));
    	qbtMsgArray.append(qbtImageArray);
    
            //Server code
            //QByteArray to QImage
            QBuffer buffer(&qbImgData);
            QImageReader reader(&buffer, "bmp");
            QImage img = reader.read();
            pLabel->setPixmap(QPixmap::fromImage(img));

  • Lifetime Qt Champion

    Why do you hijack an old thread instead creating a new one with your specific problem.
    Also please provide some code on hwo you send and receive your data in the new thread.


Log in to reply