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

How to read video from QCamera frame by frame ?



  • For a real-time image processing project I need to get frame from QCamera and stream it with UDP protocol in bmp format. But I couldn't read frame from QCamera. I can capture pictures but when I try to save it into a buffer during debugging when I view buffer its empty. Can you show me how can I save video frame in a buffer in realtime and send it ? Or atleast the saving the buffer part ? I'm really amateur using QCamera so I am posting all of my code:

    udpcamera.h

    #ifndef UDPCAMERA_H
    #define UDPCAMERA_H
    
    #include <QCamera>
    #include <QCameraImageCapture>
    #include <QMediaRecorder>
    #include <QScopedPointer>
    #include <QObject>
    
    #include <QMainWindow>
    
    namespace Ui {
    class UDPCamera;
    }
    
    class QCamera;
    class QCameraViewfinder;
    class QCameraImageCapture;
    class QVBoxLayout;
    class QMenu;
    class QAction;
    
    
    class UDPCamera : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit UDPCamera(QWidget *parent = nullptr);
        ~UDPCamera();
    private slots:
    
    
    private:
    
        //scoped pointer kullanabilirsin ornek: QScopedPointer<QCamera> mCamera;
        Ui::UDPCamera *ui;
        QCamera *mCamera;
        QCameraViewfinder *mCameraViewFinder;
        QCameraImageCapture *mCameraImageCapture;
        QVBoxLayout * mLayout;
        QMenu *mOptionsMenu;
        QAction *mOpenAction;
        QAction *mCloseAction;
        QAction *mCaptureAction;
        QMediaRecorder * m_mediaRecorder;
    
    };
    
    #endif // UDPCAMERA_H
    
    

    udpcamera.cpp

    #include "udpcamera.h"
    #include "ui_udpcamera.h"
    #include <QCamera>
    #include <QCameraViewfinder>
    #include <QCameraImageCapture>
    #include <QVBoxLayout>
    #include <QMenu>
    #include <QAction>
    
    
    #include <QBuffer>
    #include <QObject>
    
    UDPCamera::UDPCamera(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::UDPCamera)
    {
        ui->setupUi(this);
           mCamera = new QCamera(this);
           mCameraViewFinder = new QCameraViewfinder(this);
           mCameraImageCapture = new QCameraImageCapture(mCamera,this);
           mLayout = new QVBoxLayout;
           mOptionsMenu = new QMenu("Options",this);
           mOpenAction = new QAction("Open",this);
           mCloseAction = new QAction("Close",this);
           mCaptureAction = new QAction("Capture",this);
    
    
           mOptionsMenu->addActions({mOpenAction,mCloseAction,
                                   mCaptureAction});
           ui->optionsPushButton->setMenu(mOptionsMenu);
    
           mCamera->setViewfinder(mCameraViewFinder);
           mLayout->addWidget(mCameraViewFinder);
           ui->scrollArea->setLayout(mLayout);
    
           mCamera->setCaptureMode(QCamera::CaptureStillImage);
           mCameraViewFinder->show();
           mCameraImageCapture->setCaptureDestination(QCameraImageCapture::CaptureToBuffer);
    
    
           connect(mOpenAction, &QAction::triggered, [&](){
               mCamera->start();
           });
    
           connect(mCloseAction, &QAction::triggered, [&](){
               mCamera->stop();
           });
    
           connect(mCaptureAction, &QAction::triggered, [&](){
               mCameraImageCapture->capture();
           });
    
    
           connect(mCameraImageCapture, &QCameraImageCapture::imageCaptured, [=] (int id, QImage img) {
               QByteArray buf;
               QBuffer buffer(&buf);
               buffer.open(QIODevice::WriteOnly);
               img.save(&buffer, "BMP");
    
           });
           connect(mCameraImageCapture, &QCameraImageCapture::readyForCaptureChanged, [=] (bool state) {
               if(state == true) {
                      mCamera->searchAndLock();
                      mCameraImageCapture->capture();
                      mCamera->unlock();
                  }
           });
    
    
    
    }
    
    UDPCamera::~UDPCamera()
    {
        delete ui;
    }
    
    

    udpcamera.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>UDPCamera</class>
     <widget class="QMainWindow" name="UDPCamera">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>514</width>
        <height>395</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>UDPCamera</string>
      </property>
      <widget class="QWidget" name="centralWidget">
       <widget class="QWidget" name="layoutWidget">
        <property name="geometry">
         <rect>
          <x>40</x>
          <y>10</y>
          <width>451</width>
          <height>321</height>
         </rect>
        </property>
        <layout class="QVBoxLayout" name="verticalLayout">
         <item>
          <widget class="QPushButton" name="optionsPushButton">
           <property name="text">
            <string>Options</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QScrollArea" name="scrollArea">
           <property name="widgetResizable">
            <bool>true</bool>
           </property>
           <widget class="QWidget" name="scrollAreaWidgetContents">
            <property name="geometry">
             <rect>
              <x>0</x>
              <y>0</y>
              <width>447</width>
              <height>286</height>
             </rect>
            </property>
           </widget>
          </widget>
         </item>
        </layout>
       </widget>
      </widget>
      <widget class="QMenuBar" name="menuBar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>514</width>
         <height>22</height>
        </rect>
       </property>
      </widget>
      <widget class="QToolBar" name="mainToolBar">
       <attribute name="toolBarArea">
        <enum>TopToolBarArea</enum>
       </attribute>
       <attribute name="toolBarBreak">
        <bool>false</bool>
       </attribute>
      </widget>
      <widget class="QStatusBar" name="statusBar"/>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources/>
     <connections/>
    </ui>
    
    

  • Lifetime Qt Champion

    Hi,

    What kind of camera is it ?
    What do you want to do with each frame ?


Log in to reply