2017-07-10 13:50:11 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QDebug>
|
|
|
|
|
2017-07-18 10:16:35 -04:00
|
|
|
#include <QFile>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QImageReader>
|
|
|
|
#include <QBuffer>
|
|
|
|
|
2017-09-07 14:30:08 -04:00
|
|
|
#include "qpainter.h"
|
|
|
|
|
2017-07-18 12:09:35 -04:00
|
|
|
|
2017-07-10 13:50:11 -04:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
# include <QtAndroid>
|
|
|
|
# include <QtAndroidExtras/QAndroidJniObject>
|
|
|
|
#endif // __ANDROID__
|
|
|
|
|
|
|
|
struct AndroidImagePicker : QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
|
|
|
|
static void openPicker()
|
|
|
|
{
|
|
|
|
qDebug() << "Starting image picker intent";
|
|
|
|
|
2017-07-18 10:16:35 -04:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
QtAndroid::androidActivity().callMethod<void>(
|
|
|
|
"openImagePicker",
|
|
|
|
"()V" );
|
|
|
|
#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();
|
|
|
|
|
2017-07-18 12:09:35 -04:00
|
|
|
qDebug() << "imageToBase64() local path:" << localPath ;
|
2017-09-11 10:52:15 -04:00
|
|
|
QImage image= getImage (localPath);
|
2017-07-18 10:16:35 -04:00
|
|
|
|
2017-07-18 12:09:35 -04:00
|
|
|
image = image.scaled(96,96,Qt::KeepAspectRatio,Qt::SmoothTransformation);
|
|
|
|
|
2017-09-11 10:52:15 -04:00
|
|
|
qDebug() << "imageToBase64() encoding" ;
|
2017-07-18 10:16:35 -04:00
|
|
|
|
2017-09-11 10:52:15 -04:00
|
|
|
return imageToB64(image);
|
2017-09-07 14:30:08 -04:00
|
|
|
}
|
|
|
|
|
2017-09-11 10:52:15 -04:00
|
|
|
static QString b64AvatarGen (QVariantList onloads, int size)
|
2017-09-07 14:30:08 -04:00
|
|
|
{
|
2018-01-25 10:20:25 -05:00
|
|
|
qDebug() << "Generating hash face avatar";
|
2017-09-11 10:52:15 -04:00
|
|
|
|
2017-09-07 14:30:08 -04:00
|
|
|
QImage result(size, size, QImage::Format_ARGB32_Premultiplied);
|
|
|
|
QPainter painter(&result);
|
|
|
|
|
|
|
|
|
|
|
|
int counter = 0;
|
2018-01-25 10:20:25 -05:00
|
|
|
for(QVariantList::iterator j = onloads.begin(); j != onloads.end(); ++j)
|
2017-09-07 14:30:08 -04:00
|
|
|
{
|
2017-09-11 10:52:15 -04:00
|
|
|
QImage image = getImage (":/"+(*j).toString());
|
2017-09-07 14:30:08 -04:00
|
|
|
painter.drawImage(0, 0, image); // xi, yi is the position for imagei
|
2018-01-25 10:20:25 -05:00
|
|
|
++counter;
|
2017-09-07 14:30:08 -04:00
|
|
|
}
|
|
|
|
painter.end();
|
|
|
|
|
2017-09-11 10:52:15 -04:00
|
|
|
return imageToB64(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static QImage getImage (QString const& path)
|
|
|
|
{
|
|
|
|
QImageReader reader;
|
|
|
|
reader.setFileName(path);
|
|
|
|
return reader.read();
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString imageToB64 (QImage image)
|
|
|
|
{
|
2017-09-07 14:30:08 -04:00
|
|
|
// Transform image into PNG format
|
|
|
|
QByteArray ba;
|
|
|
|
QBuffer buffer( &ba );
|
|
|
|
buffer.open( QIODevice::WriteOnly );
|
2017-09-11 10:52:15 -04:00
|
|
|
image.save( &buffer, "png" );
|
2017-09-07 14:30:08 -04:00
|
|
|
|
|
|
|
// Get Based 64 image string
|
2017-09-11 10:52:15 -04:00
|
|
|
return QString(ba.toBase64());
|
2017-07-10 13:50:11 -04:00
|
|
|
}
|
2017-09-07 14:30:08 -04:00
|
|
|
|
|
|
|
|
2017-07-10 13:50:11 -04:00
|
|
|
};
|