PDA

View Full Version : How to save an image (png) into a xml file



richardander
8th October 2009, 10:49
hi friend,

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

Please help!

Richard -

aamer4yu
8th October 2009, 15:16
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.

richardander
8th October 2009, 19:03
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 !

spirit
8th October 2009, 19:09
use QByteArray::toBase64 and QByteArray::fromBase64.

richardander
8th October 2009, 20:46
thank you for the input. Following is the implementation.

question:

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

thank you!




QFile file( strImageFilename );
file.open(QIODevice::ReadOnly);
QDataStream in(&file); // read the data serialized from the file
QByteArray imageData;
in >> imageData; // extract data;

// convert to ascii
QByteArray imageData_Base64 = imageData.toBase64();

// save in xml
ImageElement.setAttribute("ImageData_Base64", imageData_Base64 );

spirit
8th October 2009, 20:50
ctor of QString takes a QByteArray, see this (http://doc.trolltech.com/4.5/qstring.html#QString-8).

richardander
8th October 2009, 21:56
The following code can't read the whole file into rawData.

What is the problem?

thank you!



// load file
QFile file( strImageFilename );
file.open(QIODevice::ReadOnly);
QDataStream in(&file); // read the data serialized from the file

char * rawData = new char [ file.size() ];
in >> rawData; // extract data;
file.close();

// convert to ascii
QByteArray imageData( rawData );

yan
8th October 2009, 22:25
If you want load the binary file to a QByteArray juste use readAll (http://doc.trolltech.com/4.5/qiodevice.html#readAll)


QFile file( strImageFilename );
file.open(QIODevice::ReadOnly);
QByteArray imageData = file.readAll();
QByteArray imageData_Base64 = imageData.toBase64();

spirit
9th October 2009, 10:57
take a look at this example, works fine


#include <QtGui>
#include <QtSql>
#include <QtXml>
#include <QApplication>

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("pictures");
if (!db.open())
return db.lastError().number();

QSqlQuery query;
qDebug() << query.exec("CREATE TABLE IF NOT EXISTS pictures (id INT PRIMARY KEY, pic BLOB)");

QImage image("flower.jpg");
qDebug() << image.size();
QByteArray byteArray;
QBuffer buffer(&byteArray);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "jpg");

QDomDocument doc;
QDomElement root = doc.createElement("PictureData");
doc.appendChild(root);
QDomText data = doc.createTextNode(byteArray.toBase64());
root.appendChild(data);

query.prepare("INSERT INTO pictures (id, pic) VALUES (?, ?)");
query.addBindValue(1);
query.addBindValue(doc.toString());
qDebug() << query.exec();

qDebug() << query.exec("SELECT pic FROM pictures WHERE id = 1");
qDebug() << query.next();

doc.setContent(query.value(0).toString());
root = doc.documentElement();
data = root.firstChild().toText();
byteArray = QByteArray::fromBase64(data.toText().data().toAsci i());

QPixmap pixmap = QPixmap::fromImage(QImage::fromData(byteArray, "jpg"));
qDebug() << pixmap.size();

QLabel label;
label.setPixmap(pixmap);
label.show();

return app.exec();
}