Qt5.14.2中使用QCamera实现切换相机、分辨率和图像捕获功能

demo中主要涉及的Qt类有:相机类QCamera,取景器类QCameraViewfinder,图像捕获类QCameraImageCapture,因此添加模块multimedia multimediawidgets

QT += core gui multimedia multimediawidgets

widget.h中添加相关类声明:

class QCamera;
class QCameraViewfinder;
class QCameraImageCapture;
class QCameraInfo;
class QCameraViewfinderSettings;

在类Widget中添加私有变量:

QCamera *camera;                   //相机
QCameraViewfinder *viewfinder;     //取景器
QCameraImageCapture *imageCapture; //图像捕获
QList<QCameraInfo> cameras;        //相机列表
QList<QSize> mResSize = {};        //分辨率列表

widget.cpp中添加头文件:

#include 
#include 
#include 
#include 
#include 
#include 
#include 

widget.cpp的构造函数中添加代码:

先设置下窗口名称及大小

setWindowTitle("Camera");
setFixedSize(600,400);

接着获取所有相机列表并选择一个相机初始化

cameras = QCameraInfo::availableCameras();   //获取所有相机的列表
//qDebug() << "this is camera: ";
if (cameras.count() > 0) {
    for(const QCameraInfo &cameraInfo:cameras) {
        qDebug() << cameraInfo.description();
    }
    camera = new QCamera(cameras.at(0));     //初始化实例化一个相机对象
}

实例化取景器类,取景器本质是一个窗口类,若不将当前窗口设为父对象,则可以另起一个窗口显示。将当前窗口设为父对象后,视频画面便在当前窗口显示。

//设置取景器
viewfinder = new QCameraViewfinder(this);
camera->setViewfinder(viewfinder);
viewfinder->resize(600,350);                 //设置取景器显示大小
//viewfinder->show();

实例化一个图像捕获类,并开启相机

imageCapture = new QCameraImageCapture(camera);
camera->start();                             //开启相机

设置默认摄像头的分辨率及帧率。注意:帧率一定要与摄像头匹配,否则不会显示画面。

//设置默认摄像头参数
QCameraViewfinderSettings set;
set.setResolution(640, 480);                 //设置显示分辨率
set.setMaximumFrameRate(25);                 //设置帧率
camera->setViewfinderSettings(set);

设置两个下拉框部件的初始化

//添加初始化的相机选项
for(const QCameraInfo &cameraInfo:cameras) {
    qDebug() << "CameraInfo:" << cameraInfo;
    ui->cameraSel->addItem(cameraInfo.description());
}
ui->cameraSel->setCurrentIndex(0);

//添加初始化相机的分辨率选项
mResSize = camera->supportedViewfinderResolutions();
//qDebug() << "mResSize = " << mResSize;
for (const QSize &msize:mResSize) {
       qDebug() << msize; //摄像头支持分辨率打印
       ui->resolutionSel->addItem(QString::number(msize.width(), 10) + "*" + QString::number(msize.height(), 10));
}
ui->resolutionSel->setCurrentIndex(0);

调整下widget.ui的内容,添加两个下拉框及一个按钮

Qt5.14.2中使用QCamera实现切换相机、分辨率和图像捕获功能_第1张图片
编写信号槽连接

首先是选择摄影机,先释放之前的内存,再将camera指向新的。选择新的相机意味着分辨率选项也会变化,因此需要删除分辨率下拉框中前相机的所有分辨率选项,再添加新相机的所有分辨率选项。

connect(ui->cameraSel, QOverload<int>::of(&QComboBox::activated), [=](int index){
    //qDebug() << "Index" << ui->cameraSel->currentIndex() << ": " << ui->cameraSel->currentText();
    camera->stop();
    delete camera;
    camera = new QCamera(cameras[index]);                //camera指向指定的摄像头
    camera->setCaptureMode(QCamera::CaptureStillImage);  //设定捕获模式
    camera->setViewfinder(viewfinder);                   //设置取景器
    camera->start();

    //删除前相机的所有分辨率内容
    for (int i = 0; i < mResSize.size(); i++) {
           ui->resolutionSel->removeItem(0);
    }

    //添加新相机的所有分辨率
    mResSize.clear();
    //qDebug() << "now mResSize = " << mResSize;
    mResSize = camera->supportedViewfinderResolutions();
    for (const QSize &msize:mResSize) {
           qDebug() << msize; //摄像头支持分辨率打印
           ui->resolutionSel->addItem(QString::number(msize.width(), 10) + "*" + QString::number(msize.height(), 10));
    }
    ui->resolutionSel->setCurrentIndex(0);
});

选择分辨率

connect(ui->resolutionSel, QOverload<int>::of(&QComboBox::activated), [=](int index){
    //qDebug() << "Index" << ui->resolutionSel->currentIndex() << ": " << ui->resolutionSel->currentText();
    //qDebug() << "mResSize:" << mResSize[index];
    //重新设置摄像头参数
    QCameraViewfinderSettings reset;
    reset.setResolution(mResSize[index]);
    camera->setViewfinderSettings(reset);
});

截图按钮

connect(ui->screenShot, &QPushButton::clicked, [=](){
    camera->setCaptureMode(QCamera::CaptureStillImage);
    delete imageCapture;
    imageCapture = new QCameraImageCapture(camera);
    QString fileName = QFileDialog::getSaveFileName();
    camera->searchAndLock();
    imageCapture->capture(fileName);
    camera->unlock();
});

最终效果

Qt5.14.2中使用QCamera实现切换相机、分辨率和图像捕获功能_第2张图片
附完整代码

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class QCamera;
class QCameraViewfinder;
class QCameraImageCapture;
class QCameraInfo;
class QCameraViewfinderSettings;

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;
    QCamera *camera;                   //相机
    QCameraViewfinder *viewfinder;     //取景器
    QCameraImageCapture *imageCapture; //图像捕获
    QList<QCameraInfo> cameras;        //相机列表
    QList<QSize> mResSize = {};        //分辨率列表
    //QCameraViewfinderSettings *set;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setWindowTitle("Camera");
    setFixedSize(600,400);

    cameras = QCameraInfo::availableCameras();   //获取所有相机的列表
    //qDebug() << "this is camera: ";
    if (cameras.count() > 0) {
        for(const QCameraInfo &cameraInfo:cameras) {
            qDebug() << cameraInfo.description();
        }
        camera = new QCamera(cameras.at(0));     //初始化实例化一个相机对象
    }

    //设置取景器
    viewfinder = new QCameraViewfinder(this);
    camera->setViewfinder(viewfinder);
    viewfinder->resize(600,350);                 //设置取景器显示大小
    //viewfinder->show();

    imageCapture = new QCameraImageCapture(camera);
    camera->start();                             //开启相机

    //设置默认摄像头参数
    QCameraViewfinderSettings set;
    set.setResolution(640, 480);                 //设置显示分辨率
    set.setMaximumFrameRate(25);                 //设置帧率
    camera->setViewfinderSettings(set);

    //添加初始化的相机选项
    for(const QCameraInfo &cameraInfo:cameras) {
        qDebug() << "CameraInfo:" << cameraInfo;
        ui->cameraSel->addItem(cameraInfo.description());
    }
    ui->cameraSel->setCurrentIndex(0);

    //添加初始化相机的分辨率选项
    mResSize = camera->supportedViewfinderResolutions();
    //qDebug() << "mResSize = " << mResSize;
    for (const QSize &msize:mResSize) {
           qDebug() << msize; //摄像头支持分辨率打印
           ui->resolutionSel->addItem(QString::number(msize.width(), 10) + "*" + QString::number(msize.height(), 10));
    }
    ui->resolutionSel->setCurrentIndex(0);

    //信号槽连接
    connect(ui->cameraSel, QOverload<int>::of(&QComboBox::activated), [=](int index){
        //qDebug() << "Index" << ui->cameraSel->currentIndex() << ": " << ui->cameraSel->currentText();
        camera->stop();
        delete camera;
        camera = new QCamera(cameras[index]);                //camera指向指定的摄像头
        camera->setCaptureMode(QCamera::CaptureStillImage);  //设定捕获模式
        camera->setViewfinder(viewfinder);                   //设置取景器
        camera->start();

        //删除前相机的所有分辨率内容
        for (int i = 0; i < mResSize.size(); i++) {
               ui->resolutionSel->removeItem(0);
        }

        //添加新相机的所有分辨率
        mResSize.clear();
        //qDebug() << "now mResSize = " << mResSize;
        mResSize = camera->supportedViewfinderResolutions();
        for (const QSize &msize:mResSize) {
               qDebug() << msize; //摄像头支持分辨率打印
               ui->resolutionSel->addItem(QString::number(msize.width(), 10) + "*" + QString::number(msize.height(), 10));
        }
        ui->resolutionSel->setCurrentIndex(0);
    });

    connect(ui->resolutionSel, QOverload<int>::of(&QComboBox::activated), [=](int index){
        //qDebug() << "Index" << ui->resolutionSel->currentIndex() << ": " << ui->resolutionSel->currentText();
        //qDebug() << "mResSize:" << mResSize[index];
        //重新设置摄像头参数
        QCameraViewfinderSettings reset;
        reset.setResolution(mResSize[index]);
        camera->setViewfinderSettings(reset);
    });

    connect(ui->screenShot, &QPushButton::clicked, [=](){
        camera->setCaptureMode(QCamera::CaptureStillImage);
        delete imageCapture;
        imageCapture = new QCameraImageCapture(camera);
        QString fileName = QFileDialog::getSaveFileName();
        camera->searchAndLock();
        imageCapture->capture(fileName);
        camera->unlock();
    });
}

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

你可能感兴趣的:(Qt/MFC,qt,c++)