Qt实战:如何打开摄像头并实现视频的实时预览
文章目录
- 一、在 pro 文件中添加模块
- 二、代码实现
一、在 pro 文件中添加模块
在 Qt5 中,如果你要使用 QCamera、QCameraViewfinder、QCameraImageCapture 等类,需要在 pro 文件里添加 multimedia 模块。
QT += multimedia multimediawidgets
- multimedia:核心多媒体功能(QCamera、QCameraImageCapture)
- multimediawidgets:视频显示控件(QCameraViewfinder、QVideoWidget)
检查包含头文件:
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
二、代码实现
在 Qt5 里,使用的是 QCamera + QCameraViewfinder + QCameraImageCapture。
CameraWindow.h
#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
#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
#include "CameraWindow.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);CameraWindow w;w.show();return a.exec();
}