Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Image transfer using QTcpsocket
Forum Updated to NodeBB v4.3 + New Features

Image transfer using QTcpsocket

Scheduled Pinned Locked Moved General and Desktop
18 Posts 2 Posters 12.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anmol2701
    wrote on last edited by
    #1

    Can anybody guide how to transfer image over QTcpsocket.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      anmol2701
      wrote on last edited by
      #2

      Hello Experts!
      I am able to send image using below method:

       QByteArray read ;
       inputFile.open(QIODevice::ReadOnly);
       while(1)
       {
           read.clear();
           read = inputFile.read(inputFile.size());
           qDebug() << "Read : " << read.size();
           if(read.size()==0)
                break;
           qDebug() << "Written : " << socket->write(read);
           socket->waitForBytesWritten();
           read.clear();
       }
      

      But how to read data of image on client side and save the image??

      p3c0P 1 Reply Last reply
      0
      • A anmol2701

        Hello Experts!
        I am able to send image using below method:

         QByteArray read ;
         inputFile.open(QIODevice::ReadOnly);
         while(1)
         {
             read.clear();
             read = inputFile.read(inputFile.size());
             qDebug() << "Read : " << read.size();
             if(read.size()==0)
                  break;
             qDebug() << "Written : " << socket->write(read);
             socket->waitForBytesWritten();
             read.clear();
         }
        

        But how to read data of image on client side and save the image??

        p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #3

        @anmol2701 Well once you receive data chunks on client side, store it in a QByteArray. Once you receive all the data you can use QImage::fromData to get the image.

        157

        A 1 Reply Last reply
        0
        • p3c0P p3c0

          @anmol2701 Well once you receive data chunks on client side, store it in a QByteArray. Once you receive all the data you can use QImage::fromData to get the image.

          A Offline
          A Offline
          anmol2701
          wrote on last edited by
          #4

          @p3c0 Thanks for reply

          I am able receive data on client side and i am trying save image as as image format(png or jpg) using following code but after writing ,file is empty.

          QString fileName = QFileDialog::getSaveFileName(0,"Save file",QDir::currentPath(),
          "Image files (.PNG);;Text files (.txt);;All files (.)",
          new QString("Image files (*.PNG)"));
          if (!fileName.isEmpty())
          {
          QFile file(fileName);
          if (!file.open(QIODevice::WriteOnly))
          {
          QImage img(2048,1024,QImage::Format_Indexed8);
          img = QImage::fromData(data,"PNG");
          }

          p3c0P 1 Reply Last reply
          0
          • A anmol2701

            @p3c0 Thanks for reply

            I am able receive data on client side and i am trying save image as as image format(png or jpg) using following code but after writing ,file is empty.

            QString fileName = QFileDialog::getSaveFileName(0,"Save file",QDir::currentPath(),
            "Image files (.PNG);;Text files (.txt);;All files (.)",
            new QString("Image files (*.PNG)"));
            if (!fileName.isEmpty())
            {
            QFile file(fileName);
            if (!file.open(QIODevice::WriteOnly))
            {
            QImage img(2048,1024,QImage::Format_Indexed8);
            img = QImage::fromData(data,"PNG");
            }

            p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            @anmol2701 You don't need QFile to write QImage to the file. Use QImage::save to save the image. To check if it is valid use isNull.
            Have you made sure all the data is received on the client side ?

            157

            1 Reply Last reply
            0
            • A Offline
              A Offline
              anmol2701
              wrote on last edited by
              #6

              Now I am sending image using following code:

                imageObject = new QImage();
                  imageObject->load(imagePath);
                  image = QPixmap::fromImage(*imageObject);
              
                  QByteArray ba;              // Construct a QByteArray object
                  QBuffer buffer(&ba);        // Construct a QBuffer object using the QbyteArray
                  image.save(&buffer, "PNG"); // Save the QImage data into the QBuffer
                  socket->write(ba);
              

              Receive all the data on client side, but the saved image is blurred.

              p3c0P 1 Reply Last reply
              1
              • A anmol2701

                Now I am sending image using following code:

                  imageObject = new QImage();
                    imageObject->load(imagePath);
                    image = QPixmap::fromImage(*imageObject);
                
                    QByteArray ba;              // Construct a QByteArray object
                    QBuffer buffer(&ba);        // Construct a QBuffer object using the QbyteArray
                    image.save(&buffer, "PNG"); // Save the QImage data into the QBuffer
                    socket->write(ba);
                

                Receive all the data on client side, but the saved image is blurred.

                p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                @anmol2701 If the data received on client side is exactly same on sender side then there should be no problem. Are you sure image that is being send is not blurred ? Or the image that you are loading is of other format and you are saving it as png ?

                157

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  anmol2701
                  wrote on last edited by
                  #8

                  image sending and saving is png format.
                  Plz share
                  Any documentation or link for more details??

                  p3c0P 1 Reply Last reply
                  0
                  • A anmol2701

                    image sending and saving is png format.
                    Plz share
                    Any documentation or link for more details??

                    p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #9

                    @anmol2701 I still doubt that some bytes are still missing on client side. How did you check that ?

                    157

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      anmol2701
                      wrote on last edited by anmol2701
                      #10

                      I was checking qDebug() << "Written : " << socket->write(ba);
                      or qdeqint64 W_Length = file.readLine(buffer, sizeof(buffer));
                      On client side qint64 R_bytes = buffer->write(socket->readAll());

                      p3c0P 1 Reply Last reply
                      0
                      • A anmol2701

                        I was checking qDebug() << "Written : " << socket->write(ba);
                        or qdeqint64 W_Length = file.readLine(buffer, sizeof(buffer));
                        On client side qint64 R_bytes = buffer->write(socket->readAll());

                        p3c0P Offline
                        p3c0P Offline
                        p3c0
                        Moderators
                        wrote on last edited by
                        #11

                        @anmol2701 So are they equal ?
                        On sender side you have the image i.e you can check its size in bytes manually and on the client side check the size of the whole bytearray from which you construct the image. They should match exactly.

                        157

                        A 1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          anmol2701
                          wrote on last edited by
                          #12
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • p3c0P p3c0

                            @anmol2701 So are they equal ?
                            On sender side you have the image i.e you can check its size in bytes manually and on the client side check the size of the whole bytearray from which you construct the image. They should match exactly.

                            A Offline
                            A Offline
                            anmol2701
                            wrote on last edited by
                            #13

                            @p3c0 Hi!

                            Now I am saving image using
                            image_rcv.loadFromData(test);

                            It gives error: libpng error: PNG unsigned integer out of range.
                            Any Suggestion??

                            p3c0P 1 Reply Last reply
                            0
                            • A anmol2701

                              @p3c0 Hi!

                              Now I am saving image using
                              image_rcv.loadFromData(test);

                              It gives error: libpng error: PNG unsigned integer out of range.
                              Any Suggestion??

                              p3c0P Offline
                              p3c0P Offline
                              p3c0
                              Moderators
                              wrote on last edited by
                              #14

                              @anmol2701 Are you now receiving all the data ? I see in your earlier post that you are sending 560 bytes and receiving 533 bytes on client side.

                              157

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                anmol2701
                                wrote on last edited by
                                #15

                                Yes,

                                Now I am receiving all the data in case of png file.

                                p3c0P 1 Reply Last reply
                                0
                                • A anmol2701

                                  Yes,

                                  Now I am receiving all the data in case of png file.

                                  p3c0P Offline
                                  p3c0P Offline
                                  p3c0
                                  Moderators
                                  wrote on last edited by
                                  #16

                                  @anmol2701 That error is from libpng. It is not able to reconstruct the image from the data. I guess the data is mismatched.

                                  157

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    anmol2701
                                    wrote on last edited by anmol2701
                                    #17

                                    Hi!

                                    I am able construct image.
                                    Now the problem is with large size of images.

                                    I am following this approach:

                                    while (buffer->canReadLine())
                                    {
                                    QByteArray data_r = buffer->readLine();
                                    image_r.append(data_r);
                                    }
                                    Then create image from Qbytearray image_r.
                                    Any suggestion?

                                    p3c0P 1 Reply Last reply
                                    0
                                    • A anmol2701

                                      Hi!

                                      I am able construct image.
                                      Now the problem is with large size of images.

                                      I am following this approach:

                                      while (buffer->canReadLine())
                                      {
                                      QByteArray data_r = buffer->readLine();
                                      image_r.append(data_r);
                                      }
                                      Then create image from Qbytearray image_r.
                                      Any suggestion?

                                      p3c0P Offline
                                      p3c0P Offline
                                      p3c0
                                      Moderators
                                      wrote on last edited by
                                      #18

                                      @anmol2701 If your earlier logic works perfectly then it should work for all files.
                                      Anyway you can also look at serialization. Qt has provision to serialize some of the Qt data types. QDataStream can be used here to transferQImage directly. Have a look at fortuneserver and fortuneclient example. Try doing similar for QImage.

                                      157

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved