mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-10-01 02:35:48 -04:00
Refactor and delete repeated code
This commit is contained in:
parent
be7c6a966f
commit
44ffdb97c5
@ -43,30 +43,19 @@ public slots:
|
|||||||
QString localPath = url.toLocalFile();
|
QString localPath = url.toLocalFile();
|
||||||
|
|
||||||
qDebug() << "imageToBase64() local path:" << localPath ;
|
qDebug() << "imageToBase64() local path:" << localPath ;
|
||||||
|
QImage image= getImage (localPath);
|
||||||
// Read the image
|
|
||||||
QImageReader reader;
|
|
||||||
reader.setFileName(localPath);
|
|
||||||
QImage image = reader.read();
|
|
||||||
|
|
||||||
image = image.scaled(96,96,Qt::KeepAspectRatio,Qt::SmoothTransformation);
|
image = image.scaled(96,96,Qt::KeepAspectRatio,Qt::SmoothTransformation);
|
||||||
|
|
||||||
// Transform image into PNG format
|
qDebug() << "imageToBase64() encoding" ;
|
||||||
QByteArray ba;
|
|
||||||
QBuffer buffer( &ba );
|
|
||||||
buffer.open( QIODevice::WriteOnly );
|
|
||||||
image.save( &buffer, "png" );
|
|
||||||
|
|
||||||
// Get Based 64 image string
|
return imageToB64(image);
|
||||||
QString encoded = QString(ba.toBase64());
|
|
||||||
|
|
||||||
qDebug() << "imageToBase64() encoded" ;
|
|
||||||
|
|
||||||
return encoded;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static QString base64FaceAvatarGenerator (QVariantList onloads, int size)
|
static QString b64AvatarGen (QVariantList onloads, int size)
|
||||||
{
|
{
|
||||||
|
qDebug() << "b64AvatarGen(): Generating face Avatar from";
|
||||||
|
|
||||||
QImage result(size, size, QImage::Format_ARGB32_Premultiplied);
|
QImage result(size, size, QImage::Format_ARGB32_Premultiplied);
|
||||||
QPainter painter(&result);
|
QPainter painter(&result);
|
||||||
|
|
||||||
@ -74,27 +63,32 @@ public slots:
|
|||||||
int counter = 0;
|
int counter = 0;
|
||||||
for (QVariantList::iterator j = onloads.begin(); j != onloads.end(); j++)
|
for (QVariantList::iterator j = onloads.begin(); j != onloads.end(); j++)
|
||||||
{
|
{
|
||||||
QString path = ":/"+(*j).toString();
|
QImage image = getImage (":/"+(*j).toString());
|
||||||
QImageReader reader;
|
|
||||||
reader.setFileName(path);
|
|
||||||
QImage image = reader.read();
|
|
||||||
painter.drawImage(0, 0, image); // xi, yi is the position for imagei
|
painter.drawImage(0, 0, image); // xi, yi is the position for imagei
|
||||||
qDebug() << "Generating face Avatar from: " << (*j).toString(); // Print QVariant
|
|
||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
painter.end();
|
painter.end();
|
||||||
|
|
||||||
|
return imageToB64(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
static QImage getImage (QString const& path)
|
||||||
|
{
|
||||||
|
QImageReader reader;
|
||||||
|
reader.setFileName(path);
|
||||||
|
return reader.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
static QString imageToB64 (QImage image)
|
||||||
|
{
|
||||||
// Transform image into PNG format
|
// Transform image into PNG format
|
||||||
QByteArray ba;
|
QByteArray ba;
|
||||||
QBuffer buffer( &ba );
|
QBuffer buffer( &ba );
|
||||||
buffer.open( QIODevice::WriteOnly );
|
buffer.open( QIODevice::WriteOnly );
|
||||||
result.save( &buffer, "png" );
|
image.save( &buffer, "png" );
|
||||||
|
|
||||||
// Get Based 64 image string
|
// Get Based 64 image string
|
||||||
QString encoded = QString(ba.toBase64());
|
return QString(ba.toBase64());
|
||||||
|
|
||||||
return encoded;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user