Auto resize image to fit into 34kB

This commit is contained in:
hunbernd 2019-12-29 16:14:31 +01:00
parent 419a9a520c
commit 9381b744aa
2 changed files with 27 additions and 16 deletions

View file

@ -20,6 +20,7 @@
#include <QBuffer> #include <QBuffer>
#include <QMessageBox> #include <QMessageBox>
#include <QByteArray>
#include "PostedCreatePostDialog.h" #include "PostedCreatePostDialog.h"
#include "ui_PostedCreatePostDialog.h" #include "ui_PostedCreatePostDialog.h"
@ -34,6 +35,8 @@
#include <iostream> #include <iostream>
#include <util/imageutil.h>
PostedCreatePostDialog::PostedCreatePostDialog(TokenQueue* tokenQ, RsPosted *posted, const RsGxsGroupId& grpId, QWidget *parent): PostedCreatePostDialog::PostedCreatePostDialog(TokenQueue* tokenQ, RsPosted *posted, const RsGxsGroupId& grpId, QWidget *parent):
QDialog(parent, Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint), QDialog(parent, Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint),
mTokenQueue(tokenQ), mPosted(posted), mGrpId(grpId), mTokenQueue(tokenQ), mPosted(posted), mGrpId(grpId),
@ -91,17 +94,11 @@ void PostedCreatePostDialog::createPost()
post.mMeta.mAuthorId = authorId; post.mMeta.mAuthorId = authorId;
post.mMeta.mMsgName = std::string(ui->titleEdit->text().toUtf8()); post.mMeta.mMsgName = std::string(ui->titleEdit->text().toUtf8());
QByteArray ba;
QBuffer buffer(&ba);
if(!picture.isNull()) if(imagebytes.size() > 0)
{ {
// send posted image // send posted image
post.mImage.copy((uint8_t *) imagebytes.data(), imagebytes.size());
buffer.open(QIODevice::WriteOnly);
picture.save(&buffer, "PNG"); // writes image into ba in PNG format
post.mImage.copy((uint8_t *) ba.data(), ba.size());
} }
if(ui->titleEdit->text().isEmpty()) { if(ui->titleEdit->text().isEmpty()) {
@ -119,15 +116,27 @@ void PostedCreatePostDialog::createPost()
void PostedCreatePostDialog::addPicture() void PostedCreatePostDialog::addPicture()
{ {
QPixmap img = misc::getOpenThumbnailedPicture(this, tr("Load thumbnail picture"), 800, 600); imagefilename = "";
imagebytes.clear();
if (img.isNull()) // select a picture file
return; if (misc::getOpenFileName(window(), RshareSettings::LASTDIR_IMAGES, tr("Load Picture File"), "Pictures (*.png *.xpm *.jpg *.jpeg *.gif *.webp )", imagefilename)) {
QString encodedImage;
int maxMessageSize = 34000; //34 kB
QImage image;
if (image.load (imagefilename) == false) {
fprintf (stderr, "RsHtml::makeEmbeddedImage() - image \"%s\" can't be load\n", imagefilename.toLatin1().constData());
return;
}
picture = img; QImage opt;
if(ImageUtil::optimizeSizeBytes(imagebytes, image, opt, 800*600, maxMessageSize)) {
// to show the selected ui->imageLabel->setPixmap(QPixmap::fromImage(opt));
ui->imageLabel->setPixmap(picture); } else {
imagefilename = "";
imagebytes.clear();
}
}
} }
void PostedCreatePostDialog::on_postButton_clicked() void PostedCreatePostDialog::on_postButton_clicked()

View file

@ -43,7 +43,9 @@ public:
explicit PostedCreatePostDialog(TokenQueue* tokenQ, RsPosted* posted, const RsGxsGroupId& grpId, QWidget *parent = 0); explicit PostedCreatePostDialog(TokenQueue* tokenQ, RsPosted* posted, const RsGxsGroupId& grpId, QWidget *parent = 0);
~PostedCreatePostDialog(); ~PostedCreatePostDialog();
QPixmap picture; private:
QString imagefilename;
QByteArray imagebytes;
private slots: private slots:
void createPost(); void createPost();