PDA

View Full Version : Save pictures in JPEG format into QDataStream



Eos Pengwern
31st December 2009, 09:33
Hello,

Is there a convenient way to save photographic images, using JPEG compression, into a QDataStream?

According to the Qt documentation, the QImage and QPixmap << operators save the image data in PNG format, but this is not very efficient (from the compression point of view) when the images are colour photographs rather than line graphics.

In my application, I have objects that contain multiple photographic images, alongside a bunch of other metadata (including other images which are best saved in PNG format). I'd like to be able to serialize the whole contents of the object into a single binary file, but with JPEG compression for the photographic images - I don't want to have to save a bunch of .jpg files with a separate binary file for everything else.

Can this be done?

Thanks,
Stephen.

wysota
31st December 2009, 11:39
Use QImage::save() variant that takes a QIODevice to write the image to a byte array and then stream the array to the stream.

Eos Pengwern
31st December 2009, 13:14
Aha; I think I see.

So if I have a QImage called 'fred', for example, I would use (following the example given in the Qt documentation for QImage::save):


QByteArray ba;
QBuffer buffer(&ba); // QBuffer inherits QIODevice
buffer.open(QIODevice::WriteOnly);
fred.save(&buffer, "JPG")
buffer.close();

Then if 'ds' is the data stream that the rest of my object's data is being written to, then:


ds << ba;

..should complete the process.

Looks good! Thank you very much.