How to get md5 of an image in QML?
-
I have image thumbnails stored as their md5 hash eg. fc00082dcdb3c5924f96e0b35773e182.jpg.
This way I can access them even if they are moved in the system.
Then in QML when I load an image/images I want to look up their md5 hash and then load the appropriate thumbnail.
So far I have had no luck. I have tried:Qt.md5(fileUrl)
and
Qt.md5(imageid.source)
But they are not correct. The images are displayed in gridview btw.
Any ideas? -
@Nineswiss said in How to get md5 of an image in QML?:
Then in QML when I load an image/images I want to look up their md5 hash and then load the appropriate thumbnail.
Then you have to load the real image data, calculate the md5 hash sum so you know the filename.
-
@Christian-Ehrlicher So that means doing it in C++ I would imagine?
-
@Nineswiss It maybe also works with qml but I would guess it's faster and easier with c++ - QFile and QCryptoGraphicHash are your friends.
-
@Christian-Ehrlicher Damn, I was hoping to find away to avoid C++ for this as I am very new to it. Oh well!
-
SOLVED!
getmd5.h#ifndef GETMD5_H #define GETMD5_H #include <QFile> #include <QCryptographicHash> class GetMd5 : public QObject { Q_OBJECT public slots: QString getMd5(const QString &filepath) { QFile file(filepath); file.open(QIODevice::ReadOnly); QCryptographicHash md5(QCryptographicHash::Md5); while(!file.atEnd()) { md5.addData(file.read(8192)); } QString Md5Str = md5.result().toHex(); file.close(); return Md5Str; } public: GetMd5() {} }; #endif // GETMD5_H
myqml.qml
getMd5.getMd5(fileUrl) //Minus 'file//
Thanks for the help @Christian-Ehrlicher !
-
I would increase the buffer size a little bit - maybe 1MB or so. Otherwise there are a lot of small read operations for nothing.