Saving QCameraImageCapture as "PNG"
-
Hello everyone, this is my first time ever asking someone on a forum for help (which basically means i'm deseperate), i'm also very new to Qt. So what i want to do with my application is take pictures and save them as "PNG" in a directory, when i try it everything goes well except for the format which remains "JPEG", so i've looked almost everywhere and found some interesting ways, among them is the line that starts from " QObject::connect(_image_capture, &QCameraImageCapture::imageCaptured, [=] (int id, QImage img)" but the problem when i build my project i get this error saying "no matching function for call to ‘MainWindow::connect(QScopedPointer<QCameraImageCapture>&, void (QCameraImageCapture::)(int, const QImage&), MainWindow::MainWindow(QWidget)::<lambda(int, QImage)>)’ });** ", can someone please help me out with this ? if not, how can i save the captured images in "PNG" and not "JPEG" Thank you
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"); timer = new QTimer(this); _central_widget = new QWidget(); setCentralWidget(_central_widget); _setup_ui(); _setup_camera_devices(); set_camera(QCameraInfo::defaultCamera()); connect(_turn_camera_off, &QPushButton::clicked, [this]{_camera.data()->stop(); timer->stop();}); connect(_turn_camera_on, &QPushButton::clicked, [this]{_camera.data()->start();}); connect(timer, SIGNAL(timeout()), this, SLOT(timerfunction())); connect(_take_image_button, &QPushButton::clicked, [this]{ _image_capture.data()->capture(); timer->start(60000); }); QObject::connect(_image_capture, &QCameraImageCapture::imageCaptured, [=] (int id, QImage img){ QByteArray ba; QBuffer buffer(&ba); buffer.open(QIODevice::WriteOnly); img.save(&buffer, "PNG"); });
-
@JonB thank u, i think i have found the solution for this problem, here's the 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"); timer = new QTimer(this); _central_widget = new QWidget(); setCentralWidget(_central_widget); _setup_ui(); _setup_camera_devices(); set_camera(QCameraInfo::defaultCamera()); _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"); }); connect(_turn_camera_off, &QPushButton::clicked, [this]{_camera.data()->stop(); timer->stop();}); connect(_turn_camera_on, &QPushButton::clicked, [this]{_camera.data()->start();}); connect(timer, SIGNAL(timeout()), this, SLOT(timerfunction())); connect(_take_image_button, &QPushButton::clicked, [this]{ _image_capture.data()->capture(); }); }
-
@hamzaelazizi
You are only saving your image, as aPNG
, into a (local variable) which is aQBuffer buffer
, and that puts the bytes into a (local variable) which is aQByteArray ba;
. I don't think your disk file (JPEG
) is being altered by any code you have shown.If you want to save it to an external file you need to do so via a
QFile
, or the dedicated bool QImage::save(const QString &fileName, const char *format = nullptr, int quality = -1) const.The call you using is bool QImage::save(QIODevice *device, const char *format = nullptr, int quality = -1) const, which only writes it into memory.
-
This post is deleted!
-
@JonB First of all, i would like to thank u for the time u spent writing this message. Unfortunately, i don't really get how i'm going to do that ! the point is always to get to save my pictures as "PNG" and it would be even better if i could choose also where to save it ! Thank you sir !
-
@hamzaelazizi said in Saving QCameraImageCapture as "PNG":
if i could choose also where to save it
Well, that's exactly what you do, by specifying the filepath:
img.save("/some/path/to/a/directory/somefilename.png");
-
While JonB's answer is correct there is a caveat in this case, the second argument you get here is not meant to be saved down, that is just a preview not the actual image.
What you should do is:
_image_capture->setCaptureDestination(QCameraImageCapture::CaptureToBuffer);
in the constructor, to prevent saving it as a JPGQObject::connect(_image_capture, &QCameraImageCapture::imageAvailable, [] (int id, const QVideoFrame &img){img->image().save("ImageName.png","PNG");};
to save it as a png
-
@JonB thank u, i think i have found the solution for this problem, here's the 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"); timer = new QTimer(this); _central_widget = new QWidget(); setCentralWidget(_central_widget); _setup_ui(); _setup_camera_devices(); set_camera(QCameraInfo::defaultCamera()); _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"); }); connect(_turn_camera_off, &QPushButton::clicked, [this]{_camera.data()->stop(); timer->stop();}); connect(_turn_camera_on, &QPushButton::clicked, [this]{_camera.data()->start();}); connect(timer, SIGNAL(timeout()), this, SLOT(timerfunction())); connect(_take_image_button, &QPushButton::clicked, [this]{ _image_capture.data()->capture(); }); }
-
@hamzaelazizi said in Saving QCameraImageCapture as "PNG":
path = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + "/" + fileName;
Yep, perfect! :)