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;
}
相关推荐
coder!mq5 小时前
说几个常见的语法糖?
java·开发语言·算法
gugucoding5 小时前
38. 【Java】Stream API(下):高级操作与性能
java·开发语言
Lhan.zzZ6 小时前
在 Visual Studio 2022 中打造可扩展的动态链接库模块:从零搭建到原理解析
开发语言·c++·visual studio
心平气和量大福大6 小时前
android-实例-蒲公英-更新与安装-5-签名与生成APK
android·java·开发语言
码哥DFS6 小时前
构造函数、实例对象、对象原型 ----三者关系
开发语言·javascript·原型模式
quantdash_cc7 小时前
告别自建 Requests/BS4 网页爬虫:基于 QuantDash 搭建零维保的高性能量化行情流水线
开发语言·爬虫·python·pandas·量化·quantdash
ydd1001007 小时前
寻找两个正序数组的中位数 Java 题解,二分分割详解
java·开发语言
半亩码田7 小时前
C#转Python第3.1篇:Python 的 class 没有访问修饰符?面向对象的另一条路
开发语言·python·c#
Wzx1980128 小时前
python沙箱和docker沙箱你选对了吗?
开发语言·python·docker
牧羊人.3338 小时前
Python 办公自动化从入门到入土|09 数据容器之字典
开发语言·python