Please nominate your Qt Champions for 2021!

pixmap saving image with random size

  • 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

    void Camera::processCapturedImage(int requestId, const QImage& img)
    QImage scaledImage = img.scaled(ui->viewfinder->size(),
     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();
     //scale 3x4 pic
     pixmap.scaled(QSize(354, 472),  Qt::KeepAspectRatio);;
     emit enteredText(QString("Imagem Salva!"));
    // Display captured image for 4 seconds.
    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

  • @tham solved, thank you

Log in to reply