1. 新建一个mainwindow项目,目录如下:
![](https://i-blog.csdnimg.cn/blog_migrate/89265a15c6700cadd2793c05e3d7276f.png)
2. 进入qt designer,添加控件,如下图:注意修改对应的控件名称
![](https://i-blog.csdnimg.cn/blog_migrate/aec186c31d72214e70ddd3ec42454b30.png)
3. 在.pro文件中添加:
QT +=multimedia
QT+=multimediawidgets
4.
4.1 编辑mainwindow.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QFileDialog>
#include <QCameraInfo>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class QCamera;
class QCameraViewfinder;
class QCameraImageCapture;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void captureImage();
void displayImage(int,QImage);
void saveImage();
private:
Ui::MainWindow *ui;
QCamera *camera;
QCameraViewfinder *viewfinder;
QCameraImageCapture *imageCapture;
};
#endif // MAINWINDOW_H
4.2 编辑main.cpp文件,将获取摄像头设备名的代码添加到这里,我这里共两个摄像头,本机+usb
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
QList<QCameraInfo>::iterator cameras_iter = cameras.begin();
for(;cameras_iter!= cameras.end();cameras_iter++)
qDebug()<<cameras_iter->deviceName()<<"\n"<<cameras_iter->description();//打印设备名字和描述信息
qDebug()<<QCameraInfo::defaultCamera().description();//查看系统默认摄像头
MainWindow w;
//w.setFixedSize(800,600); 自定义窗口大小
w.show();
return a.exec();
}
![](https://i-blog.csdnimg.cn/blog_migrate/0cc9ebac5de0ab2616a5571456a9f3d0.png)
4.3 编辑mainwindow.cpp文件
其中
camera = new QCamera("@device:pnp:\\\\?\\usb#vid_0c45&pid_6340&mi_00#6&16ce49d7&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\\global");
选择你需要调取的摄像头,我这里选的是usb摄像头
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
camera = new QCamera("@device:pnp:\\\\?\\usb#vid_0c45&pid_6340&mi_00#6&16ce49d7&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\\global");
viewfinder = new QCameraViewfinder(this);
imageCapture = new QCameraImageCapture(camera);
ui->ImageView->addWidget(viewfinder);
ui->ImageCapture->setScaledContents(true);
camera->setViewfinder(viewfinder);
camera->start(); //启动摄像头
connect(imageCapture,SIGNAL(imageCaptured(int,QImage)),this,SLOT(displayImage(int,QImage)));
connect(ui->buttonCapture,SIGNAL(clicked()),this,SLOT(captureImage()));
connect(ui->buttonSave,SIGNAL(clicked()),this,SLOT(saveImage()));
connect(ui->buttonQuit,SIGNAL(clicked()),this,SLOT(close()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::captureImage(){
ui->statusBar->showMessage(tr("capturing..."),10000);
imageCapture->capture();
}
void MainWindow::displayImage(int,QImage image){
ui->ImageCapture->setPixmap(QPixmap::fromImage(image));
ui->statusBar->showMessage(tr("capture OK"),50000);
}
void MainWindow::saveImage(){
QString fileName = QFileDialog::getSaveFileName(this,tr("save file"),QDir::homePath(),tr("jpegfile(*.jpg)"));
if(fileName.isEmpty()){
ui->statusBar->showMessage(tr("save cancel"),50000);
return;
}
const QPixmap* pixmap = ui->ImageCapture->pixmap();
if(pixmap){
pixmap->save(fileName);
ui->statusBar->showMessage(tr("save OK"),50000);
}
}
5. 输出显示:
窗口可以手动调节,我这里设置的是控件随着窗口变化自动调节
实现了:拍照抓取、保存、退出这三个功能。
6. 参考链接:
https://www.cnblogs.com/annt/p/ant_000.html
https://my.oschina.net/u/3413573/blog/876764