Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to start and stop QCamera



  • #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "unistd.h"
    #include <qdebug.h>
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    m_scene = new QGraphicsScene;
    m_item = new QGraphicsVideoItem;
    
    ui->graphicsView->setScene(m_scene);
    ui->graphicsView->scene()->addItem(m_item);
    
    camera = new QCamera(QCameraInfo::defaultCamera());
    camera->setViewfinder(m_item);
    camera->setCaptureMode(QCamera::CaptureVideo);
    }
    
    MainWindow::~MainWindow()
    {
    delete ui;
    }
    // On Start Button
    void MainWindow::on_pushButton_clicked()
    {
    camera->start();
    }
    // On Stop Button
    void MainWindow::on_pushButton_2_clicked()
    {
    camera->stop();
    }
    

    I have above code to stream USB camera. Camera stream does not start on pressing "Start Button". Camera stream is displayed when I first press Stop button and then Start button. Multiple USB cameras are connected to my Linux system. I want select any one camera and start or stop stream. I do not want to record.

    How to make it work correctly?



  • Hi @mit_cruze

    The above code works fine, behavior is also as expected.

    void MyCamera_Forum::on_pushButton_clicked()
    {
        camera->start(); // Start Camera & Streaming 
    }
    
    void MyCamera_Forum::on_pushButton_2_clicked()
    {
        camera->stop(); // Stop Streaming 
    }
    

    Do you have any other warnings ?
    And you can check for availableCameras under QCameraInfo
    With the available devises you can specify which Camera you want to use.

    All the best.



  • Check the error() condition and state() to determine whether an operation is valid or if an error occured when you try to do something with your camera. Don't make assumptions about the state of the device.



  • @Pradeep-P-N On what Qt version did you test?



  • @mit_cruze
    Qt 5.12.1 (Creator 4.10.1) & Qt 5.9.2 (Creator 4.4.1)

    And as said by @Kent-Dorfman
    Did you check the error() QCamera::Error and state() QCamera::State of the camera ?



  • @Pradeep-P-N Hi Pradeep Thanks for reply.

    I have added debugs for Camera state, status and error. Also added output.
    My pushbutton press sequence is 1) Start 2) Stop 3 ) Start

    Debugs:

    // On Start Button
    void MainWindow::on_pushButton_clicked()
    {
        qDebug() << "before start camera->status(): " << camera->status();
        qDebug() << "before start camera->state(): " << camera->state();
        qDebug() << "before start camera->error(): " << camera->error();
        qDebug() << "before start camera->errorString():" << camera->errorString() ;
        camera->start();
        qDebug() << "after start camera->status(): " << camera->status();
        qDebug() << "after start camera->state(): " << camera->state();
        qDebug() << "after start camera->error(): " << camera->error();
        qDebug() << "after start camera->errorString():" << camera->errorString() << "\n";
    }
    // On Stop Button
    void MainWindow::on_pushButton_2_clicked()
    {
        qDebug() << "before stop camera->status(): " << camera->status();
        qDebug() << "before stop camera->state(): " << camera->state();
        qDebug() << "before stop camera->error(): " << camera->error();
        qDebug() << "before stop camera->errorString():" << camera->errorString() ;
    
        camera->stop();
        qDebug() << "after stop camera->status(): " << camera->status();
        qDebug() << "after stop camera->state(): " << camera->state();
        qDebug() << "after stop camera->error(): " << camera->error();
        qDebug() << "after stop camera->errorString():" << camera->errorString()  << "\n";
    
    }
    
    

    Outputs:

    before start camera->status():  QCamera::UnloadedStatus
    before start camera->state():  QCamera::UnloadedState
    before start camera->error():  QCamera::NoError
    before start camera->errorString(): ""
    after start camera->status():  QCamera::LoadingStatus
    after start camera->state():  QCamera::ActiveState
    after start camera->error():  QCamera::NoError
    after start camera->errorString(): "" 
    
    before stop camera->status():  QCamera::LoadedStatus
    before stop camera->state():  QCamera::ActiveState
    before stop camera->error():  QCamera::NoError
    before stop camera->errorString(): ""
    after stop camera->status():  QCamera::LoadedStatus
    after stop camera->state():  QCamera::LoadedState
    after stop camera->error():  QCamera::NoError
    after stop camera->errorString(): "" 
    
    before start camera->status():  QCamera::LoadedStatus
    before start camera->state():  QCamera::LoadedState
    before start camera->error():  QCamera::NoError
    before start camera->errorString(): ""
    after start camera->status():  QCamera::StartingStatus
    after start camera->state():  QCamera::ActiveState
    after start camera->error():  QCamera::NoError
    after start camera->errorString(): "" 
    

    I have Qt 5.9.5 and Qt creator 4.5.2



  • Let you check with like this,
    ui->setupUi(this);
    mCamera=new QCamera(this);
    mCameraViewfinder=new QCameraViewfinder(this);
    mCameraImageCapture=new QCameraImageCapture(mCamera,this);
    mLayout=new QVBoxLayout;
    mOpenMenu=new QMenu("OPEN",this);
    mEncoderAction=new QAction("ENCODER",this);
    mAction=new QAction("Capture",this);
    mCaptureAction=new QAction("Save As",this);
    mOpenMenu->addActions({mEncoderAction,mAction,mCaptureAction});
    mCamera->setViewfinder(mCameraViewfinder);
    mLayout->setMargin(0);
    ui->pushButton->setMenu(mOpenMenu);
    mLayout->addWidget(mCameraViewfinder);
    ui->scrollArea->setLayout(mLayout);

    connect(mEncoderAction,&QAction::triggered,[&](){
        mCamera->start();
    });
    connect(mAction,&QAction::triggered,[&](){
        mCamera->stop();
    });
    connect(mCaptureAction,&QAction::triggered,[&](){
       auto filename= QFileDialog::getSaveFileName(this,"Capture","/","Image(*.jpg,*.jpeg)");
       if(filename.isEmpty()){
           return;
       }
        mCameraImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
        QImageEncoderSettings imageEncodersettings;
        imageEncodersettings.setCodec("image/jpeg");
        imageEncodersettings.setResolution(1600,1200);
        mCameraImageCapture->setEncodingSettings(imageEncodersettings);
        mCamera->setCaptureMode(QCamera::CaptureStillImage);
        mCamera->start();
        mCamera->searchAndLock();
        mCameraImageCapture->capture(filename);
        mCamera->unlock();
    });


  • @SURYA-P Hi, I have tried your code. I have to press "Capture" and then "ENCODER" to start camera stream. Only pressing ENDCODER camera does not start streaming.



  • Hi i think you want import on your .pro file this
    QT += core gui multimedia multimediawidgets


Log in to reply