QT打开摄像头教程

来源:互联网 发布:淘宝客服双11快捷短语 编辑:程序博客网 时间:2024/04/29 17:44

VS2015+QT5.8

第一步:新建工程

第二步:画上一个垂直布局,添加成以下形式,并设置widget为水平布局

第三步:设置相关库文件


.h文件如下:

#pragmaonce

 

#include<QtWidgets/QMainWindow>

#include"ui_QtCamera.h"

#include<QCamera>

#include<QCameraViewfinder>

#include<QCameraImageCapture>

 

classQtCamera :publicQMainWindow

{

        Q_OBJECT

 

public:

        QtCamera(QWidget *parent =Q_NULLPTR);

        ~QtCamera();

publicslots:

        voidopenCam();

private:

        Ui::QtCameraClassui;

        QCamera *m_Cam;

        QVideoWidget *m_widget;

        QCameraViewfinder *viewfinder;

};

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.cpp文件如下:

 

#include"QtCamera.h"

 

QtCamera::QtCamera(QWidget *parent)

        : QMainWindow(parent)

{

        ui.setupUi(this);

 

        m_widget =newQVideoWidget(this);

        m_Cam =newQCamera;

        //添加显示界面

        ui.verticalLayout->addWidget(m_widget,0);

        //链接按钮的信号和槽

        connect(ui.pushButton,SIGNAL(clicked()),this,SLOT(openCam()));

        ui.pushButton->setText(QString::fromLocal8Bit("打开摄像头"));

}

QtCamera::~QtCamera()

{

        deletem_Cam;

}

voidQtCamera::openCam()

{

        m_Cam->setCaptureMode(QCamera::CaptureVideo);

        m_Cam->setViewfinder(m_widget);

        m_Cam->start();

        ui.pushButton->setEnabled(false);

        

}

 

 

1 0
原创粉丝点击