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();
}
相关推荐
打码的猿21 小时前
Qt对话框不锁死主程序的方法
开发语言·qt
小小码农Come on1 天前
Qt Creator常用设置
qt
wkm9561 天前
在arm64 ubuntu系统安装Qt后编译时找不到Qt3DExtras头文件
开发语言·arm开发·qt
小小码农Come on1 天前
QT开发环境安装
开发语言·qt
小小码农Come on1 天前
QT内存管理
开发语言·qt
有理想的打工人1 天前
QT的安装
qt
SilentSlot1 天前
【QT-QML】8. 输入元素
qt·qml
是店小二呀1 天前
Visual Studio C++ 工程架构深度解析:从 .vcxproj 到 Qt MOC 的文件管理实录
c++·qt·visual studio
枫叶丹41 天前
【Qt开发】Qt系统(十二)-> Qt视频
c语言·开发语言·c++·qt·音视频
浅碎时光8071 天前
Qt (信号与槽 Widget控件 qrc文件)
开发语言·qt