Qt6实现调用摄像头并显示画面
音视频开发老舅 人气:01、Cmake环境
CmakeLists.txt添加
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) find_package(Qt6 COMPONENTS Multimedia REQUIRED) find_package(Qt6 COMPONENTS MultimediaWidgets REQUIRED) target_link_libraries(samon1 PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) target_link_libraries(samon1 PRIVATE Qt6::Multimedia) target_link_libraries(samon1 PRIVATE Qt6::MultimediaWidgets) set_target_properties(samon1 PROPERTIES
这个samon1是我工程目录的文件名,具体请根据自己的工程目录名和CmakeLists配置而定。
文件调用记得修改名称
set(PROJECT_SOURCES main.cpp camera.cpp camera.h camera.ui )
qmake编译环境请自行根据调用头文件查找Qt手册。
2、源代码
main.cpp
#include <QApplication>//Qt必要头文件 #include "camera.h"//摄像头 int main(int argc, char *argv[]) { QApplication a(argc, argv);//Qt初始化 Camera w;//创建窗口类 w.setWindowTitle("Camera");//设置窗口标题 w.show();//显示窗口 return a.exec();//程序结束 }
camera.cpp
#include "camera.h"//摄像头 #include "./ui_camera.h"//CMakeLists.txt构建后产生文件 Camera::Camera(QWidget *parent) : QMainWindow(parent) , ui(new Ui::Camera) { ui->setupUi(this); //qDebug("Camera Device Check:%d",Camera::checkCameraAvailability());//摄像头驱动检测debug测试 my_camera.reset(new QCamera(QMediaDevices::defaultVideoInput()));//新建并设置摄像头使用默认驱动 my_captureSession.setCamera(my_camera.data());//捕获摄像头画面 my_captureSession.setVideoOutput(ui->viewfinder);//设置捕捉画面显示窗口 my_camera->start();//启动摄像头 } //检测摄像头驱动能否识别 bool Camera::checkCameraAvailability() { if (QMediaDevices::videoInputs().count() > 0) return true;//检测到摄像头驱动 else return false;//没有检测到摄像头驱动 } //消类 Camera::~Camera() { delete ui; }
camera.h
//icysamon@outlook.com #ifndef CAMERA_H #define CAMERA_H #include <QMainWindow> #include <QCamera> #include <QVideoWidget> #include <QMediaCaptureSession> #include <QMediaDevices> #include <QImageCapture> #include <QMediaRecorder> QT_BEGIN_NAMESPACE namespace Ui { class Camera; } QT_END_NAMESPACE class Camera : public QMainWindow { Q_OBJECT public: Camera(QWidget *parent = nullptr); ~Camera(); private: Ui::Camera *ui; QScopedPointer<QCamera> my_camera; QScopedPointer<QMediaRecorder> my_mediaRecorder; QMediaCaptureSession my_captureSession; bool checkCameraAvailability(void); }; #endif // CAMERA_H
camera.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Camera</class> <widget class="QMainWindow" name="Camera"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>762</width> <height>567</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QVideoWidget" name="viewfinder"/> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>762</width> <height>26</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <customwidgets> <customwidget> <class>QVideoWidget</class> <extends>QWidget</extends> <header location="global">qvideowidget.h</header> <container>1</container> </customwidget> </customwidgets> <resources/> <connections/> </ui>
ui设计界面类关系如图所示
- 以QMainWindow为父类建立子类Camera
- 以QVedioWidget为父类建立子类viewfinder
最后编译运行,就能实现一个纯视频的窗口,有关摄像头程序的截屏涉及个人隐私,故不做展示。
加载全部内容