Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Saving QCameraImageCapture as "PNG"

Saving QCameraImageCapture as "PNG"

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • hamzaelaziziH Offline
    hamzaelaziziH Offline
    hamzaelazizi
    wrote on last edited by hamzaelazizi
    #1

    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");
        });
    
    JonBJ 1 Reply Last reply
    0
    • JonBJ JonB

      @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");
      
      hamzaelaziziH Offline
      hamzaelaziziH Offline
      hamzaelazizi
      wrote on last edited by
      #9

      @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();
              });
      
      }
      
      JonBJ 1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #2

        What's _image_capture what type is it?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        hamzaelaziziH 1 Reply Last reply
        0
        • hamzaelaziziH hamzaelazizi

          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");
              });
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #3

          @hamzaelazizi
          You are only saving your image, as a PNG, into a (local variable) which is a QBuffer buffer, and that puts the bytes into a (local variable) which is a QByteArray 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.

          hamzaelaziziH 1 Reply Last reply
          2
          • hamzaelaziziH Offline
            hamzaelaziziH Offline
            hamzaelazizi
            wrote on last edited by hamzaelazizi
            #4
            This post is deleted!
            1 Reply Last reply
            0
            • JonBJ JonB

              @hamzaelazizi
              You are only saving your image, as a PNG, into a (local variable) which is a QBuffer buffer, and that puts the bytes into a (local variable) which is a QByteArray 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.

              hamzaelaziziH Offline
              hamzaelaziziH Offline
              hamzaelazizi
              wrote on last edited by
              #5

              @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 !

              JonBJ 1 Reply Last reply
              0
              • VRoninV VRonin

                What's _image_capture what type is it?

                hamzaelaziziH Offline
                hamzaelaziziH Offline
                hamzaelazizi
                wrote on last edited by
                #6

                @VRonin in the file "mainwindow.h" _image_capture is like this :QScopedPointer<QCameraImageCapture>_image_capture;

                1 Reply Last reply
                0
                • hamzaelaziziH hamzaelazizi

                  @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 !

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #7

                  @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");
                  
                  hamzaelaziziH 1 Reply Last reply
                  3
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #8

                    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 JPG
                    • QObject::connect(_image_capture, &QCameraImageCapture::imageAvailable, [] (int id, const QVideoFrame &img){img->image().save("ImageName.png","PNG");}; to save it as a png

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    1
                    • JonBJ JonB

                      @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");
                      
                      hamzaelaziziH Offline
                      hamzaelaziziH Offline
                      hamzaelazizi
                      wrote on last edited by
                      #9

                      @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();
                              });
                      
                      }
                      
                      JonBJ 1 Reply Last reply
                      0
                      • hamzaelaziziH hamzaelazizi

                        @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();
                                });
                        
                        }
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #10

                        @hamzaelazizi said in Saving QCameraImageCapture as "PNG":

                        path = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + "/" + fileName;

                        Yep, perfect! :)

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved