.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QCamera>
#include <QCameraViewfinder>
#include <QMediaRecorder>
#include <QVBoxLayout>#include <QCameraImageCapture>
#include <QCamera>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void on_scan_camera_clicked();void on_Star_camera_clicked();void on_Stop_camera_clicked();private:Ui::MainWindow *ui;QCamera *camera;QCameraViewfinder *viewfinder;QMediaRecorder *recorder;QCamera *ca;
};#endif // MAINWINDOW_H
.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QCameraInfo>
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QPixmap>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);QList<QCameraInfo> infos = QCameraInfo::availableCameras();QString camera = infos.value(0).deviceName();ui->camera_cb->addItem(infos.value(0).description());ca =new QCamera(camera.toUtf8() ,this );ui->camera->show();QCameraViewfinder *v2 = new QCameraViewfinder(ui->camera);v2->resize(ui->camera->size());ca->setViewfinder(v2);v2->show();
}MainWindow::~MainWindow()
{//camera->stop();delete ui;
}void MainWindow::on_scan_camera_clicked()
{ui->camera_cb->clear();QList<QCameraInfo> infos = QCameraInfo::availableCameras();QString camera = infos.value(0).deviceName();ui->camera_cb->addItem(infos.value(0).description());
}void MainWindow::on_Star_camera_clicked()
{ca->start();
}void MainWindow::on_Stop_camera_clicked()
{ca->stop();
}
.ui界面