qt简单连接摄像头

要使用摄像头,就需要链接多媒体模块 以及多媒体工具模块

需要在.pro文件中添加QT += multimedia multimediawidgets

是用的库文件

QCamera 类用于打开系统的摄像头设备,

QCameraViewfinder 用于显示捕获的视频,

QCameraImageCapture 用于截图。

mainwindows.h文件

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
//摄像头
#include <QCamera>
#include <QCameraImageCapture>

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QCamera *ca;
    QCameraImageCapture *capture;
};
#endif // MAINWINDOW_H

mainwindows.cpp

cpp 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
//调用摄像头
#include <QCameraInfo>
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QPixmap>
//弹窗
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //获取可用摄像头设备并输出在控制台
    QList<QCameraInfo> infos = QCameraInfo::availableCameras();
    qDebug() << infos.value(0).deviceName() << ":" <<infos.value(0).description();
    QString camera = infos.value(0).deviceName();
    qDebug() << camera;
    //显示摄像头
    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();
    ca->start();
    capture = new QCameraImageCapture(ca);
    capture->capture("home/hua/XXX/.jpg");
    QMessageBox::information(this, "ok", "ok!");
}

MainWindow::~MainWindow()
{
    delete ui;
}
相关推荐
wjs20242 分钟前
SQL LEN() 函数详解
开发语言
姓刘的哦7 分钟前
Qt自定义控件
开发语言·qt
Ricky_Theseus8 分钟前
C++静态库
开发语言·c++
SuperEugene9 分钟前
Python 异步 async/await:为什么 AI 框架大量使用?| 基础篇
开发语言·人工智能·python
SMF191914 分钟前
【uv】Python包管理器uv安装和应用
开发语言·python·uv
Lyyaoo.15 分钟前
【JAVA基础面经】String、StringBuffer、StringBuilder
java·开发语言
蓝色的杯子17 分钟前
Python面试30分钟突击掌握-LeetCode1-Array
开发语言·python·面试
Kiri霧17 分钟前
Kotlin递归
android·开发语言·kotlin
范纹杉想快点毕业20 分钟前
Zynq开发视角下的C语言能力分级详解
c语言·开发语言
常利兵22 分钟前
Kotlin抽象类与接口:相爱相杀的编程“CP”
android·开发语言·kotlin