您好,登录后才能下订单哦!
这篇文章主要介绍“Qt怎么使用QCamera实现切换相机,分辨率和图像捕获功能”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Qt怎么使用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 <QCamera> #include <QCameraInfo> #include <QCameraViewfinder> #include <QCameraImageCapture> #include <QFileDialog> #include <QList> #include <QDebug>
在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的内容,添加两个下拉框及一个按钮
编写信号槽连接
首先是选择摄影机,先释放之前的内存,再将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(); });
附完整代码
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> 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 <QCamera> #include <QCameraInfo> #include <QCameraViewfinder> #include <QCameraImageCapture> #include <QFileDialog> #include <QList> #include <QDebug> 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怎么使用QCamera实现切换相机,分辨率和图像捕获功能”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。