QT中调用实时视频并设计界面

在QT中调用本地视频主要使用QMediaPlay进行播放,
首先要在设计界面放一个播放框架,进行布局后进行编程
在源文件里添加需要播放的视频
在QT中.pro文件添加

QT       += multimedia multimediawidgets
_player = new QMediaPlayer; 
_videoSurface = new baseVideoSurface();
_player->setVideoOutput(_videoSurface);
connect(_videoSurface,&baseVideoSurface::signalShowImage,_videoImageLa,&videoWidget::slotSetImage);
_player->setMedia(QUrl::fromLocalFile("C:/Users/123/Desktop/tuLei.mp4"));      
     connect(_player,&QMediaPlayer::stateChanged,this,&videoPlayerWidget::slotStateChanged);
#ifndef PLAYITEM_H
#define PLAYITEM_H

#include <QObject>
#include <QGraphicsItem>
#include <QImage>

class PlayItem : public QObject, public QGraphicsPixmapItem
{
    Q_OBJECT
public:
    explicit PlayItem(QObject *parent = nullptr);
    PlayItem(const QImage &qimg);
    ~PlayItem();
    void setQImage(const QImage &qimg);
};

#endif // PLAYITEM_H

下面定义显示的界面

#ifndef PLAYWIDGET_H
#define PLAYWIDGET_H

#include <QGraphicsView>
#include <QGraphicsScene>
#include <opencv2/core/core.hpp>
#include "PlayItem.h"

class PlayWidget : public QGraphicsView
{
    Q_OBJECT
public:
    PlayWidget();

public slots:
    void getResult(const QImage &qimg);


private:
//    QImage Mat2QImage(const cv::Mat &img);
    void updateScene();

    // event
    void wheelEvent(QWheelEvent *event);

private:
    PlayItem* m_play_item_ptr;
    QGraphicsScene* m_img_scene_ptr;
    cv::Mat m_img;

    double m_zoom_factor;
};

#endif // PLAYWIDGET_H
#include "PlayWidget.h"

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <QWheelEvent>


PlayWidget::PlayWidget()
{
    m_img_scene_ptr = new QGraphicsScene();
    this->setScene(m_img_scene_ptr);
    m_play_item_ptr = new PlayItem();
    m_img_scene_ptr->addItem(m_play_item_ptr);

    double width_scale = double(this->width()) / double(1280);
    double height_scale = double(this->height()) / double(720);
    m_zoom_factor = width_scale < height_scale ? width_scale : height_scale;
    this->updateScene();
}

void PlayWidget::getResult(const QImage &qimg)
{
    m_play_item_ptr->setQImage(qimg);
}

void PlayWidget::updateScene()
{
    QMatrix matrix;
    matrix.scale(m_zoom_factor, m_zoom_factor);
    this->setMatrix(matrix);
}

void PlayWidget::wheelEvent(QWheelEvent *event)
{
    if(event->delta() > 0)
        m_zoom_factor *= 1.15;
    else
        m_zoom_factor *= 0.9;
    this->updateScene();
}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值