您好,登錄后才能下訂單哦!
這篇文章主要介紹“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。