PDA

View Full Version : QImage to text file



OverTheOCean
2nd August 2010, 06:11
Guys,

quick question, is that possible to dump a Qimage in a text file so l can same numerical data and images in a same text file ?

thanks,

Michael

aamer4yu
2nd August 2010, 06:28
You can always write an image to a file. It depends on what format you want to write.
text files contains text. Applications will open text files as text files.
If you want to embedd image with text, you can save in doc or pdf format. Qt provides support to write in pdf format. You can opt for that.

Other easy way -
Save QImage as image file...open in paint , copy image and paste in MS Word. ;)

ChrisW67
2nd August 2010, 10:36
I think the OP wants to save an image and some related, human readable data in the same text file and have the image recoverable even if the user edits the extra data. In this case base64, a start/end marker pair, and a big warning that the user should not edit between the markers might be the best bet.

tbscope
2nd August 2010, 12:12
Just a note that base64 is not efficient for binary data.
I would go for a gzipped folder containing the images as jpg or png and some xml or other config files.

spirit
2nd August 2010, 12:37
you can save your image in QByteArray and then save a QByteArray into file.
take a look at QImage::save.

OverTheOCean
3rd August 2010, 05:17
thanks guy, got it to work.
Save to text file:
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
QTextStream out(&file);

Image_snapshot.save(&buffer, "PNG");
out << buffer.data().toHex();


load from file:
if(line.contains("#BEGIN_Image"))
{
line = in.readLine();
ba.clear();
while (!line.contains("#END_Image")) {
line = in.readLine();
QTextStream(&ba) << line;
}
QByteArray readCompressed = QByteArray::fromHex(Buffer.toAscii());
if(!WebCam_snapshot.loadFromData(readCompressed))
qDebug() << "fail to load Qbytrearray";

//test image
if(!WebCam_snapshot.save("test.bmp"))
qDebug() << "fail to save image from Qbytrearray";

}