Results 1 to 3 of 3

Thread: Save pictures in JPEG format into QDataStream

  1. #1
    Join Date
    Aug 2009
    Posts
    50
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Save pictures in JPEG format into QDataStream

    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.

  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: Save pictures in JPEG format into QDataStream

    Use QImage::save() variant that takes a QIODevice to write the image to a byte array and then stream the array to the stream.
    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
    Aug 2009
    Posts
    50
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Save pictures in JPEG format into QDataStream

    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):

    Qt Code:
    1. QBuffer buffer(&ba); // QBuffer inherits QIODevice
    2. buffer.open(QIODevice::WriteOnly);
    3. fred.save(&buffer, "JPG")
    4. buffer.close();
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. ds << ba;
    To copy to clipboard, switch view to plain text mode 

    ..should complete the process.

    Looks good! Thank you very much.

Similar Threads

  1. How to save graphics as dxf format
    By soumya in forum Qt Programming
    Replies: 4
    Last Post: 31st December 2009, 20:32
  2. Saving image in jpeg format failed
    By febil in forum Qt Programming
    Replies: 5
    Last Post: 23rd April 2009, 11:33
  3. cannot save QPixmap as jpeg
    By arunredi in forum Qt Programming
    Replies: 1
    Last Post: 16th May 2008, 08:42
  4. Some very weird compilation warnings
    By MarkoSan in forum Qt Programming
    Replies: 21
    Last Post: 23rd January 2008, 16:48

Tags for this Thread

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.