Results 1 to 9 of 9

Thread: How to save an image (png) into a xml file

  1. #1
    Join Date
    Dec 2008
    Posts
    68

    Default How to save an image (png) into a xml file

    hi friend,

    I would like to save an image into an xml file. Your help is highly appreciated!

    Please help!

    Richard -

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to save an image (png) into a xml file

    You could write the png file as QByteArray/QString in the xml, couldnt you ?
    something like -
    <PNGDATA>.......</PNGDATA> where PNGDATA tag holds the data for you.

  3. #3
    Join Date
    Dec 2008
    Posts
    68

    Default Re: How to save an image (png) into a xml file

    I think the content in PNG file is binary and cann't be put in a xml file directly.

    I am thinking to convert them into a text format and save in xml file. when I need to load the image, I can read the text from xml and convert back to binary.

    Is there any reliable way to do this conversion or there is any other way to handle image/binary data in xml?

    Thank you !

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to save an image (png) into a xml file

    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Dec 2008
    Posts
    68

    Default Re: How to save an image (png) into a xml file

    thank you for the input. Following is the implementation.

    question:

    can " in >> imageData; // extract data;" read all image file data into Qbytearray?

    thank you!


    Qt Code:
    1. QFile file( strImageFilename );
    2. file.open(QIODevice::ReadOnly);
    3. QDataStream in(&file); // read the data serialized from the file
    4. QByteArray imageData;
    5. in >> imageData; // extract data;
    6.  
    7. // convert to ascii
    8. QByteArray imageData_Base64 = imageData.toBase64();
    9.  
    10. // save in xml
    11. ImageElement.setAttribute("ImageData_Base64", imageData_Base64 );
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to save an image (png) into a xml file

    ctor of QString takes a QByteArray, see this.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Dec 2008
    Posts
    68

    Default Re: How to save an image (png) into a xml file

    The following code can't read the whole file into rawData.

    What is the problem?

    thank you!


    Qt Code:
    1. // load file
    2. QFile file( strImageFilename );
    3. file.open(QIODevice::ReadOnly);
    4. QDataStream in(&file); // read the data serialized from the file
    5.  
    6. char * rawData = new char [ file.size() ];
    7. in >> rawData; // extract data;
    8. file.close();
    9.  
    10. // convert to ascii
    11. QByteArray imageData( rawData );
    To copy to clipboard, switch view to plain text mode 
    Last edited by richardander; 8th October 2009 at 21:09.

  8. #8
    Join Date
    Oct 2009
    Location
    Rennes, France
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to save an image (png) into a xml file

    If you want load the binary file to a QByteArray juste use readAll

    Qt Code:
    1. QFile file( strImageFilename );
    2. file.open(QIODevice::ReadOnly);
    3. QByteArray imageData = file.readAll();
    4. QByteArray imageData_Base64 = imageData.toBase64();
    To copy to clipboard, switch view to plain text mode 
    Contributor from the French Qt community from developpez.com
    * Forum
    * FAQ Qt ( > 100 QR)
    * Advanced and Beginner Tutorials

  9. #9
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to save an image (png) into a xml file

    take a look at this example, works fine
    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3. #include <QtXml>
    4. #include <QApplication>
    5.  
    6. int main(int argc, char **argv)
    7. {
    8. QApplication app(argc, argv);
    9.  
    10. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    11. db.setDatabaseName("pictures");
    12. if (!db.open())
    13. return db.lastError().number();
    14.  
    15. QSqlQuery query;
    16. qDebug() << query.exec("CREATE TABLE IF NOT EXISTS pictures (id INT PRIMARY KEY, pic BLOB)");
    17.  
    18. QImage image("flower.jpg");
    19. qDebug() << image.size();
    20. QByteArray byteArray;
    21. QBuffer buffer(&byteArray);
    22. buffer.open(QIODevice::WriteOnly);
    23. image.save(&buffer, "jpg");
    24.  
    25. QDomElement root = doc.createElement("PictureData");
    26. doc.appendChild(root);
    27. QDomText data = doc.createTextNode(byteArray.toBase64());
    28. root.appendChild(data);
    29.  
    30. query.prepare("INSERT INTO pictures (id, pic) VALUES (?, ?)");
    31. query.addBindValue(1);
    32. query.addBindValue(doc.toString());
    33. qDebug() << query.exec();
    34.  
    35. qDebug() << query.exec("SELECT pic FROM pictures WHERE id = 1");
    36. qDebug() << query.next();
    37.  
    38. doc.setContent(query.value(0).toString());
    39. root = doc.documentElement();
    40. data = root.firstChild().toText();
    41. byteArray = QByteArray::fromBase64(data.toText().data().toAscii());
    42.  
    43. QPixmap pixmap = QPixmap::fromImage(QImage::fromData(byteArray, "jpg"));
    44. qDebug() << pixmap.size();
    45.  
    46. QLabel label;
    47. label.setPixmap(pixmap);
    48. label.show();
    49.  
    50. return app.exec();
    51. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. How to save file with QFileDialog
    By pnikolov in forum Qt Programming
    Replies: 11
    Last Post: 1st June 2012, 10:23
  2. how to save a image file ( say png ) into mysql database?
    By AviMittal in forum Qt Programming
    Replies: 12
    Last Post: 21st July 2011, 12:49
  3. Replies: 2
    Last Post: 21st August 2009, 23:09
  4. Saving pure plot data to image file
    By Debilski in forum Qwt
    Replies: 4
    Last Post: 7th April 2009, 17:02
  5. Save greyscale image
    By Lele in forum Qt Programming
    Replies: 8
    Last Post: 12th August 2008, 08:59

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.