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. A camera problem.
Forum Updated to NodeBB v4.3 + New Features

A camera problem.

Scheduled Pinned Locked Moved Unsolved General and Desktop
91 Posts 7 Posters 18.9k Views 2 Watching
  • 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.
  • jsulmJ jsulm

    @jenya7 No, this belongs to C++ basics: if m_camera is a pointer then you have to use -> if it is not a pointer then you have to use dot. So, what is m_camera in YOUR code?

    J Offline
    J Offline
    jenya7
    wrote on last edited by jenya7
    #35

    @jsulm said in A camera problem.:

    @jenya7 No, this belongs to C++ basics: if m_camera is a pointer then you have to use -> if it is not a pointer then you have to use dot. So, what is m_camera in YOUR code?

    in camera.h

    class Camera : public QMainWindow
    {
        Q_OBJECT
    
    public:
        Camera(QObject *parent = nullptr);
    
         void takeImage();
    
    private slots:
        void setCamera(const QCameraInfo &cameraInfo);
    
        void startCamera();
        void stopCamera();
    
        void record();
        void pause();
        void stop();
        void setMuted(bool);
    
        void toggleLock();
        void displayCaptureError(int, QCameraImageCapture::Error, const QString &errorString);
    
        void configureCaptureSettings();
        void configureVideoSettings();
        void configureImageSettings();
        void displayRecorderError();
        void displayCameraError();
    
        void updateCameraDevice(QAction *action);
    
        void updateCameraState(QCamera::State);
        void updateCaptureMode();
        void updateRecorderState(QMediaRecorder::State state);
        void setExposureCompensation(int index);
    
        void updateRecordTime();
    
        void processCapturedImage(int requestId, const QImage &img);
        void updateLockStatus(QCamera::LockStatus, QCamera::LockChangeReason);
    
        void displayViewfinder();
        void displayCapturedImage();
    
        void readyForCapture(bool ready);
        void imageSaved(int id, const QString &fileName);
    
    protected:
        void keyPressEvent(QKeyEvent *event) override;
        void keyReleaseEvent(QKeyEvent *event) override;
        void closeEvent(QCloseEvent *event) override;
    
    private:
        Ui::Camera *ui;
    
        QScopedPointer<QCamera> m_camera;
    
        QScopedPointer<QCameraImageCapture> m_imageCapture;
        QScopedPointer<QMediaRecorder> m_mediaRecorder;
    
        QImageEncoderSettings m_imageSettings;
        QAudioEncoderSettings m_audioSettings;
        QVideoEncoderSettings m_videoSettings;
        QString m_videoContainerFormat;
        bool m_isCapturingImage = false;
        bool m_applicationExiting = false;
    };
    
    

    It's a private member initialized after the camera found in a constructor.

    Camera::Camera(QObject *parent) : QObject(parent) 
    {
          //some code
          if (cameraInfo == QCameraInfo::defaultCamera())
                  videoDeviceAction->setChecked(true);
    
           setCamera(QCameraInfo::defaultCamera());
    }
    
    jsulmJ 1 Reply Last reply
    0
    • J jenya7

      @jsulm said in A camera problem.:

      @jenya7 No, this belongs to C++ basics: if m_camera is a pointer then you have to use -> if it is not a pointer then you have to use dot. So, what is m_camera in YOUR code?

      in camera.h

      class Camera : public QMainWindow
      {
          Q_OBJECT
      
      public:
          Camera(QObject *parent = nullptr);
      
           void takeImage();
      
      private slots:
          void setCamera(const QCameraInfo &cameraInfo);
      
          void startCamera();
          void stopCamera();
      
          void record();
          void pause();
          void stop();
          void setMuted(bool);
      
          void toggleLock();
          void displayCaptureError(int, QCameraImageCapture::Error, const QString &errorString);
      
          void configureCaptureSettings();
          void configureVideoSettings();
          void configureImageSettings();
          void displayRecorderError();
          void displayCameraError();
      
          void updateCameraDevice(QAction *action);
      
          void updateCameraState(QCamera::State);
          void updateCaptureMode();
          void updateRecorderState(QMediaRecorder::State state);
          void setExposureCompensation(int index);
      
          void updateRecordTime();
      
          void processCapturedImage(int requestId, const QImage &img);
          void updateLockStatus(QCamera::LockStatus, QCamera::LockChangeReason);
      
          void displayViewfinder();
          void displayCapturedImage();
      
          void readyForCapture(bool ready);
          void imageSaved(int id, const QString &fileName);
      
      protected:
          void keyPressEvent(QKeyEvent *event) override;
          void keyReleaseEvent(QKeyEvent *event) override;
          void closeEvent(QCloseEvent *event) override;
      
      private:
          Ui::Camera *ui;
      
          QScopedPointer<QCamera> m_camera;
      
          QScopedPointer<QCameraImageCapture> m_imageCapture;
          QScopedPointer<QMediaRecorder> m_mediaRecorder;
      
          QImageEncoderSettings m_imageSettings;
          QAudioEncoderSettings m_audioSettings;
          QVideoEncoderSettings m_videoSettings;
          QString m_videoContainerFormat;
          bool m_isCapturingImage = false;
          bool m_applicationExiting = false;
      };
      
      

      It's a private member initialized after the camera found in a constructor.

      Camera::Camera(QObject *parent) : QObject(parent) 
      {
            //some code
            if (cameraInfo == QCameraInfo::defaultCamera())
                    videoDeviceAction->setChecked(true);
      
             setCamera(QCameraInfo::defaultCamera());
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #36

      @jenya7 OK, now it makes sense. Did you try to set parent on the viewfinder?

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

      J 1 Reply Last reply
      0
      • jsulmJ jsulm

        @jenya7 OK, now it makes sense. Did you try to set parent on the viewfinder?

        J Offline
        J Offline
        jenya7
        wrote on last edited by
        #37

        @jsulm said in A camera problem.:

        @jenya7 OK, now it makes sense. Did you try to set parent on the viewfinder?

        That's it - they use the widget int the app. What should I choose as a parent?

        jsulmJ 1 Reply Last reply
        0
        • J jenya7

          @jsulm said in A camera problem.:

          @jenya7 OK, now it makes sense. Did you try to set parent on the viewfinder?

          That's it - they use the widget int the app. What should I choose as a parent?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #38

          @jenya7 Do you have any other UI/widgets in your application?

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

          J 1 Reply Last reply
          0
          • jsulmJ jsulm

            @jenya7 Do you have any other UI/widgets in your application?

            J Offline
            J Offline
            jenya7
            wrote on last edited by jenya7
            #39

            @jsulm said in A camera problem.:

            @jenya7 Do you have any other UI/widgets in your application?

            well...I can put one just for debug sake. right now it's a window with a text box for a command line receiving.

            I did so

            QWidget *viewfinderPage;
            viewfinderPage = new QWidget();
            v_finder = new QCameraViewfinder(viewfinderPage);
            

            and I get the same exception on
            viewfinderPage = new QWidget();

            jsulmJ 1 Reply Last reply
            0
            • J jenya7

              @jsulm said in A camera problem.:

              @jenya7 Do you have any other UI/widgets in your application?

              well...I can put one just for debug sake. right now it's a window with a text box for a command line receiving.

              I did so

              QWidget *viewfinderPage;
              viewfinderPage = new QWidget();
              v_finder = new QCameraViewfinder(viewfinderPage);
              

              and I get the same exception on
              viewfinderPage = new QWidget();

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #40

              @jenya7 said in A camera problem.:

              and I get the same exception on
              viewfinderPage = new QWidget();

              You get same crash if you instanciate a QWidget?
              Then something is really broken in your setup.
              Can you build and execute a simple Qt widget application?

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

              J 1 Reply Last reply
              0
              • jsulmJ jsulm

                @jenya7 said in A camera problem.:

                and I get the same exception on
                viewfinderPage = new QWidget();

                You get same crash if you instanciate a QWidget?
                Then something is really broken in your setup.
                Can you build and execute a simple Qt widget application?

                J Offline
                J Offline
                jenya7
                wrote on last edited by
                #41

                @jsulm said in A camera problem.:

                @jenya7 said in A camera problem.:

                and I get the same exception on
                viewfinderPage = new QWidget();

                You get same crash if you instanciate a QWidget?
                Then something is really broken in your setup.
                Can you build and execute a simple Qt widget application?

                actually viewfinderPage is null pointed. I have to create it. I'll try.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jenya7
                  wrote on last edited by jenya7
                  #42

                  well...I added in a main window

                  QCameraViewfinder *m_viewfinder =  new QCameraViewfinder(this);
                  
                  

                  and then in camera class

                  camera->setViewfinder(m_viewfinder );
                  

                  But still get the message - "Image Capture Error: Camera not ready"
                  I get it on
                  m_imageCapture->capture();
                  When I try to go into capture() - I can't - the source unavailable, can not debug it to see why m_imageCapture decides the camera isn't ready.

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    Bonnie
                    wrote on last edited by
                    #43

                    According to https://doc.qt.io/qt-5/qcamera.html#State-enum :

                    In the active state as soon as camera is started the viewfinder displays video frames and the camera is ready for capture.
                    

                    So capture should not be called right after start.
                    After calling start, you should asynchronously wait until the state turns to Active before performing any capturing.

                    J 1 Reply Last reply
                    3
                    • B Bonnie

                      According to https://doc.qt.io/qt-5/qcamera.html#State-enum :

                      In the active state as soon as camera is started the viewfinder displays video frames and the camera is ready for capture.
                      

                      So capture should not be called right after start.
                      After calling start, you should asynchronously wait until the state turns to Active before performing any capturing.

                      J Offline
                      J Offline
                      jenya7
                      wrote on last edited by jenya7
                      #44

                      @Bonnie said in A camera problem.:

                      According to https://doc.qt.io/qt-5/qcamera.html#State-enum :

                      In the active state as soon as camera is started the viewfinder displays video frames and the camera is ready for capture.
                      

                      So capture should not be called right after start.
                      After calling start, you should asynchronously wait until the state turns to Active before performing any capturing.

                      so I did

                      void Camera::takeImage()
                      {
                          m_camera->start();
                          m_camera->searchAndLock();
                      
                           while (m_imageCapture->isReadyForCapture() == false) ;
                      
                          m_isCapturingImage = true;
                          m_imageCapture->capture();
                      
                          m_camera->unlock();
                      }
                      
                      

                      it's never ready. never gets out of while.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #45

                        Hi,

                        Your tight loop does not allow Qt's event loop to run. You should either use an event loop to "block" the execution or properly implement this through signals and slots.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        J 1 Reply Last reply
                        2
                        • SGaistS SGaist

                          Hi,

                          Your tight loop does not allow Qt's event loop to run. You should either use an event loop to "block" the execution or properly implement this through signals and slots.

                          J Offline
                          J Offline
                          jenya7
                          wrote on last edited by
                          #46

                          @SGaist said in A camera problem.:

                          Hi,

                          Your tight loop does not allow Qt's event loop to run. You should either use an event loop to "block" the execution or properly implement this through signals and slots.

                          OK. Did so

                          connect(m_imageCapture.data(), &QCameraImageCapture::isReadyForCapture, this, &Camera::IsReadyForCapture);
                          
                          void Camera::takeImage()
                          {
                              m_camera->start();
                              m_camera->searchAndLock();
                          
                             // while (m_imageCapture->isReadyForCapture() == false) ;
                          
                              //m_isCapturingImage = true;
                              //m_imageCapture->capture();
                          
                              //m_camera->unlock();
                          }
                          
                          void Camera::IsReadyForCapture()
                          {
                              m_isCapturingImage = true;
                              m_imageCapture->capture();
                          
                              m_camera->unlock();
                          }
                          

                          It never happens.

                          JonBJ 1 Reply Last reply
                          0
                          • J jenya7

                            @SGaist said in A camera problem.:

                            Hi,

                            Your tight loop does not allow Qt's event loop to run. You should either use an event loop to "block" the execution or properly implement this through signals and slots.

                            OK. Did so

                            connect(m_imageCapture.data(), &QCameraImageCapture::isReadyForCapture, this, &Camera::IsReadyForCapture);
                            
                            void Camera::takeImage()
                            {
                                m_camera->start();
                                m_camera->searchAndLock();
                            
                               // while (m_imageCapture->isReadyForCapture() == false) ;
                            
                                //m_isCapturingImage = true;
                                //m_imageCapture->capture();
                            
                                //m_camera->unlock();
                            }
                            
                            void Camera::IsReadyForCapture()
                            {
                                m_isCapturingImage = true;
                                m_imageCapture->capture();
                            
                                m_camera->unlock();
                            }
                            

                            It never happens.

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

                            @jenya7 said in A camera problem.:

                            connect(m_imageCapture.data(), &QCameraImageCapture::isReadyForCapture, this, &Camera::IsReadyForCapture);

                            Did this go through without complaint? I am "surprised", as QCameraImageCapture::isReadyForCapture() is not a signal, would have thought connect() would barf on that.... Try using void QCameraImageCapture::readyForCaptureChanged(bool ready) signal?

                            J 1 Reply Last reply
                            2
                            • JonBJ JonB

                              @jenya7 said in A camera problem.:

                              connect(m_imageCapture.data(), &QCameraImageCapture::isReadyForCapture, this, &Camera::IsReadyForCapture);

                              Did this go through without complaint? I am "surprised", as QCameraImageCapture::isReadyForCapture() is not a signal, would have thought connect() would barf on that.... Try using void QCameraImageCapture::readyForCaptureChanged(bool ready) signal?

                              J Offline
                              J Offline
                              jenya7
                              wrote on last edited by
                              #48

                              @JonB said in A camera problem.:

                              @jenya7 said in A camera problem.:

                              connect(m_imageCapture.data(), &QCameraImageCapture::isReadyForCapture, this, &Camera::IsReadyForCapture);

                              Did this go through without complaint? I am "surprised", as QCameraImageCapture::isReadyForCapture() is not a signal, would have thought connect() would barf on that.... Try using void QCameraImageCapture::readyForCaptureChanged(bool ready) signal?

                              did so

                              connect(m_imageCapture.data(), &QCameraImageCapture::readyForCaptureChanged, this, &Camera::ReadyForCaptureChanged);
                              
                              void Camera::takeImage()
                              {
                                  m_camera->start();
                                  m_camera->searchAndLock();
                              }
                              
                              void Camera::ReadyForCaptureChanged(bool ready)
                              {
                                  m_isCapturingImage = true;
                                  m_imageCapture->capture();
                              
                                  m_camera->unlock();
                              }
                              

                              never get into ReadyForCaptureChanged

                              jsulmJ 1 Reply Last reply
                              0
                              • J jenya7

                                @JonB said in A camera problem.:

                                @jenya7 said in A camera problem.:

                                connect(m_imageCapture.data(), &QCameraImageCapture::isReadyForCapture, this, &Camera::IsReadyForCapture);

                                Did this go through without complaint? I am "surprised", as QCameraImageCapture::isReadyForCapture() is not a signal, would have thought connect() would barf on that.... Try using void QCameraImageCapture::readyForCaptureChanged(bool ready) signal?

                                did so

                                connect(m_imageCapture.data(), &QCameraImageCapture::readyForCaptureChanged, this, &Camera::ReadyForCaptureChanged);
                                
                                void Camera::takeImage()
                                {
                                    m_camera->start();
                                    m_camera->searchAndLock();
                                }
                                
                                void Camera::ReadyForCaptureChanged(bool ready)
                                {
                                    m_isCapturingImage = true;
                                    m_imageCapture->capture();
                                
                                    m_camera->unlock();
                                }
                                

                                never get into ReadyForCaptureChanged

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #49

                                @jenya7 said in A camera problem.:

                                never get into ReadyForCaptureChanged

                                Sure? How did you verify that?

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

                                J 1 Reply Last reply
                                0
                                • jsulmJ jsulm

                                  @jenya7 said in A camera problem.:

                                  never get into ReadyForCaptureChanged

                                  Sure? How did you verify that?

                                  J Offline
                                  J Offline
                                  jenya7
                                  wrote on last edited by jenya7
                                  #50

                                  @jsulm said in A camera problem.:

                                  @jenya7 said in A camera problem.:

                                  never get into ReadyForCaptureChanged

                                  Sure? How did you verify that?

                                  I set a break point inside. I see it goes into takeImage but never gets to ReadyForCaptureChanged
                                  Now I don't get any errors but it prints
                                  Starting camera without viewfinder available
                                  Is it OK?

                                  J.HilkJ JonBJ 2 Replies Last reply
                                  0
                                  • J jenya7

                                    @jsulm said in A camera problem.:

                                    @jenya7 said in A camera problem.:

                                    never get into ReadyForCaptureChanged

                                    Sure? How did you verify that?

                                    I set a break point inside. I see it goes into takeImage but never gets to ReadyForCaptureChanged
                                    Now I don't get any errors but it prints
                                    Starting camera without viewfinder available
                                    Is it OK?

                                    J.HilkJ Offline
                                    J.HilkJ Offline
                                    J.Hilk
                                    Moderators
                                    wrote on last edited by
                                    #51

                                    @jenya7
                                    maybe its a typical object lifetime problem.

                                    where does your Camera instance exist/live ?


                                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                    Q: What's that?
                                    A: It's blue light.
                                    Q: What does it do?
                                    A: It turns blue.

                                    J 1 Reply Last reply
                                    0
                                    • J jenya7

                                      @jsulm said in A camera problem.:

                                      @jenya7 said in A camera problem.:

                                      never get into ReadyForCaptureChanged

                                      Sure? How did you verify that?

                                      I set a break point inside. I see it goes into takeImage but never gets to ReadyForCaptureChanged
                                      Now I don't get any errors but it prints
                                      Starting camera without viewfinder available
                                      Is it OK?

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

                                      @jenya7
                                      Just to make sure, what happens in your code after you call your takeImage()? Do you allow the main event loop to run, or do you have other code which does not?

                                      J 1 Reply Last reply
                                      0
                                      • J.HilkJ J.Hilk

                                        @jenya7
                                        maybe its a typical object lifetime problem.

                                        where does your Camera instance exist/live ?

                                        J Offline
                                        J Offline
                                        jenya7
                                        wrote on last edited by
                                        #53

                                        @J-Hilk said in A camera problem.:

                                        @jenya7
                                        maybe its a typical object lifetime problem.

                                        where does your Camera instance exist/live ?

                                        It's a global object. I call it's methods from a command line parser object.

                                        J.HilkJ 1 Reply Last reply
                                        0
                                        • J jenya7

                                          @J-Hilk said in A camera problem.:

                                          @jenya7
                                          maybe its a typical object lifetime problem.

                                          where does your Camera instance exist/live ?

                                          It's a global object. I call it's methods from a command line parser object.

                                          J.HilkJ Offline
                                          J.HilkJ Offline
                                          J.Hilk
                                          Moderators
                                          wrote on last edited by
                                          #54

                                          @jenya7 soooo, inside main() ?


                                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                          Q: What's that?
                                          A: It's blue light.
                                          Q: What does it do?
                                          A: It turns blue.

                                          J 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