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;
}
相关推荐
码界索隆2 分钟前
Python转Java系列:语法与类型系统
java·开发语言·python
ch.ju4 分钟前
Java程序设计(第3版)第四章——编译中的错误:无法覆盖
java·开发语言
阿正的梦工坊7 分钟前
【Rust】15-Rust 内存布局、Drop 顺序与 unsafe 边界
开发语言·rust
我认不到你8 分钟前
【开源、教程】RAG全流程实现(java+完整代码):第二弹
java·开发语言·人工智能·深度学习·ai·语言模型·开源
AKA__Zas20 分钟前
初识多线程plus(2.0)
java·开发语言·学习方法
Rabitebla21 分钟前
C++ 多态详解:从概念到虚表底层原理(代码轰炸)
开发语言·c++
砍材农夫32 分钟前
python 如何一次性安装项目所有依赖包(pip和uv)
开发语言·python·pip·uv
IpdataCloud33 分钟前
信贷审核中如何验证用户地址与IP属地一致性?用IP查询工具实现反欺诈
开发语言·tcp/ip·金融·php·ip
云水-禅心42 分钟前
解决MacOS 安装Python之后默认版本指向不正确问题
开发语言·python·macos
冰暮流星44 分钟前
javascript之this关键字
开发语言·前端·javascript