Results 1 to 6 of 6

Thread: Send QImage over network

  1. #1
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Send QImage over network

    Hi, I'm trying to send a QImage through QTcpSockets, but the data always comes out corrupted on the receiving end.

    What's the most direct way of writing and reading the image?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Send QImage over network

    The most direct way is to write the data directly to the socket and read it the same way on the other end. But if you provide the code you wrote to write and feed and retrieve the image from the socket, maybe we'll be able to tell what mistakes you have made.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Send QImage over network

    I've tried several approaches. The main problem is retrieving the actual data from the QImage class, and building a new QImage object from that data on the other side.

    I tried using QImageWriter to write the image to a file on the hdd, and then using QFile, read the data, and transmit the QByteArray over the socket:

    Qt Code:
    1. // This code is inside my subclass of QTcpSocket
    2. QImageWriter writer( QString("image.jpg"), "jpg" );
    3. writer.write(image);
    4.  
    5. QFile imageFile( QString("image.jpg"), this );
    6. QByteArray data = imageFile.readAll();
    7.  
    8. if(write(data))
    9. emit writeln(tr("Write Successful"));
    To copy to clipboard, switch view to plain text mode 

    Then on the other side I just do this:
    Qt Code:
    1. QByteArray data = readAll();
    2. QFile out(tr("output.jpg"));
    3. out.open( QIODevice::ReadWrite );
    4. out.write(data);
    5. out.close();
    To copy to clipboard, switch view to plain text mode 

    However, I also tried wrapping the QTcpSocket with QImageWriter, and transmitting that way:

    Qt Code:
    1. QImageWriter writer( this, "jpg" );
    2. writer.write(image);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Send QImage over network

    Have you checked that the appropriate amount of data is ready to be read on the receiving end? The fact that you are notified there is something to be read doesn't mean there is everything to be read, maybe that's the problem?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Send QImage over network

    Quote Originally Posted by wysota View Post
    Have you checked that the appropriate amount of data is ready to be read on the receiving end? The fact that you are notified there is something to be read doesn't mean there is everything to be read, maybe that's the problem?
    Thank you for your help. I'm still quite new to network programming in QT. Right now I'm using QIODevice::readyRead() signal to notify my socket when to begin reading. Is that incorrect? How should I determine when the reading should begin?

    I tried sending the size of the file first, but I find that readyRead() is emitted several times for each transfer.

  6. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Send QImage over network

    it is ok to begin reading. the point is when to stop.
    because unless you send the package size (before the image data) you will never know if the image is complete.

    see network-fortuneclient for an example to wait for a complete package of network data

    hth

Similar Threads

  1. Replies: 12
    Last Post: 7th September 2011, 16:37
  2. Replies: 12
    Last Post: 22nd March 2009, 11:22
  3. Streaming QImage (QByteArray, QDataStream, QBuffer)
    By knarz in forum Qt Programming
    Replies: 5
    Last Post: 17th January 2009, 22:05
  4. Multiple QPainters on QImage
    By fire in forum Qt Programming
    Replies: 3
    Last Post: 14th August 2008, 13:10
  5. Send QEvents over network?
    By Winni in forum Qt Programming
    Replies: 2
    Last Post: 2nd October 2007, 20:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.