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

468 views Asked by At

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();});}
2

There are 2 answers

0
Hamza On BEST ANSWER

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");
             
            });
0
Öö Tiib On

You should get QImage in some point of your work. It has save member. Example from cited documentation:

QImage image;
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG"); // writes image into ba in PNG format

So the usage in context of QCameraImageCapture goes something like that:

QObject::connect(cap, &QCameraImageCapture::imageCaptured, [=] (int id, QImage img) {
    QByteArray ba;
    QBuffer buffer(&ba);
    buffer.open(QIODevice::WriteOnly);
    img.save(&buffer, "PNG");
});