Function to convert an image to a png base64 format

This commit is contained in:
Angela Mazzurco 2017-07-18 16:16:35 +02:00
parent 0494dd7516
commit c570aae9f7

View File

@ -3,6 +3,12 @@
#include <QObject> #include <QObject>
#include <QDebug> #include <QDebug>
#include <QFile>
#include <QUrl>
#include <QImage>
#include <QImageReader>
#include <QBuffer>
#ifdef __ANDROID__ #ifdef __ANDROID__
# include <QtAndroid> # include <QtAndroid>
# include <QtAndroidExtras/QAndroidJniObject> # include <QtAndroidExtras/QAndroidJniObject>
@ -23,5 +29,33 @@ public slots:
"openImagePicker", "openImagePicker",
"()V" ); "()V" );
#endif // __ANDROID__ #endif // __ANDROID__
}
// Used to convert a given image path into a png base64 string
static QString imageToBase64 (QString const& path)
{
// Get local path from uri
QUrl url (path);
QString localPath = url.toLocalFile();
// Read the image
QImageReader reader;
reader.setFileName(localPath);
QImage image = reader.read();
// Transform image into PNG format
QByteArray ba;
QBuffer buffer( &ba );
buffer.open( QIODevice::WriteOnly );
image.save( &buffer, "PNG" );
// Get Based 64 image string
QString encoded = QString(ba.toBase64());
qDebug() << "imageToBase64() " ;
return encoded;
} }
}; };