qt5.14.2 opencv调用摄像头显示在label

ui界面添加一个Qlabel名字是默认的label

还有一个button名字是pushButton

mainwindow.h

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include <QMainWindow>
#include <opencv2/opencv.hpp>  // 添加OpenCV头文件
#include <QTimer>              // 添加定时器头文件


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE


class MainWindow : public QMainWindow
{
    Q_OBJECT


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


private slots:
    void on_pushButton_clicked();
    void updateFrame();  // 新增的帧更新槽函数


private:
    Ui::MainWindow *ui;
    cv::VideoCapture cap;  // OpenCV视频捕获对象
    QTimer *timer;         // 定时器对象
};
#endif // MAINWINDOW_H

mainwindow.cpp

cpp 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/opencv.hpp>
#include <QTimer>
#include <QImage>
#include <QPixmap>


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


    // 初始化定时器
    timer = new QTimer(this);


    // 连接信号和槽
    connect(timer, &QTimer::timeout, this, &MainWindow::updateFrame);


    // 设置Label的缩放策略
    ui->label->setScaledContents(true);
}


MainWindow::~MainWindow()
{
    // 释放资源
    if(cap.isOpened()) {
        cap.release();
    }
    if(timer->isActive()) {
        timer->stop();
    }
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    if (!timer->isActive()) {
        // 尝试打开摄像头
        cap.open(0); // 0表示默认摄像头


        if (!cap.isOpened()) {
            ui->label->setText("无法打开摄像头");
            return;
        }


        // 设置摄像头分辨率(可选)
        cap.set(cv::CAP_PROP_FRAME_WIDTH, 640);
        cap.set(cv::CAP_PROP_FRAME_HEIGHT, 480);


        timer->start(30); // 每30毫秒更新一帧
        ui->pushButton->setText("停止摄像头");
    } else {
        // 停止摄像头
        timer->stop();
        cap.release();
        ui->pushButton->setText("启动摄像头");
        ui->label->clear();
    }
}


void MainWindow::updateFrame()
{
    cv::Mat frame;
    cap >> frame; // 从摄像头获取一帧


    if (!frame.empty()) {
        // 将OpenCV的BGR格式转换为RGB
        cv::cvtColor(frame, frame, cv::COLOR_BGR2RGB);


        // 将cv::Mat转换为QImage
        QImage img(frame.data,
                  frame.cols,
                  frame.rows,
                  frame.step,
                  QImage::Format_RGB888);


        // 将QImage转换为QPixmap并显示在Label上
        ui->label->setPixmap(QPixmap::fromImage(img));
    }
}
相关推荐
charlie1145141918 小时前
现代Qt开发教程(新手篇)2.3——QImage、QPixmap、QIcon 图像处理基础
开发语言·图像处理·qt
AoDeLuo11 小时前
SOEM2.0编译与Qt调用
qt·机器视觉
大树学长12 小时前
【QT开发】Windows 10 + Qt 5.15.2 手动编译安装 Qt OPC UA 模块完整记录
开发语言·windows·qt
爱炸薯条的小朋友12 小时前
全局锁的性能优势,以及链路优化为何常常低于预期——基于 `MatPoolsTest` 中小图池与大图池的实战复盘
opencv·算法·c#
m0_6174939413 小时前
OpenCV报错解决:cornerSubPix断言失败 src.channels() == 1 的终极指南
人工智能·opencv·计算机视觉
小短腿的代码世界13 小时前
Qt低级网络编程与零拷贝技术在高频交易中的应用:从QTcpSocket到共享内存的全链路优化
开发语言·网络·qt
qq_4017004114 小时前
Qt 自定义无边框窗口:标题栏、拖拽移动与缩放
开发语言·qt
xiaoye-duck16 小时前
Qt 信号与槽深度解析:connect 用法、自定义信号槽与 Lambda 实战
开发语言·qt
郝学胜-神的一滴18 小时前
Qt 高级开发 008: 使用QSetting记住上次打开路径
开发语言·c++·qt·开源软件
W.W.H.19 小时前
Qt 应用防多开:极简单例方案
开发语言·qt·单例模式·共享内存