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 手撸视频播放器,实现自己的看片神器的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/cjjbc/10214.html