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. QCameraViewFinder freezes and QCameraImageCapture stops saving
Qt 6.11 is out! See what's new in the release blog

QCameraViewFinder freezes and QCameraImageCapture stops saving

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 2.2k 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.
  • JRohnerJ Offline
    JRohnerJ Offline
    JRohner
    wrote on last edited by
    #1

    Hi!

    I have a QCameraViewFinder and a QCameraImageCapture. Somewhat randomly the viewfinder just freezes and stops showing a moving image. When this happens QCameraImageCapture also stops saving images to disk.

    Strangely, QCamera also does not seem to fire any signals as my connected slots do nothing. Maybe I made an error? Here is my code (I'm on Qt 5.8.0 and OSX Sierra). The main code is in
    void MainWindow::on_pushButton_clicked()

    Thanks in advance!

    /JC

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        camera = new QCamera(this);
        viewfinder = new QCameraViewfinder(this);
        camera->setViewfinder(viewfinder);
        imageCapture = new QCameraImageCapture(camera);
    
        connect(camera, SIGNAL(locked()), this, SLOT(my_camera_lock()));
        connect(camera, SIGNAL(lockFailed()), this, SLOT(my_lock_failed()));
    
        ui->verticalLayout->addWidget(viewfinder);
        viewfinder->show();
        camera->start();
    
    }
    
    void MainWindow::on_pushButton_clicked()
    {
    
        for(int i = 0; i < 40; i++){
    
            QString path = "/Users/JCRohner/Junk/image" + QString::number(i) + ".jpg";
    
            QApplication::beep();
            camera->searchAndLock();
            while(!imageCapture->isReadyForCapture()){
                QApplication::processEvents(QEventLoop::AllEvents, 100);
            }
            imageCapture->capture(path);
            camera->unlock();
            delayForSecs(3);
        }
    
    }
    
    void MainWindow::my_camera_lock(){
        qDebug()<<"locked";
    }
    
    void MainWindow::my_lock_failed(){
        qDebug()<<"lock failed";
    }
    
    void MainWindow::delayForSecs(int secs){
        QTime dieTime = QTime::currentTime().addSecs(secs);
        while (QTime::currentTime() < dieTime){
            QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
        }
    }
    
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    jsulmJ 1 Reply Last reply
    0
    • JRohnerJ JRohner

      Hi!

      I have a QCameraViewFinder and a QCameraImageCapture. Somewhat randomly the viewfinder just freezes and stops showing a moving image. When this happens QCameraImageCapture also stops saving images to disk.

      Strangely, QCamera also does not seem to fire any signals as my connected slots do nothing. Maybe I made an error? Here is my code (I'm on Qt 5.8.0 and OSX Sierra). The main code is in
      void MainWindow::on_pushButton_clicked()

      Thanks in advance!

      /JC

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          camera = new QCamera(this);
          viewfinder = new QCameraViewfinder(this);
          camera->setViewfinder(viewfinder);
          imageCapture = new QCameraImageCapture(camera);
      
          connect(camera, SIGNAL(locked()), this, SLOT(my_camera_lock()));
          connect(camera, SIGNAL(lockFailed()), this, SLOT(my_lock_failed()));
      
          ui->verticalLayout->addWidget(viewfinder);
          viewfinder->show();
          camera->start();
      
      }
      
      void MainWindow::on_pushButton_clicked()
      {
      
          for(int i = 0; i < 40; i++){
      
              QString path = "/Users/JCRohner/Junk/image" + QString::number(i) + ".jpg";
      
              QApplication::beep();
              camera->searchAndLock();
              while(!imageCapture->isReadyForCapture()){
                  QApplication::processEvents(QEventLoop::AllEvents, 100);
              }
              imageCapture->capture(path);
              camera->unlock();
              delayForSecs(3);
          }
      
      }
      
      void MainWindow::my_camera_lock(){
          qDebug()<<"locked";
      }
      
      void MainWindow::my_lock_failed(){
          qDebug()<<"lock failed";
      }
      
      void MainWindow::delayForSecs(int secs){
          QTime dieTime = QTime::currentTime().addSecs(secs);
          while (QTime::currentTime() < dieTime){
              QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
          }
      }
      
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @JRohner Did you verify that connect() succeeded?

      qDebug() << connect(camera, SIGNAL(locked()), this, SLOT(my_camera_lock()));
      qDebug() << connect(camera, SIGNAL(lockFailed()), this, SLOT(my_lock_failed()));
      

      Also the way your doing it is not really nice: you implement a blocking slot and use QApplication::processEvents to not to block the event loop. You should consider using QTimer.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • JRohnerJ Offline
        JRohnerJ Offline
        JRohner
        wrote on last edited by
        #3

        Thanks for your suggestions @jsulm ! I made the changes you proposed (see code below). The connections succeed as i get "true" and "true" using your code. My slots still do not get called however (nothing more than "true" and "true" gets outputted); and saving images drops out randomly (e.g. the first time i run the app i get all 41 images on disk; the second time i get 3-4 on disk, etc). Strange. I appreciate any suggestions!

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            camera = new QCamera(this);
            viewfinder = new QCameraViewfinder(this);
            camera->setViewfinder(viewfinder);
            imageCapture = new QCameraImageCapture(camera);
        
            connect(camera, SIGNAL(locked()), this, SLOT(my_camera_lock()));
            connect(camera, SIGNAL(lockFailed()), this, SLOT(my_lock_failed()));
            qDebug() << connect(camera, SIGNAL(locked()), this, SLOT(my_camera_lock()));
            qDebug() << connect(camera, SIGNAL(lockFailed()), this, SLOT(my_lock_failed()));
        
            ui->verticalLayout->addWidget(viewfinder);
            viewfinder->show();
            camera->start();
        
            counter = 0;
            myTimer = new QTimer(this);
            connect(myTimer, SIGNAL(timeout()), this, SLOT(doCapture()));
            myTimer->start(4000);
        
        }
        
        void MainWindow::doCapture()
        {
        
            QString path = "/Users/JCRohner/Junk/image" + QString::number(counter) + ".jpg";
            QApplication::beep();
            camera->searchAndLock();
            imageCapture->capture(path);
            camera->unlock();
            counter++;
            if(counter == 40) myTimer->stop();
        
        }
        
        void MainWindow::my_camera_lock(){
            qDebug()<<"locked";
        }
        
        void MainWindow::my_lock_failed(){
            qDebug()<<"lock failed";
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        jsulmJ 1 Reply Last reply
        0
        • JRohnerJ JRohner

          Thanks for your suggestions @jsulm ! I made the changes you proposed (see code below). The connections succeed as i get "true" and "true" using your code. My slots still do not get called however (nothing more than "true" and "true" gets outputted); and saving images drops out randomly (e.g. the first time i run the app i get all 41 images on disk; the second time i get 3-4 on disk, etc). Strange. I appreciate any suggestions!

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          
          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
              camera = new QCamera(this);
              viewfinder = new QCameraViewfinder(this);
              camera->setViewfinder(viewfinder);
              imageCapture = new QCameraImageCapture(camera);
          
              connect(camera, SIGNAL(locked()), this, SLOT(my_camera_lock()));
              connect(camera, SIGNAL(lockFailed()), this, SLOT(my_lock_failed()));
              qDebug() << connect(camera, SIGNAL(locked()), this, SLOT(my_camera_lock()));
              qDebug() << connect(camera, SIGNAL(lockFailed()), this, SLOT(my_lock_failed()));
          
              ui->verticalLayout->addWidget(viewfinder);
              viewfinder->show();
              camera->start();
          
              counter = 0;
              myTimer = new QTimer(this);
              connect(myTimer, SIGNAL(timeout()), this, SLOT(doCapture()));
              myTimer->start(4000);
          
          }
          
          void MainWindow::doCapture()
          {
          
              QString path = "/Users/JCRohner/Junk/image" + QString::number(counter) + ".jpg";
              QApplication::beep();
              camera->searchAndLock();
              imageCapture->capture(path);
              camera->unlock();
              counter++;
              if(counter == 40) myTimer->stop();
          
          }
          
          void MainWindow::my_camera_lock(){
              qDebug()<<"locked";
          }
          
          void MainWindow::my_lock_failed(){
              qDebug()<<"lock failed";
          }
          
          MainWindow::~MainWindow()
          {
              delete ui;
          }
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JRohner You need to somehow find out where it actually freezes. You can add some qDebug() << "..." debug output for that.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

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