PDA

View Full Version : Putting and getting images from MySQL database?



allensr
11th May 2007, 17:13
I have a simple little Qt/MySql program that puts a JPG image in a MySQL database and then retrieves it. It looks like it "works", but when I save out the image that I retrieve, the size of the two files (the one I read from disk originally and the one I save out) are different sizes (original is ~69Kb and the saved outr is ~14Kb). Now I can't tell any difference in the images in an image viewer but I cant tell why the sizes are different. But if I do this method for a .bmp file the sizes are the same. Anyone have an explanation?

The database only has 2 columns
id - integer
image - mediumblob

Here is the code:




QVariant id(0);
QByteArray bytes;
QBuffer buffer(&bytes);
QImage testImage;
QImageWriter writer(&buffer, "JPG");

bool test = testImage.load("testOut.jpg","JPG");
int numBytes = testImage.numBytes();
writer.write(testImage);
QByteArray data = buffer.data();

//Put image in database as byteArray
if(!query->prepare("INSERT into testBlob (id,image) VALUES (?,?)"))
qDebug() << query->lastError().text();

query->addBindValue(id);
query->addBindValue(data);

if(!query->exec())
{
qDebug() << query->lastError().text();
}

//Now get image back out of database
if(!query->prepare("SELECT * FROM testBlob where id = 11 "))
qDebug() << query->lastError().text();

if(!query->exec())
{
qDebug() << query->lastError().text();
}

QByteArray image;
if(query->first() == true)
{
QSqlRecord rec = query->record();
int id = query->value(rec.indexOf("id")).toInt();
image = query->value(rec.indexOf("image")).toByteArray();
}

//Write retrieved image out to disk to compare to original
QImage imageWrite;
imageWrite.loadFromData(image,"JPG");
imageWrite.save("savedImage.jpg","JPG");

jcr
12th May 2007, 00:27
What about the compression-levelof the jpeg that can be set for QImage.save() as the quality parameter?

patrik08
13th May 2007, 21:47
I write zip && jpg to db mysql... longblob fields max 2GB
https://qt-webdav.svn.sourceforge.net/svnroot/qt-webdav/console_zip_unzip/README.txt

whitout
imageWrite.loadFromData(image,"JPG"); QImage
and image stay exact the same as on disc!

important is zip or gunzip to faster write an read on dbs...


and i not lost data from jpg exif or other ...

link http://en.wikipedia.org/wiki/Exif