RetroShare/retroshare-qml-app/src/androidimagepicker.h

102 lines
2.1 KiB
C
Raw Normal View History

#pragma once
#include <QObject>
#include <QDebug>
#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
#ifdef __ANDROID__
# include <QtAndroid>
# include <QtAndroidExtras/QAndroidJniObject>
#endif // __ANDROID__
struct AndroidImagePicker : QObject
{
Q_OBJECT
public slots:
static void openPicker()
{
qDebug() << "Starting image picker intent";
#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 ;
// Read the image
QImageReader reader;
reader.setFileName(localPath);
QImage image = reader.read();
2017-07-18 12:09:35 -04:00
image = image.scaled(96,96,Qt::KeepAspectRatio,Qt::SmoothTransformation);
// Transform image into PNG format
QByteArray ba;
QBuffer buffer( &ba );
buffer.open( QIODevice::WriteOnly );
2017-07-18 12:09:35 -04:00
image.save( &buffer, "png" );
// Get Based 64 image string
QString encoded = QString(ba.toBase64());
2017-07-18 13:27:56 -04:00
qDebug() << "imageToBase64() encoded" ;
2017-09-07 14:30:08 -04:00
return encoded;
}
static QString faceImage (QVariantList onloads, int size)
{
QImage result(size, size, QImage::Format_ARGB32_Premultiplied);
QPainter painter(&result);
int counter = 0;
for (QVariantList::iterator j = onloads.begin(); j != onloads.end(); j++)
{
2017-09-11 10:27:16 -04:00
QString path = ":/"+(*j).toString();
2017-09-07 14:30:08 -04:00
QImageReader reader;
reader.setFileName(path);
QImage image = reader.read();
painter.drawImage(0, 0, image); // xi, yi is the position for imagei
2017-09-11 10:42:29 -04:00
qDebug() << "Generating face Avatar from: " << (*j).toString(); // Print QVariant
2017-09-07 14:30:08 -04:00
counter++;
}
painter.end();
// Transform image into PNG format
QByteArray ba;
QBuffer buffer( &ba );
buffer.open( QIODevice::WriteOnly );
result.save( &buffer, "png" );
// Get Based 64 image string
QString encoded = QString(ba.toBase64());
return encoded;
}
2017-09-07 14:30:08 -04:00
};