Qt界面使用OpenCV、RTSP加载网络摄像头,并实时显示

Qt界面使用OpenCV、RTSP加载网络摄像头,并实时显示

注意:

1、Qt界面加载网络摄像头,并实时显示
2、使用OpenCV、RTSP打开摄像头
3、适合初学者
4、本人使用Qt5.10.0、vs2015、opencv3.4.3,必须在运行根目录添加opencv动态库或将此库添加环境变量

代码:

********************LoadCamera.pro********************

#-------------------------------------------------
#
# Project created by QtCreator 2020-04-07T08:48:03
#
#-------------------------------------------------
 
QT       += core gui
 
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 
TARGET = LoadCamera
TEMPLATE = app
 
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
 
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
 
 
SOURCES += \
        main.cpp \
    cframe.cpp \
    CReadVideo.cpp
 
HEADERS += \
    cframe.h \
    CReadVideo.h
 
INCLUDEPATH += \
D:\Softwares\opencv3.4.3\build\include\
D:\Softwares\opencv3.4.3\build\include\opencv\
D:\Softwares\opencv3.4.3\build\include\opencv2\
 
win32:CONFIG(release, debug|release): LIBS += -LD:/Softwares/opencv3.4.3/build/x64/vc15/lib/ -lopencv_world343
else:win32:CONFIG(debug, debug|release): LIBS += -LD:/Softwares/opencv3.4.3/build/x64/vc15/lib/ -lopencv_world343d
********************main.cpp********************
#include "cframe.h"
#include <QApplication>
 
int main(int argc, char *argv[])
{
 
    QApplication a(argc, argv);
    CFrame w;
    w.show();
 
    return a.exec();
}
********************cframe.h********************
#ifndef CFRAME_H
#define CFRAME_H
 
#include <QFrame>
#include <CReadVideo.h>
#include <QLabel>
 
class CFrame : public QFrame
{
 
    Q_OBJECT
 
public:
    explicit CFrame(QWidget *parent = 0);
    ~CFrame();
 
protected:
    //窗体改变大小
    void resizeEvent(QResizeEvent* event) override;
 
protected slots:
    //接收摄像头数据
    void slotRecviveQimage(const QImage& image);
 
private:
    //摄像头显示在的QLabel控件
    QLabel *m_LabelCameraView;
    //读视频线程
    CReadVideo* m_ReadVideo;
};
 
#endif // CFRAME_H

********************cframe.cpp********************

#include "cframe.h"
 
CFrame::CFrame(QWidget *parent) :
    QFrame(parent)
{
 
    resize(960, 540);
    this->setMinimumWidth(192);
    this->setMinimumHeight(108);
 
    m_LabelCameraView = new QLabel(this);
    m_LabelCameraView->setGeometry(0, 0, this->width(), this->height());
 
    //海康摄像头rtsp://admin:admin12345@192.168.10.100/h264/ch1/main/av_stream       ch2是辅码流
    //大华摄像头rtsp://admin:zhhh6052@192.168.2.5:554/cam/realmonitor?channel=1&subtype=0   subtype=1是辅码流
    m_ReadVideo = new CReadVideo(this);
    m_ReadVideo->setVideoAddress("rtsp://admin:zhhh6052@10.0.105.101:554/cam/realmonitor?channel=1&subtype=0");
    connect(m_ReadVideo, SIGNAL(sendQImage(const QImage)), this, SLOT(slotRecviveQimage(const QImage)));
    m_ReadVideo->start();
}
 
CFrame::~CFrame()
{
 
    m_ReadVideo->stop();
    delete m_ReadVideo;
    m_ReadVideo = nullptr;
    delete m_LabelCameraView;
    m_LabelCameraView = nullptr;
}
 
void CFrame::resizeEvent(QResizeEvent *event)
{
 
    Q_UNUSED(event);
 
    //界面大小修改,则显示窗口跟随改变
    m_LabelCameraView->setGeometry(0, 0, this->width(), this->height());
}
 
void CFrame::slotRecviveQimage(const QImage& image)
{
 
    if(this->isHidden())
        return;
 
    m_LabelCameraView->setPixmap(QPixmap::fromImage(image));
}

********************creadvideo.h********************

#ifndef CREADVIDEO_H
#define CREADVIDEO_H
 
#include <QThread>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/videoio/videoio.hpp>
 
class CReadVideo : public QThread
{
 
    Q_OBJECT
public:
    explicit CReadVideo(QObject *parent = nullptr);
    ~CReadVideo();
    void stop();
 
    //设置网络摄像头地址
    void setVideoAddress(const QString& str);
 
signals:
    void sendQImage(const QImage&);
 
protected:
    virtual void run();
 
private:
    bool stopped;
 
    cv::VideoCapture m_VideoCapture;
    //Mat数据
    cv::Mat m_MatRead, m_MatRGB;
    //网络摄像头地址
    QString m_sVideoAddress;
};
 
#endif // CREADVIDEO_H

********************CReadVideo.cpp********************

#include "CReadVideo.h"
#include <QImage>
 
CReadVideo::CReadVideo(QObject *parent) : QThread(parent)
{
 
    stopped = false;
    m_sVideoAddress = "";
}
 
CReadVideo::~CReadVideo()
{
 
    stopped = true;
    if(this->isRunning())
    {
 
        this->stop();
        //等待1500毫秒,线程退出
        for(int i=0; i<300; ++i)
        {
 
            if(stopped == false)
                break;
            //qDebug()<<"wait stop"<<i<<stopped;
            msleep(5);
        }
    }
}
 
void CReadVideo::stop()
{
 
    stopped = true;
}
 
void CReadVideo::setVideoAddress(const QString& str)
{
 
    m_sVideoAddress = str;
}
 
void CReadVideo::run()
{
 
    stopped = false;
 
    while(!stopped)
    {
 
        try{
 
            //打开摄像头
            if(!m_VideoCapture.open(m_sVideoAddress.toStdString()))
            {
 
                sleep(6);
                continue;
            }
 
            while(!stopped)
            {
 
                if(!stopped)
                {
 
                    //读取摄像头视频
                    if(m_VideoCapture.read(m_MatRead))
                    {
 
                        //OpenCV使用BGR,QImage使用RGB,通道转换
                        cv::cvtColor(m_MatRead, m_MatRGB, CV_BGR2RGB);
                        //cv::Mat转QImage
                        QImage image = QImage(m_MatRGB.data, m_MatRGB.cols, m_MatRGB.rows, QImage::Format_RGB888);
                        //发送转换完成的QImage信号
                        if(!stopped)
                            emit sendQImage(image);
                    }
                    else
                    {
 
                        break;
                    }
                }
                else
                {
 
                    break;
                }
            }
        }
        catch(QString sException)
        {
 
            Q_UNUSED(sException);
            sleep(60);
            continue;
        }
    }
 
    //m_VideoCapture.release();
    stopped = false;
}

********************结束********************

整体代码,请见我的资源https://download.csdn.net/download/u011093735/15082017

    原文作者:非鹏爱贴2012
    原文地址: https://blog.csdn.net/u011093735/article/details/113699229
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞