c++qtqt-creatorcross-compilingqcamera

Save a captured image using QCameraImageCapture::capture() in PNG Format


this is literally my first question in a forum. So I'm a Qt newbie and I'm stuck at this little detail. I'm creating this application that takes pictures and saves them, but the issue is that it saves then in a "JPEG" format and i need them in "PNG" or "GIF" or "tiff" and i I've tried a lot of stuff but nothing worked, so here's my code :

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
_camera_view = new QCameraViewfinder();
_take_image_button = new QPushButton("Take Image");
_turn_camera_off = new QPushButton("Turn Off");
_turn_camera_on= new QPushButton("Turn On");

_central_widget = new QWidget();
setCentralWidget(_central_widget);

_setup_ui();
_setup_camera_devices();
set_camera(QCameraInfo::defaultCamera());
connect(_take_image_button, &QPushButton::clicked, [this]{
      _image_capture.data()->capture();});

connect(_turn_camera_off, &QPushButton::clicked, [this]{_camera.data()->stop();});
connect(_turn_camera_on, &QPushButton::clicked, [this]{_camera.data()->start();});}

Solution

  • For anyone that might encounter this problem in the future, here's the solution i've found :

        _image_capture->setCaptureDestination(QCameraImageCapture::CaptureToBuffer);
    
     QObject::connect(_image_capture.data(), &QCameraImageCapture::imageCaptured, [=] (int id, QImage img) {
    
               fileName = "image.png";
               path = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + "/" + fileName;
               img.save(path, "PNG");
                 
                });