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();
}
相关推荐
hqwest1 天前
码上通QT实战12--监控页面04-绘制6个灯珠及开关
开发语言·qt·qpainter·qt事件·stackedwidget
youyicc1 天前
Qt连接Pg数据库
开发语言·数据库·qt
楚Y6同学1 天前
基于 Haversine 公式实现【经纬度坐标点】球面距离计算(C++/Qt 实现)
开发语言·c++·qt·经纬度距离计算
江公望1 天前
QT/QML qmlRegisterType()函数浅谈
开发语言·qt
ZouZou老师1 天前
Linux Qt出现xcb异常问题解决办法
开发语言·qt
雁门.11 天前
qt封装dll及调用
开发语言·qt
办公自动化软件定制化开发python1 天前
基于PyQt5开发的文件智能查找工具,开源思路+完整实现,解决办公文件检索痛点
开发语言·qt
深蓝海拓1 天前
PySide6,QEventLoop.exec()的使用
笔记·python·qt·学习·pyqt
_OP_CHEN1 天前
【从零开始的Qt开发指南】(二十)Qt 多线程深度实战指南:从基础 API 到线程安全,带你实现高效并发应用
开发语言·c++·qt·安全·线程·前端开发·线程安全
qq_401700411 天前
CardLayout 实现自定义布局
qt