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;
}
相关推荐
时空系3 分钟前
第10篇:继承扩展——面向对象编程进阶 python中文编程
开发语言·python·ai编程
CHANG_THE_WORLD1 小时前
python 批量终止进程exe
开发语言·python
古城小栈1 小时前
从 cargo-whero 库中,找到提升 rust 的契机
开发语言·后端·rust
Gary Studio3 小时前
安卓HAL C++基础-智能指针
开发语言·c++
啧不应该啊3 小时前
Day1 Python 与 C 的类型区别
c语言·开发语言
cen__y3 小时前
Linux07(信号01)
linux·运维·服务器·c语言·开发语言
xingpanvip3 小时前
星盘接口开发文档:星相日历接口指南
android·开发语言·前端·css·php·lua
guygg884 小时前
基于遗传算法的双层规划模型求解MATLAB实现
开发语言·matlab
凯瑟琳.奥古斯特4 小时前
SQLAlchemy核心功能解析
开发语言·python·flask
卷Java4 小时前
GPTQ vs AWQ vs GGUF:模型量化工具横向测评
开发语言·windows·python