Image transfer using QTcpsocket
-
@anmol2701 You don't need
QFile
to writeQImage
to the file. Use QImage::save to save the image. To check if it is valid useisNull
.
Have you made sure all the data is received on the client side ? -
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.
-
@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 ?
-
image sending and saving is png format.
Plz share
Any documentation or link for more details?? -
@anmol2701 I still doubt that some bytes are still missing on client side. How did you check that ?
-
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()); -
@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. -
This post is deleted! -
@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?? -
@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.
-
Yes,
Now I am receiving all the data in case of png file.
-
@anmol2701 That error is from libpng. It is not able to reconstruct the image from the data. I guess the data is mismatched.
-
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? -
@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 forQImage
.