Results 1 to 7 of 7

Thread: QImage sent through QDataStream

  1. #1
    Join Date
    Dec 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    1

    Default QImage sent through QDataStream

    Hi,

    I'm trying to send images over a network connection the easiest way possible. Its not a Video stream and its only local network, so the bandwidth usage is not that important.

    I've tried two approaches till now:

    First using the overloaded << operator "QDataStream << QImage"

    Client Side:
    Qt Code:
    1. // QImage image is already instantiated
    2. _socket->connectToHost("127.0.0.1", 11111, QIODevice::ReadWrite);
    3. QDataStream stream(_socket);
    4. stream << image;
    5. _socket->waitForBytesWritten(-1);
    To copy to clipboard, switch view to plain text mode 

    Server Side:
    Qt Code:
    1. QAbstractSocket *socket = _server.nextPendingConnection();
    2. QDataStream stream(socket, QIODevice::ReadWrite);
    3. socket->waitForReadyRead(-1);
    4. stream >> inQImage;
    5. if (inQImage.isNull()){
    6. qDebug("Received Image is empty");
    7. }
    To copy to clipboard, switch view to plain text mode 
    Result:
    libpng error: Read Error
    Received Image is empty


    Second Approach using the QImageWriter and QImageReader:
    Client Side:
    Qt Code:
    1. QImageWriter writer(socket, "png");
    2. writer.write(image);
    3. socket->waitForBytesWritten(-1);
    To copy to clipboard, switch view to plain text mode 

    Server Side:
    Qt Code:
    1. _socket->waitForReadyRead(-1);
    2. QImageReader imReader(_socket,"png");
    3. imReader.read(_image);
    4. _image.save("received.png","png");
    To copy to clipboard, switch view to plain text mode 

    The Result is also an libpng error: Read Error but the Image is partially transferred. When using a sufficiently small image it works.
    It also works when the IODevice is not a QTcpSocket but a QFile

    In both cases is seams that not all data is sent, is there a possibility that waitForBytesWritten() is not working as expected?

    thanks in advance
    Daniel

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

    Default Re: QImage sent through QDataStream

    readyRead is emitted whenever there is any data available, not whenever there is all data available. Your image simply didn't fully arive just yet.
    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
    Dec 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: QImage sent through QDataStream

    Thanks for the quick reply.

    Shouldn't both the ImageReader and the >> operator check how big the picture is, and repeatedly read from the IODevice.
    If not, has anyone a solution?

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

    Default Re: QImage sent through QDataStream

    Quote Originally Posted by dfink View Post
    Shouldn't both the ImageReader and the >> operator check how big the picture is, and repeatedly read from the IODevice.
    No they shouldn't. How should they know that no more data would come?

    If not, has anyone a solution?
    Send the size of the image before sending the image itself. Then continue waiting until there is enough data ready. Only then read the data from the socket.
    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. The following user says thank you to wysota for this useful post:

    dfink (2nd December 2009)

  6. #5
    Join Date
    Dec 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: QImage sent through QDataStream

    Thanks for the Hints, it finally works.
    I used repeated Calls to readRawData(char* , int len) of QDataStream which did the trick.
    What I find weird though is that using QDataStream & QDataStream::readBytes ( char *& s, uint & l ) didn't work at all.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QImage sent through QDataStream

    readBytes() and readRawBytes() do two completely different things so you can't exchange one for the other.
    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.


  8. #7
    Join Date
    Aug 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QImage sent through QDataStream

    Hi,

    This thread is a bit old but now I have the same problem.

    I have connected the readyread signal to my slot but i cannot receive the whole image when readyread is emitted. I only receive 8192 bytes of. How can i make the program wait for whole to come? Is there any example code?

    Thanks...

Similar Threads

  1. Replies: 12
    Last Post: 7th September 2011, 17:37
  2. setGamma/setQuality to QImage
    By ArmanS in forum Qt Programming
    Replies: 1
    Last Post: 3rd October 2009, 11:00
  3. QImage internal error
    By GiuseppeBonfa in forum Qt Programming
    Replies: 1
    Last Post: 4th August 2009, 15:12
  4. Streaming QImage (QByteArray, QDataStream, QBuffer)
    By knarz in forum Qt Programming
    Replies: 5
    Last Post: 17th January 2009, 23:05
  5. Multiple QPainters on QImage
    By fire in forum Qt Programming
    Replies: 3
    Last Post: 14th August 2008, 14:10

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
  •  
Qt is a trademark of The Qt Company.