Results 1 to 3 of 3

Thread: Putting and getting images from MySQL database?

  1. #1
    Join Date
    May 2006
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Putting and getting images from MySQL database?

    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:

    Qt Code:
    1. QVariant id(0);
    2. QByteArray bytes;
    3. QBuffer buffer(&bytes);
    4. QImage testImage;
    5. QImageWriter writer(&buffer, "JPG");
    6.  
    7. bool test = testImage.load("testOut.jpg","JPG");
    8. int numBytes = testImage.numBytes();
    9. writer.write(testImage);
    10. QByteArray data = buffer.data();
    11.  
    12. //Put image in database as byteArray
    13. if(!query->prepare("INSERT into testBlob (id,image) VALUES (?,?)"))
    14. qDebug() << query->lastError().text();
    15.  
    16. query->addBindValue(id);
    17. query->addBindValue(data);
    18.  
    19. if(!query->exec())
    20. {
    21. qDebug() << query->lastError().text();
    22. }
    23.  
    24. //Now get image back out of database
    25. if(!query->prepare("SELECT * FROM testBlob where id = 11 "))
    26. qDebug() << query->lastError().text();
    27.  
    28. if(!query->exec())
    29. {
    30. qDebug() << query->lastError().text();
    31. }
    32.  
    33. QByteArray image;
    34. if(query->first() == true)
    35. {
    36. QSqlRecord rec = query->record();
    37. int id = query->value(rec.indexOf("id")).toInt();
    38. image = query->value(rec.indexOf("image")).toByteArray();
    39. }
    40.  
    41. //Write retrieved image out to disk to compare to original
    42. QImage imageWrite;
    43. imageWrite.loadFromData(image,"JPG");
    44. imageWrite.save("savedImage.jpg","JPG");
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Posts
    73
    Thanks
    16
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Putting and getting images from MySQL database?

    What about the compression-levelof the jpeg that can be set for QImage.save() as the quality parameter?

  3. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Putting and getting images from MySQL database?

    I write zip && jpg to db mysql... longblob fields max 2GB
    https://qt-webdav.svn.sourceforge.ne...zip/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

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.