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;
}
相关推荐
hqxstudying27 分钟前
Java异常处理
java·开发语言·安全·异常
wjs20243 小时前
状态模式(State Pattern)
开发语言
我命由我123453 小时前
Kotlin 数据容器 - List(List 概述、创建 List、List 核心特性、List 元素访问、List 遍历)
java·开发语言·jvm·windows·java-ee·kotlin·list
liulilittle3 小时前
C++ TAP(基于任务的异步编程模式)
服务器·开发语言·网络·c++·分布式·任务·tap
励志要当大牛的小白菜5 小时前
ART配对软件使用
开发语言·c++·qt·算法
爱装代码的小瓶子7 小时前
数据结构之队列(C语言)
c语言·开发语言·数据结构
Maybe_ch8 小时前
.NET-键控服务依赖注入
开发语言·c#·.net
超浪的晨9 小时前
Java UDP 通信详解:从基础到实战,彻底掌握无连接网络编程
java·开发语言·后端·学习·个人开发
终焉暴龙王9 小时前
CTFHub web进阶 php Bypass disable_function通关攻略
开发语言·安全·web安全·php
Edingbrugh.南空10 小时前
Aerospike与Redis深度对比:从架构到性能的全方位解析
java·开发语言·spring