QT中Camera相机调用

一:摄像头配置
1、创建摄像头类,获取电脑端的所有摄像头
cameras = QCameraInfo::availableCameras(); //获取所有相机的列表
if (cameras.count() > 0)
{
for(const QCameraInfo &cameraInfo:cameras)
{
qDebug() << cameraInfo.description();
}
camera = new QCamera(cameras.at(0)); //初始化实例化第一个相机对象
}
2、设置取景器(将摄像头采集的图像放入一个屏幕中)
viewfinder=new QCameraViewfinder(ui->lblVideo);
viewfinder->resize(640, 480);
CamerimageCapture=new QCameraImageCapture(camera);
CamerimageCapture>setCaptureDestination(QCameraImageCapture::CaptureToFile);
3、将取景器与摄像头连接
camera->setCaptureMode(QCamera::CaptureStillImage);//将其采集为图片camera->setCaptureMode(QCamera::CaptureMode::CaptureViewfinder);//将采集到取景器中
camera->setViewfinder(viewfinder);
viewfinder->resize(640, 480);
4、设置默认摄像头的参数
//设置默认摄像头参数
QCameraViewfinderSettings set; //专门用于设置取景器参数,创建取景器类对象
set.setResolution(640, 480); //设置显示分辨率,以像素为单位设置取景器的分辨率
set.setMaximumFrameRate(75); //设置帧率,设置取景器的最小帧速率(以每秒帧数为单位)
camera->setViewfinderSettings(set); //设置取景器参数与相机匹配
5、设置相机列表选项
for(const QCameraInfo &cameraInfo:cameras)
{
qDebug() << “CameraInfo:” << cameraInfo;
ui->cameraSel->addItem(cameraInfo.description());
}
ui->cameraSel->setCurrentIndex(0);
注释:
cameraSel:为一个控件(QComboBox);
ui->cameraSel->addItem(cameraInfo.description());把所有相机显示在控件中;
6、设置初始(默认)相机的分辨率选项
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);//默认列表中第一个相机
resolutionSel:为一个控件(QComboBox);
7、相机选项槽函数——相机显示QComboBox控件
//信号槽连接
connect(ui->cameraSel, QOverload::of(&QComboBox::activated), [=](int index)
{
for (int i = 0; i < mResSize.size(); i++)
{
ui->resolutionSel->removeItem(0);
}
//添加新相机的所有分辨率
mResSize.clear();
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::of(&QComboBox::activated), [=](int index)
{
//重新设置摄像头参数
QCameraViewfinderSettings reset;
reset.setResolution(mResSize[index]);
camera->setViewfinderSettings(reset);
});
8、截图槽函数
connect(ui->screenShot, &QPushButton::clicked, ={
camera->setCaptureMode(QCamera::CaptureStillImage);//设置捕获模式为拍照,静态帧捕获
delete imageCapture;
imageCapture = new QCameraImageCapture(camera); //创建图像捕获类对象,与camera配合进行拍照
QString fileName = QFileDialog::getSaveFileName(); //获得保存路径
camera->searchAndLock(); //锁定相机
imageCapture->capture(fileName); //捕获图片并保存文件
camera->unlock(); //解除相机锁定
});
connect(imageCapture, SIGNAL(imageCaptured(int,QImage)), this, SLOT(rcv_image(int,QImage)));
9、关闭相机槽函数
void Widget::on_closeScreen_clicked()
{
delete camera;
camera->stop();
QApplication *app;
app->exit(0);

}
10、开启相机槽函数
void Widget::on_openScreen_clicked()
{
camera->stop();
delete camera;
camera->start();
fps_time.start(33);
}
11、完整代码
Widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
class QCamera;
class QCameraViewfinder;
class QCameraImageCapture;
class QCameraInfo;
class QCameraViewfinderSettings;
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

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

private slots:
    void on_openScreen_clicked();
    void rcv_image(int, QImage);
    void on_closeScreen_clicked();

private:
    Ui::Widget *ui;
    QCamera *camera;                   //相机
    QCameraViewfinder *viewfinder;     //取景器
    QCameraImageCapture *imageCapture; //图像捕获
    QList<QCameraInfo> cameras;        //相机列表
    QList<QSize> mResSize = {};        //分辨率列表
    QUdpSocket *sender;
    QTimer fps_time;
    //QCameraViewfinderSettings *set;
};
#endif // WIDGET_H
在这里插入代码片

Widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setWindowTitle("Camera");                    //设置标题
    cameras = QCameraInfo::availableCameras();   //获取所有相机的列表
    if (cameras.count() > 0)
    {
      for(const QCameraInfo &cameraInfo:cameras)//用来查询系统当前可用的相机设备
      {
         qDebug() << cameraInfo.description();  //打印出相机列表
      }
            camera = new QCamera(cameras.at(0));
            //使用第一个相机设备信息创建相机
            //初始化实例化一个相机对象
    }

    //设置取景器
    viewfinder = new QCameraViewfinder(ui->lblVideo);//设置一个控件为取景器,创建取景器部件
    camera->setViewfinder(viewfinder);               //加入取景器,设置取景器部件
    viewfinder->resize(640,480);                     //设置取景器显示大小,设置取景器size

    imageCapture = new QCameraImageCapture(camera); //创建图像捕获类对象,与camera配合进行拍照
    imageCapture->setCaptureDestination(QCameraImageCapture::CaptureToFile);
    camera->setCaptureMode(QCamera::CaptureStillImage);             //设置捕获模式为拍照,静态帧捕获
    camera->setCaptureMode(QCamera::CaptureMode::CaptureViewfinder);//将采集到取景器中
    //设置默认摄像头参数
    QCameraViewfinderSettings set;               //专门用于设置取景器参数,创建取景器类对象
    set.setResolution(640, 480);                 //设置显示分辨率,以像素为单位设置取景器的分辨率
    set.setMaximumFrameRate(75);                 //设置帧率,设置取景器的最小帧速率(以每秒帧数为单位)
    camera->setViewfinderSettings(set);          //设置取景器参数与相机匹配

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

    //添加初始化相机的分辨率选项
    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->cameraSel, QOverload<int>::of(&QComboBox::activated), [=](int index)
    {
                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)
            {
                //重新设置摄像头参数
                QCameraViewfinderSettings reset;
                reset.setResolution(mResSize[index]);
                camera->setViewfinderSettings(reset);
            });

            connect(ui->screenShot, &QPushButton::clicked, [=](){
                camera->setCaptureMode(QCamera::CaptureStillImage);//设置捕获模式为拍照,静态帧捕获
                delete imageCapture;
                imageCapture = new QCameraImageCapture(camera);     //创建图像捕获类对象,与camera配合进行拍照
                QString fileName = QFileDialog::getSaveFileName();  //获得保存路径
                camera->searchAndLock();                            //锁定相机
                imageCapture->capture(fileName);                    //捕获图片并保存文件
                camera->unlock();                                   //解除相机锁定
            });



}

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

void Widget::on_openScreen_clicked()
{
    camera->start();
    fps_time.start(33);
}
void Widget::on_closeScreen_clicked()
{
    delete camera;
    camera->stop();
    QApplication *app;
    app->exit(0);

}在这里插入代码片

widget.ui
QT中Camera相机调用_第1张图片
实际效果图
QT中Camera相机调用_第2张图片

你可能感兴趣的:(QT,qt,c++)