pixmap saving image with random size
Solved
General and Desktop
-
I am using the following code to take a pic from a webcam but the pixmap is saving the image with random size, and I would like to save it with 354, 472, does anyone know what's going on with pixamap.scaled?
I am following this example https://doc.qt.io/qt-5/qtmultimedia-multimediawidgets-camera-example.html
void Camera::processCapturedImage(int requestId, const QImage& img) { Q_UNUSED(requestId); QImage scaledImage = img.scaled(ui->viewfinder->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); ui->lastImagePreviewLabel->setPixmap(QPixmap::fromImage(scaledImage)); QPixmap pixmap(QPixmap::fromImage(scaledImage)); qDebug() << QCoreApplication::applicationDirPath(); //create /image folder QString folder = QCoreApplication::applicationDirPath() + "/images"; QString image = folder + "/new.jpg"; qDebug() << QDir(folder).exists(); if(!QDir(folder).exists()) { QDir().mkdir(folder); } // //scale 3x4 pic pixmap.scaled(QSize(354, 472), Qt::KeepAspectRatio); pixmap.save(image); emit enteredText(QString("Imagem Salva!")); this->close(); // Display captured image for 4 seconds. displayCapturedImage(); QTimer::singleShot(4000, this, SLOT(displayViewfinder())); }
-
I guess Qt::KeepAspectRatio is the reason, change it to Qt::IgnoreAspectRatio may solve this problem
//according to document, IgnoreAspectRatio is default value //you do not need to specify it if you like pixmap.scaled(QSize(354, 472), Qt::IgnoreAspectRatio);
By the way, why create another pixmap?Why not just use the QImage(scaledImage) to save your picture?Thanks