opencv+QT 视频播放+滚动条

opencv2.4.0+QT5.1+VS2010  视频播放处理(加滑动条+播放暂停键)

 

ui_qtvideo_2.h

/********************************************************************************
** Form generated from reading UI file 'qtvideo_2.ui'
**
** Created by: Qt User Interface Compiler version 5.1.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_QTVIDEO_2_H
#define UI_QTVIDEO_2_H

#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLCDNumber>
#include <QtWidgets/QLabel>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSlider>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_qtvideo_2Class
{
public:
    QPushButton *playBtn;
    QPushButton *zoomBtn;
    QLCDNumber *lcdNumber;
    QSlider *slider;
    QLabel *label;
    QPushButton *exitBtn;

    void setupUi(QWidget *qtvideo_2Class)
    {
        if (qtvideo_2Class->objectName().isEmpty())
            qtvideo_2Class->setObjectName(QStringLiteral("qtvideo_2Class"));
        qtvideo_2Class->resize(1053, 391);
        playBtn = new QPushButton(qtvideo_2Class);
        playBtn->setObjectName(QStringLiteral("playBtn"));
        playBtn->setGeometry(QRect(10, 320, 91, 61));
        playBtn->setStyleSheet(QLatin1String("background-color: rgba(0, 170, 255, 150);\n"
"color: rgb(0, 0, 255);\n"
"\n"
"font: 75 12pt \"Arial Unicode MS\";"));
        zoomBtn = new QPushButton(qtvideo_2Class);
        zoomBtn->setObjectName(QStringLiteral("zoomBtn"));
        zoomBtn->setGeometry(QRect(110, 320, 91, 61));
        zoomBtn->setStyleSheet(QLatin1String("background-color: rgba(0, 170, 255, 150);\n"
"color: rgb(0, 0, 255);\n"
"font: 75 12pt \"Arial Unicode MS\";"));
        lcdNumber = new QLCDNumber(qtvideo_2Class);
        lcdNumber->setObjectName(QStringLiteral("lcdNumber"));
        lcdNumber->setGeometry(QRect(310, 320, 241, 61));
        lcdNumber->setStyleSheet(QLatin1String("background-color: rgba(0, 170, 255, 150);\n"
"font: 75 10pt \"Aharoni\";\n"
"color: rgb(0, 0, 255);"));
        lcdNumber->setDigitCount(19);
        lcdNumber->setProperty("value", QVariant(0));
        slider = new QSlider(qtvideo_2Class);
        slider->setObjectName(QStringLiteral("slider"));
        slider->setGeometry(QRect(70, 300, 481, 21));
        slider->setStyleSheet(QStringLiteral("color: rgba(0, 170, 255, 200);"));
        slider->setOrientation(Qt::Horizontal);
        label = new QLabel(qtvideo_2Class);
        label->setObjectName(QStringLiteral("label"));
        label->setGeometry(QRect(10, 300, 61, 20));
        label->setStyleSheet(QLatin1String("color: rgb(0, 0, 255);\n"
"font: 75 12pt \"Arial Unicode MS\";"));
        exitBtn = new QPushButton(qtvideo_2Class);
        exitBtn->setObjectName(QStringLiteral("exitBtn"));
        exitBtn->setGeometry(QRect(210, 320, 91, 61));
        exitBtn->setStyleSheet(QLatin1String("background-color: rgba(0, 170, 255, 150);\n"
"color: rgb(0, 0, 255);\n"
"font: 75 12pt \"Arial Unicode MS\";"));

        retranslateUi(qtvideo_2Class);
        QObject::connect(playBtn, SIGNAL(clicked()), qtvideo_2Class, SLOT(playClicked()));
        QObject::connect(zoomBtn, SIGNAL(clicked()), qtvideo_2Class, SLOT(zoomClicked()));
        QObject::connect(exitBtn, SIGNAL(clicked()), qtvideo_2Class, SLOT(exitClicked()));

        QMetaObject::connectSlotsByName(qtvideo_2Class);
    } // setupUi

    void retranslateUi(QWidget *qtvideo_2Class)
    {
        qtvideo_2Class->setWindowTitle(QApplication::translate("qtvideo_2Class", "qtvideo_2", 0));
        playBtn->setText(QApplication::translate("qtvideo_2Class", "pause", 0));
        zoomBtn->setText(QApplication::translate("qtvideo_2Class", "zoom", 0));
        label->setText(QApplication::translate("qtvideo_2Class", "Process", 0));
        exitBtn->setText(QApplication::translate("qtvideo_2Class", "Exit", 0));
    } // retranslateUi

};

namespace Ui {
    class qtvideo_2Class: public Ui_qtvideo_2Class {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_QTVIDEO_2_H

 

 

 

qtvideo_2.h

 

#ifndef QTVIDEO_2_H
#define QTVIDEO_2_H

#include <QtWidgets/QWidget>
#include "ui_qtvideo_2.h"
#include <QImage>
#include <QTimer>
#include <QTime>
#include <QPainter>
#include <cv.h>
#include <highgui.h>


class qtvideo_2 : public QWidget
{
 Q_OBJECT

public:
 qtvideo_2(const char *fileName, QWidget *parent = 0);
 ~qtvideo_2();
 void timeUpdate();
protected:  
 void paintEvent(QPaintEvent *e); 
private slots:  
  void nextFrame();
  void playClicked();
  void zoomClicked();
  void exitClicked();
  void showTime();
  void CallBackTrackBarSlide();

private:
 Ui::qtvideo_2Class ui;
 CvCapture *capture;  
 IplImage *iplImg;
 IplImage *iplImg1;//处理后的视频帧
 IplImage *frame;
 IplImage *frame1;//处理后的视频帧
 QImage *qImg;
 QImage *qImg1;//处理成QT识别的视频帧
 QTimer *timer; //视频播放定时器
 QTimer *timer1; //电子时钟定时器
 bool flag;//视频播放暂停标志位
 int g_Pos;//视频帧位置
};


#endif // QTVIDEO_2_H

 

 

 

qtvideo_2.cpp

#include "qtvideo_2.h"

qtvideo_2::qtvideo_2(const char *fileName,QWidget *parent)
 : QWidget(parent)
{
 ui.setupUi(this);
 timeUpdate();
 capture = cvCaptureFromFile(fileName);
 int totalFrames =  (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT); 
 g_Pos=0;
 ui.slider->setRange(0, totalFrames);
 QObject::connect(ui.slider, SIGNAL(valueChanged(int)),this, SLOT(CallBackTrackBarSlide()));
 CvSize size = cvSize( (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT) );
 flag=true;
 //capture = cvCaptureFromCAM(-1);
 if (capture)  
 {   
  frame = cvQueryFrame(capture); 
  
  if (frame)
     frame1 = cvCreateImage( size, IPL_DEPTH_8U, 3 );
   cvLogPolar( frame, frame1,  cvPoint2D32f(frame->width/2, frame->height/2), 40, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS );
   //cvLogPolar( frame, frame1,  cvPoint2D32f(frame->width/4, frame->height/4), 40, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS );
   this->resize(frame->width,frame->height);
   this->resize(frame1->width,frame1->height);
  qImg = new QImage(QSize(frame->width,frame->height), QImage::Format_RGB888);   
  iplImg = cvCreateImageHeader(cvSize(frame->width,frame->height), 8,3);   
  iplImg->imageData = (char*)qImg->bits();
  qImg1 = new QImage(QSize(frame1->width,frame1->height), QImage::Format_RGB888);   
  iplImg1 = cvCreateImageHeader(cvSize(frame1->width,frame1->height), 8,3);   
  iplImg1->imageData = (char*)qImg1->bits(); 
  if(flag)
    {
   timer = new QTimer(this);
    timer->setInterval(30);    //一秒33帧
    connect(timer,SIGNAL(timeout()),this,SLOT(nextFrame()));   
    timer->start();
    }
  } 
}

qtvideo_2::~qtvideo_2()
{
 cvReleaseImage(&iplImg); 
 cvReleaseImage(&iplImg1); 
cvReleaseCapture(&capture);  
delete qImg;
delete qImg1;
delete timer;
delete timer1;
}
void qtvideo_2::nextFrame() 
{  
 frame = cvQueryFrame(capture);
 
 CvSize size = cvSize( (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT) );
 if (frame) 
{   
 frame1 = cvCreateImage( size, IPL_DEPTH_8U, 3 );
 cvLogPolar( frame, frame1,  cvPoint2D32f(frame->width/2, frame->height/2), 40, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS );
 //cvLogPolar( frame, frame1,  cvPoint2D32f(frame->width/4, frame->height/4), 40, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS );
 if (frame->origin == IPL_ORIGIN_TL)   
        { 
      cvCopy(frame,iplImg,0);  
      cvCopy(frame1,iplImg1,0);
        }   
       else {    
    cvFlip(frame,iplImg,0);
     cvFlip(frame1,iplImg1,0);
        }  
    cvCvtColor(iplImg,iplImg,CV_BGR2RGB); 
    cvCvtColor(iplImg1,iplImg1,CV_BGR2RGB);
    this->update();
    g_Pos++;
    if(g_Pos%33 == 0)
    {
    ui.slider->setValue(g_Pos); 
    }
 }
}

void qtvideo_2::paintEvent(QPaintEvent *e) 

 QPainter painter(this);  
painter.drawImage(QPoint(0,0),*qImg); 
painter.drawImage(QPoint(520,0),*qImg1);

//暂停播放功能

void qtvideo_2::playClicked()
{
if(ui.playBtn->text()==tr("pause"))
 {
  ui.playBtn->setText(tr("play"));
  ui.playBtn->setStyleSheet(QLatin1String("background-color: rgba(255, 0, 0, 150);\n"
"color: rgb(0, 0, 255);\n"
"\n"
"font: 75 12pt \"Arial Unicode MS\";"));
  flag=false;
  timer->stop();
 }
 else
 {
  ui.playBtn->setText(tr("pause"));
  ui.playBtn->setStyleSheet(QLatin1String("background-color: rgba(0, 170, 255, 150);\n"
"color: rgb(0, 0, 255);\n"
"\n"
"font: 75 12pt \"Arial Unicode MS\";"));
  flag=true;
  timer->start();
 }
}
void qtvideo_2::zoomClicked()    //视频缩放功能没写,很容易是实现的
{
 
}
void qtvideo_2::exitClicked()
{
 this->close();
}

void qtvideo_2::timeUpdate() 

   timer1=new QTimer(this); 
    //ui.lcdNumber->setNumDigits(19); 
    //ui.lcdNumber->setSegmentStyle(QLCDNumber::Flat); 
  ui.lcdNumber->display(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
   connect(timer1,SIGNAL(timeout()),this,SLOT(showTime())); 
   timer1->start(1000);
   } 
 
void qtvideo_2::showTime() 

     ui.lcdNumber->display(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));

void qtvideo_2::CallBackTrackBarSlide() 

   // 这里便是重新设定视频文件当前播放的帧 
   cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,ui.slider->value ()); 
    g_Pos =ui.slider->value () ; 
 

 

 

main.cpp

#include "qtvideo_2.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
 QApplication a(argc, argv);
 char *fileName="test_apple.flv";
 qtvideo_2 *w=new qtvideo_2(fileName);
 w->show();
 return a.exec();
}

 

运行结果:

 
暂停效果:

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值