当前位置:网站首页 > C++编程 > 正文

Opencv加QOpenGLWidget 手撸视频播放器,实现自己的看片神器

Opencv加QOpenGLWidget 手撸视频播放器,实现自己的看片神器

开发环境:
VS2017+QtQt5.9.3+Opencv3.4.3

废话不多说,直接上代码

MyXVideo.h

#pragma once #include <QOpenGLWidget> #include<QTimerEvent> #include <QPaintEvent> #include "opencv2/core.hpp" #include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #include <QImage> class XVedioWidght:public QOpenGLWidget { 
    Q_OBJECT public: XVedioWidght(QWidget *parent); virtual ~XVedioWidght(); void timerEvent(QTimerEvent*event); void slotOpenVedio(); QImage cvMat2QImage(const cv::Mat& mat); void paintEvent(QPaintEvent *e); public: cv::Mat m_mat; }; 

MyXVideo.cpp

#include "XVedioWidght.h" #include <QPainter> #include <QFileDialog> #include <QDebug> using namespace cv; VideoCapture cp; XVedioWidght::XVedioWidght(QWidget *parent):QOpenGLWidget(parent) { 
    startTimer(40); } XVedioWidght::~XVedioWidght() { 
    } void XVedioWidght::timerEvent(QTimerEvent*event) { 
    update(); } void XVedioWidght::slotOpenVedio() { 
    qDebug() << "slotOpenVedio"; QString name = QFileDialog::getOpenFileName(); if (name.isEmpty()) { 
    return; } bool bret = cp.open(name.toStdString()); if (!bret) { 
    return; } } QImage XVedioWidght::cvMat2QImage(const cv::Mat& mat) { 
    switch (mat.type()) { 
    // 8-bit, 4 channel case CV_8UC4: { 
    QImage image(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB32); return image; } // 8-bit, 3 channel case CV_8UC3: { 
    QImage image(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888); return image.rgbSwapped(); } // 8-bit, 1 channel case CV_8UC1: { 
    static QVector<QRgb> sColorTable; // only create our color table once if (sColorTable.isEmpty()) { 
    for (int i = 0; i < 256; ++i) sColorTable.push_back(qRgb(i, i, i)); } QImage image(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8); image.setColorTable(sColorTable); return image; } default: qDebug("Image format is not supported: depth=%d and %d channels\n", mat.depth(), mat.channels()); break; } return QImage(); } void XVedioWidght::paintEvent(QPaintEvent *e) { 
    if (!cp.isOpened()) { 
    return; } cp.read(m_mat); if (m_mat.empty()) { 
    return; } QImage image = cvMat2QImage(m_mat); QPainter* painter = new QPainter; painter->begin(this); painter->drawImage(QPoint(0, 0), image); painter->end(); } 

欢迎大佬们多多提意见和建议,目前测试通过,如果这篇文章对你有帮助请打赏小编,让我更有动力写技术博客,蟹蟹!!

 https://img-blog.csdnimg.cn/.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjMwOTU2NQ==,size_16,color_FFFFFF,t_70#pic_center 
到此这篇Opencv加QOpenGLWidget 手撸视频播放器,实现自己的看片神器的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • 中英双语介绍中国香港(Hong Kong, China)2024-11-23 14:00:10
  • C++STL学习笔记(第一篇:stl是什么?为什么要学习stl?迭代器在stl中扮演着什么角色?)2024-11-23 14:00:10
  • alice编程简单案例_微软编程之美2024-11-23 14:00:10
  • c++并发编程_多线程单例模式2024-11-23 14:00:10
  • cobol代码示例_c++项目实战2024-11-23 14:00:10
  • Opencv加QOpenGLWidget 手撸视频播放器,实现自己的看片神器2024-11-23 14:00:10
  • Opencv加QOpenGLWidget 手撸视频播放器,实现自己的看片神器2024-11-23 14:00:10
  • 发表一篇SCI论文到底有多难?审稿需要多久?2024-11-23 14:00:10
  • 好用的字体图标插件:阿里图标库(iconfont)2024-11-23 14:00:10
  • 十二星座英语名称(Zodiac)2024-11-23 14:00:10
  • 全屏图片