[Qt]使用opencv显示摄像头

——————.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
#include

class CameraWidget : public QWidget {
Q_OBJECT

public:
explicit CameraWidget(QWidget *parent = nullptr);

private slots:
void updateFrame();

private:
QLabel *label;
cv::VideoCapture capture;
};

#endif // CAMERAWIDGET_H
—————————————–CameraWidget.cpp—————–
#include “CameraWidget.h”
#include
#include
#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)) {
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); // 暂停以让设置生效

// 检查实际分辨率
int width = capture.get(cv::CAP_PROP_FRAME_WIDTH);
int height = capture.get(cv::CAP_PROP_FRAME_HEIGHT);
qDebug() << "当前分辨率:" << width << "x" << height; // 设置定时器定时更新摄像头画面 QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &CameraWidget::updateFrame); timer->start(20); // 每 5 毫秒更新一次
}

void CameraWidget::updateFrame() {
if (!capture.isOpened()) {
qDebug() << "摄像头未打开"; return; } cv::Mat frame; capture >> frame; // 从摄像头获取一帧图像
if (frame.empty()) {
qDebug() << "获取的帧为空"; 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

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

———————–mainwindow.cpp———————————
#include “mainwindow.h”
#include “ui_mainwindow.h”

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);

}

MainWindow::~MainWindow()
{
delete ui;
}

—————————-main.cpp————————————–
#include
#include “CameraWidget.h”

int main(int argc, char *argv[]) {
QApplication app(argc, argv);
CameraWidget cameraWidget;
cameraWidget.setWindowTitle(“USB 摄像头画面”);
cameraWidget.resize(640, 480);
cameraWidget.show();
return app.exec();
}

发表评论

邮箱地址不会被公开。 必填项已用*标注