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();
}
相关推荐
用户805533698034 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner4 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz9 天前
QML Hello World 入门示例
qt
xcyxiner12 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner13 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner13 天前
DicomViewer (添加模型类)3
qt
xcyxiner14 天前
DicomViewer (目录调整) 2
qt
xcyxiner14 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能16 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G16 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt