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:
bool test = testImage.load("testOut.jpg","JPG");
int numBytes = testImage.numBytes();
writer.write(testImage);
//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();
}
if(query->first() == true)
{
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
imageWrite.loadFromData(image,"JPG");
imageWrite.save("savedImage.jpg","JPG");
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");
To copy to clipboard, switch view to plain text mode
Bookmarks