亲宝软件园·资讯

展开

QT连接USB摄像头

vhcjgc 人气:0

功能:使用QT连接USB摄像头,点击按钮显示画面

QT += multimedia
QT += multimediawidgets

#include "camera.h"
#include "ui_camera.h"

Camera::Camera(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Camera)
{
    ui->setupUi(this);
    iniCamera();
}

Camera::~Camera()
{
    delete ui;
}

void Camera::iniCamera()
{

    cameras = QCameraInfo::availableCameras();//获取摄像头列表
    qDebug()<<cameras.size();

    for(int i = 0;i<cameras.size();i++)
        ui->comboCamera->addItem(cameras[i].description());//摄像头描述

}

void Camera::on_camStartBtn_clicked()
{
    curCamera=new QCamera(cameras[ui->comboCamera->currentIndex()],this);//新建QCamera
    curCamera->setViewfinder(ui->widget); //设置取景框预览
    curCamera->start();
}
#ifndef CAMERA_H
#define CAMERA_H

#include <QWidget>
#include <QCameraInfo>
#include <QCamera>
#include <QLabel>

#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QMediaRecorder>

namespace Ui {
class Camera;
}

class Camera : public QWidget
{
    Q_OBJECT

public:
    explicit Camera(QWidget *parent = nullptr);
    ~Camera();
    void iniCamera();//摄像头初始化

    QCamera *curCamera=Q_NULLPTR;//
    QList<QCameraInfo> cameras;//可用相机列表

    QCameraImageCapture *imageCapture; //抓图
    QMediaRecorder* mediaRecorder;//录像

private slots:
    void on_camStartBtn_clicked();

private:
    Ui::Camera *ui;
};

#endif // CAMERA_H

实现效果

加载全部内容

相关教程
猜你喜欢
用户评论