————————.pro————————————-
QT += core gui
QT += core gui widgets # 确保 widgets 模块被包含
CONFIG += link_pkgconfig
PKGCONFIG += opencv4
QT += multimedia multimediawidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
CameraWidget.cpp \
main.cpp \
mainwindow.cpp
HEADERS += \
CameraWidget.h \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
———————-CameraWidget.h—————————-
#ifndef CAMERAWIDGET_H
#define CAMERAWIDGET_H
#include
#include
#include
#include
class CameraWidget : public QWidget {
Q_OBJECT
public:
explicit CameraWidget(QWidget *parent = nullptr);
~CameraWidget();
private slots:
void updateFrame();
private:
QLabel *label; // 用于显示摄像头画面
cv::VideoCapture capture; // OpenCV 摄像头捕获对象
};
#endif // CAMERAWIDGET_H
—————CameraWidget.cpp—————————————-
#include “CameraWidget.h”
#include
#include
#include
#include
CameraWidget::CameraWidget(QWidget *parent) : QWidget(parent) {
QVBoxLayout *layout = new QVBoxLayout(this);
label = new QLabel(this);
layout->addWidget(label);
setLayout(layout);
// 打开摄像头
if (!capture.open(0)) { // 0 为默认摄像头
label->setText(“无法打开摄像头”);
return;
}
// 设置摄像头分辨率和其他属性
capture.set(cv::CAP_PROP_FRAME_WIDTH, 640); // 设置宽度
capture.set(cv::CAP_PROP_FRAME_HEIGHT, 480); // 设置高度
capture.set(cv::CAP_PROP_FPS, 15); // 设置帧率
capture.set(cv::CAP_PROP_EXPOSURE, -4); // 设置曝光(根据需要调整)
capture.set(cv::CAP_PROP_GAIN, 0); // 设置增益(根据需要调整)
QThread::msleep(100); // 暂停以让设置生效
// 设置定时器定时更新摄像头画面
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &CameraWidget::updateFrame);
timer->start(33); // 每 33 毫秒更新一次(约 30 FPS)
}
CameraWidget::~CameraWidget() {
capture.release(); // 释放摄像头资源
}
void CameraWidget::updateFrame() {
cv::Mat frame;
capture >> frame; // 从摄像头获取一帧图像
if (frame.empty()) return;
// 转换为 RGB 格式并显示
cv::cvtColor(frame, frame, cv::COLOR_BGR2RGB);
QImage image(frame.data, frame.cols, frame.rows, frame.step[0], QImage::Format_RGB888);
label->setPixmap(QPixmap::fromImage(image));
}
—————————-mainwindow.h—————————–
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include “CameraWidget.h”
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
CameraWidget *cameraWidget; // 摄像头小部件
};
#endif // MAINWINDOW_H
—————————-mainwindow.cpp—————————
#include “mainwindow.h”
#include “ui_mainwindow.h”
#include
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
// 创建并设置摄像头小部件
cameraWidget = new CameraWidget(this);
// 将摄像头小部件添加到布局
QWidget *cameraContainer = ui->cameraContainer; // 确保在 .ui 文件中设置了 cameraContainer
QVBoxLayout *layout = new QVBoxLayout(cameraContainer);
layout->addWidget(cameraWidget);
}
MainWindow::~MainWindow() {
delete ui;
}
———————-main.cpp—————————————-
#include
#include “mainwindow.h”
int main(int argc, char *argv[]) {
QApplication app(argc, argv); // 创建 QApplication 对象
MainWindow mainWindow; // 创建主窗口对象
mainWindow.show(); // 显示主窗口
return app.exec(); // 进入应用程序主事件循环
}
————————-ui————————————
新建个名字为 cameraContainer 的QWidget控件