You are on page 1of 11

hanckmann.

com - blog

2010-12-05

Send an image over a


network using Qt

A question that pops up every now and again and this time Ill document my attempt to
do this. To do this I use the Qt framework. More specifically, I used QtcpSocket for the
connection.
In this example program I will use a client-server approach to this problem. I will build a
server that accepts one or more connections and can receive images. I will also build a
client that can connect to the server and send an image.

The Client-Server
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

The Client-Server
architecture

Most of the code that set up the actuall connection have been borrowed from the Simple
Chat program. In this example a client-server system is build. What I removed from this
example is all the GUI interfacing and the functions that receive and send the messages.
The first is because I do not need a GUI here as I will use it as a lib, and the latter is
because I do not need to send and receive QString objects (and more important, I do not
have to forward them to all other connected clients).
So If you want a deeper understanding of how the connection is set-up and how the
server deals with multiple connections, then I point forward to the wiki at qtcentre.org.

How to send an image


Here I will actually cover how to send an image. In Qt this is rather easy. For the sake of
structure, I will first describe the client-side process and then the server-side process.

Sending the image (Client-side process)


What I need to do is to convert the image to a byte-array and then send this byte-array
over the network. As we are using the Qt framework, I assume that the image is already
a QImage object and the socket has been set-up by following the Simple chat example. Th
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

en sending the image is rather easy:


// To load a QImage
//QImage image;
// See the documentation on how to use QImage objects
//image.load("test.png", "PNG");
// To set-up the socket
//socket->connectToHost(address, port, QIODevice::WriteOnly)
QByteArray ba;
QBuffer buffer(&ba);
image.save(&buffer, "PNG");
socket->write(ba);

//
//
//
//

Construct a QByteArray object


Construct a QBuffer object using the QbyteArray
Save the QImage data into the QBuffer
Send the QBuffer (QbyteArray) over a socket

And that is it! Yes, there are some beautifications possible, but this is the trick!

Receiving the image (Server-side process)


Here I need to convert the incoming stream back into an image. This is basically as easy
as sending it, but I will anyway show it. Also here I assume that there is a listening socket
available, but I also assume that the buffer. This time no example code on that, as it takes
more lines to describe.
// Available are:
//QTcpSocket* socket;
//QBuffer* buffer = buffers.value(socket);
qint64 bytes = buffer->write(socket->readAll()); // Read all the received bytes
buffer->seek(buffer->pos() - bytes); // go back as many bytes as we just wrote so that
it can be read
QImage image;
// Construct a new QImage
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

image.loadFromData(buffer->buffer()); // Load the image from the receive buffer


if (image.isNull())
// Check if the image was indeed received
qDebug("The image is null. Something failed.");
// If it did not fail, the image is now in the QImage object again

It could hardly be simpler, isnt it?

Extra: Send the image


name as well

If you want the client-side to give a name to this image then this is perfectly possible by
using some interesting options from the QImage object. To attach the image name as a
tag to the image use:
//QImage image has already been constructed
image.setText("name", name);

On the receiving end we can retrieve this tag information easily using:
//QImage image has already
image.text("name")

been constructed

This leaves us with a lot of interesting possibilities so be sure you check those out as
well!
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Final notes
Well, I took also the liberty to put this in code so you can download it, test it, and learn
from it. You can find the source code here.
If you have any comments or questions on the code then leave a comment below or send
me an e-mail. If you make some changes in the code then I am happy to receive those
changes as well (as diff or as new zip file).
I hope this was helpful for you!
## Comments
### Anonymous (not verified)
. January 20th, 2011
### [Source Code Link Broken](/?q=comment/30924#comment-30924)
Hey, the code examples look good. I wanted to look at the total program and source cod
e so I could have a better idea of what was going on. Anyway you could email it to me?
or check the link?
Thanks

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

### patrick
. December 4th, 2012
### [Link checked and should be working.](/?q=comment/34581#comment-34581)
Link checked and should be working.
### VIVEK (not verified)
. October 14th, 2011
### Error
qDebug() << QImageReader::supportedImageFormats () << "WE R HERE";
image->load("abc","png");
qDebug() << QImageReader::supportedImageFormats () << "WE R HERE";
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);
OUTPUT:
("bmp", "gif", "ico", "jpeg", "jpg", "mng", "pbm", "pgm", "png", "ppm", "svg", "svgz",
"tif", "tiff", "xbm", "xpm")
The program has unexpectedly finished.WE R HERE
### patrick
. October 19th, 2011

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

### [Please elaborate](/?q=comment/31039#comment-31039)


Where did you get an error in the _original_ code?
Or
Where did you change the code and then where did you get an error?
The text you copy-pasted above is not very useful for me! Don't hesitate to ask me for
more info!
### Vivek (not verified)
. December 26th, 2011
### [Image load failed from sender](/?q=comment/31047#comment-31047)
Image load failed from sender side and as a result receiver is receiving empty file. Wh
at might be problem. Its working fine on machine where qt is installed but not working
on machine where qt is not installed.
### Jerry (not verified)
. November 28th, 2012
### [Receiving the image (Server-side process)](/?q=comment/34575#comment-34575)
I have tried different ways to save a received image file over the network using socket
s to file. This what i have done so far:
/******************Code**************************/
QBuffer *ImageBuffer = new QBuffer;
qint64 bytes = ImageBuffer->write(socket->readAll());
ImageBuffer->seek(ImageBuffer->pos() - bytes);
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

recvdImage.loadFromData(ImageBuffer->buffer());
if(!recvdImage.isNull()){
qDebug() << "Image file was received ";
}
/******************End of Code**************************/
I get an error on the server console:
QBuffer::seek: Invalid pos:1;
I get some image data from clients through the socket, I would like to save it to a fil
e as png, jpg. So that I can do further analysis on that.
Any help will be greatly appreciated, thanks.
### patrick
. December 1st, 2012
If you get an error server-side, then please provide the server side code. The client-s
ide code seems oke... but will not help me (or you) to debug. If you do not want to pas
t it, then send it to me via [e-mail](http://www.hanckmann.net/?q=node/7)!
### Jerry (not verified)
. December 3rd, 2012
Hi Patrick..this is the server side code, the client is an android phone sending an ima
ge. i can't seem to get the image file in full, please help. The client is sending the
data in int64.
### patrick
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

. December 4th, 2012


Aha... we are confused over what is a server and what is a client. So... let me rephras
e:
The issue seems to be on the sender side (which I called the server), since the code on
the receiving end seems fine. So, I need to see how you provide the data.
Did you try to write a sender in Qt?
Did you try the sender I describe in this blog-post?
Please, first try that!
If you get an error in that case... then I can help.
If not, then I need to see the android code... but I have little experience with Androi
d... so no promisses there!
P
PS. If you want to contact me directly, you can find my e-mail address on the "Contact"
page!
### Saravanan (not verified)
. March 4th, 2014
I have tried the above code.But I am implementing the same code in QT installed in Rasp
berry Pi.
I need to send an image to the modem by using QT.I need to convert the image into the b
yte format and then send the same to the GSM modem.Can you guide me in this issue.Thank
s in advance.
open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Removed the remaining old comments :-)

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

0 Comments

Recommend

hanckmann.com

Share

Start the discussion

Be the first to comment.

ALSO ON HANCKMANN.COM

System hostname not sent by DHCP

Send an image over a network using Qt

1 comment a year ago

2 comments 2 years ago

grawity You could just `nmcli con modify "ethernet"

hanckmann Hi,You are right. The source

ipv4.dhcp-hostname hostname`, by the way.

missing after the sites-update. I re-added it


find it under this link:

Subscribe

d Add Disqus to your site

Privacy

2008 - 2014 Patrick Hanckmann. License information can be found here.


Quotes Waking Life (imdb).

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com