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实现切换相机,分辨率和图像捕获功能的文章就介绍到这了,更多相关Qt QCamera内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341