Qt实战:如何打开摄像头并实现视频的实时预览

文章目录

    • [一、在 pro 文件中添加模块](#一、在 pro 文件中添加模块)
    • 二、代码实现

一、在 pro 文件中添加模块

在 Qt5 中,如果你要使用 QCamera、QCameraViewfinder、QCameraImageCapture 等类,需要在 pro 文件里添加 multimedia 模块。

cpp 复制代码
QT  += multimedia multimediawidgets
  • multimedia:核心多媒体功能(QCamera、QCameraImageCapture)
  • multimediawidgets:视频显示控件(QCameraViewfinder、QVideoWidget)

检查包含头文件:

cpp 复制代码
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>

二、代码实现

在 Qt5 里,使用的是 QCamera + QCameraViewfinder + QCameraImageCapture。

CameraWindow.h

cpp 复制代码
#pragma once

#include <QWidget>
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>

class CameraWindow : public QWidget
{
    Q_OBJECT
public:
    explicit CameraWindow(QWidget *parent = nullptr);

private slots:
    void onOpenCamera();
    void onCaptureImage();
    void onCloseCamera();
    void onImageCaptured(int id, const QImage &preview);

private:
    QCamera *m_camera = nullptr;
    QCameraViewfinder *m_viewfinder = nullptr;
    QCameraImageCapture *m_imageCapture = nullptr;

    QPushButton *m_btnOpen;
    QPushButton *m_btnCapture;
    QPushButton *m_btnClose;
    QLabel *m_labelPhoto;
};

CameraWindow.cpp

cpp 复制代码
#include "CameraWindow.h"
#include <QDebug>
#include <QDateTime>

CameraWindow::CameraWindow(QWidget *parent) : QWidget(parent)
{
    setWindowTitle("Qt5 摄像头示例");
    resize(800, 600);

    // 预览控件
    m_viewfinder = new QCameraViewfinder(this);
    m_viewfinder->setMinimumSize(640, 480);

    // 按钮
    m_btnOpen = new QPushButton("打开摄像头");
    m_btnCapture = new QPushButton("拍照");
    m_btnClose = new QPushButton("关闭摄像头");

    // 拍照结果显示
    m_labelPhoto = new QLabel;
    m_labelPhoto->setFixedSize(200, 150);
    m_labelPhoto->setStyleSheet("border: 1px solid gray;");

    // 布局
    auto vLayout = new QVBoxLayout(this);
    vLayout->addWidget(m_viewfinder, 1);

    auto hLayout = new QHBoxLayout;
    hLayout->addWidget(m_btnOpen);
    hLayout->addWidget(m_btnCapture);
    hLayout->addWidget(m_btnClose);
    hLayout->addWidget(m_labelPhoto);
    vLayout->addLayout(hLayout);

    // 信号槽
    connect(m_btnOpen, &QPushButton::clicked, this, &CameraWindow::onOpenCamera);
    connect(m_btnCapture, &QPushButton::clicked, this, &CameraWindow::onCaptureImage);
    connect(m_btnClose, &QPushButton::clicked, this, &CameraWindow::onCloseCamera);
}

void CameraWindow::onOpenCamera()
{
    if (m_camera) return; // 已经打开

    m_camera = new QCamera(this);
    m_camera->setViewfinder(m_viewfinder);

    m_imageCapture = new QCameraImageCapture(m_camera, this);
    connect(m_imageCapture, &QCameraImageCapture::imageCaptured,
            this, &CameraWindow::onImageCaptured);

    m_camera->start();
    qDebug() << "摄像头已打开";
}

void CameraWindow::onCaptureImage()
{
    if (!m_imageCapture) return;

    QString filename = QString("photo_%1.jpg")
                           .arg(QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss"));
    m_imageCapture->capture(filename);
    qDebug() << "已拍照保存到:" << filename;
}

void CameraWindow::onCloseCamera()
{
    if (m_camera) {
        m_camera->stop();
        delete m_camera;
        m_camera = nullptr;

        delete m_imageCapture;
        m_imageCapture = nullptr;

        qDebug() << "摄像头已关闭";
    }
}

void CameraWindow::onImageCaptured(int, const QImage &preview)
{
    m_labelPhoto->setPixmap(QPixmap::fromImage(preview).scaled(m_labelPhoto->size(),
                                                               Qt::KeepAspectRatio,
                                                               Qt::SmoothTransformation));
}

main.cpp

cpp 复制代码
#include "CameraWindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    CameraWindow w;
    w.show();

    return a.exec();
}
相关推荐
初阳7854 小时前
【基于Qt5仿音乐播放器(六)】——细节打磨与总结
qt·1024程序员节
小鱼仙官13 小时前
使用Qt封装C/C++ 动态库 用于第三方调用
c语言·c++·qt
扶尔魔ocy16 小时前
【QT OpenGL】知识点编目及传送门
qt·opengl·osg
qq_4017004117 小时前
Qt Modbus 源码剖析:从 QModbusClient 到工业级通信框架
开发语言·qt·系统架构
GreenHandBruce17 小时前
基于Qt 6.12.0 + FFmpeg 9.0.2的4k视频播放器
qt·ffmpeg·音视频
誰能久伴不乏20 小时前
桌面客户端的运行日志系统:SQLite 分块 + 独立写线程 + 文本降级
数据库·c++·qt·架构·sqlite
沫璃染墨1 天前
《Qt从零入门系列(十五):Qt线程安全与锁机制详解——从QMutex到RAII与读写锁实战》
开发语言·c++·qt·安全·交互·线程·qt6.3
小豆庄2 天前
视频脱敏引擎的追踪难题:转场误判定、ID撞号和淡入窗口
qt·算法·目标检测·音视频·视频·敏捷流程
小灰灰搞电子2 天前
Qt 中使用 QSettings 存储结构体数据详解
开发语言·qt
LaoWaiHang2 天前
关于Qt中鼠标左键事件被子控件截留的处理
qt