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();
}
相关推荐
ShineWinsu11 小时前
对于Qt窗口:QMainWindow、QMenuBar、Qmenu、QAction的解析
c++·qt·菜单·qmainwindow·qaction·qmenu·qmenubar
ShineWinsu16 小时前
对于Qt:QSpacerItem常见属性以及方法的解析
开发语言·c++·qt·面试·系统架构·客户端·qspaceritem
ShineWinsu19 小时前
对于Qt:QGripLayout常见属性、方法以及QSizePolicy的解析
开发语言·c++·qt·面试·gui·客户端·网格
艾莉丝努力练剑1 天前
【QT】系统相关:TCP的API介绍
开发语言·前端·c++·人工智能·qt·学习·tcp/ip
ShineWinsu1 天前
对于Qt:QVBoxLayout常见属性以及方法的解析
开发语言·数据库·c++·qt·gui·客户端·布局管理器
ShineWinsu1 天前
对于Qt:QFormLayout常见属性以及方法的解析
开发语言·c++·qt·面试·客户端·表单·qformlayout
ShineWinsu3 天前
对于Qt:QHBoxLayout常见属性以及方法的解析
c++·qt·面试·gui·客户端·布局管理器·水平布局器
敲上瘾3 天前
Qt核心机制:信号与槽详解
开发语言·c++·qt·ui·qt6.3
Sakura_j_h_4 天前
MCP SDK 2.0 实战:用 MCPServer 搭本地笔记检索服务
开发语言·笔记·python·qt·ai工具·mcp·mcpserver
ShineWinsu5 天前
对于Qt:QTreeWidget常见属性以及QTreeWidgetItem类的解析
开发语言·c++·qt·gui·客户端·多叉树·qtreewidget