OpenCV 播放视频的QT 代码

http://mobile.51cto.com/symbian-271265.htm

http://blog.csdn.net/yang_xian521/article/details/7042687

// myWidget.h  
#ifndef MYWIDGET_H  
#define MYWIDGET_H  
#include <QtGui\QWidget> 
#include <QtGui\QPaintEvent> 
#include <QtGui\QImage> 
#include <QtCore\QTimer> 
#include <cv.h> 
#include <highgui.h> 
class myWidget : public QWidget  
{
   Q_OBJECT  
   public:   
   myWidget(const char *filename,QWidget *parent = 0);
   ~myWidget();
   protected:
   void paintEvent(QPaintEvent *e);
   private slots:
   void nextFrame();
   private:
   CvCapture *capture;
   IplImage *iplImg;
   IplImage *frame;
   QImage *qImg;
   QTimer *timer;
};
#endif


 

// myWidget.cpp  
#include "myWidget.h"  
#include <QtGui\QPainter> 
#include <QtCore\QPoint> 
myWidget::myWidget(const char *filename,QWidget *parent /* = 0 */) : QWidget(parent)  
{
   capture = cvCaptureFromFile(filename);
   if (capture)
   {
		frame = cvQueryFrame(capture);
		if (frame)
			this->resize(frame->width,frame->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();
		timer = new QTimer(this);
		timer->setInterval(30);
		connect(timer,SIGNAL(timeout()),this,SLOT(nextFrame()));
		timer->start();
	}
}
myWidget::~myWidget()  
{
   cvReleaseImage(&iplImg);
   cvReleaseCapture(&capture);
   delete qImg;   delete timer;
}  
void myWidget::paintEvent(QPaintEvent *e)  
{
   QPainter painter(this);
   painter.drawImage(QPoint(0,0),*qImg);  
}  
void myWidget::nextFrame()  
{
   frame = cvQueryFrame(capture);
   if (frame)
   {
	if (frame->origin == IPL_ORIGIN_TL)
    {
		cvCopy(frame,iplImg,0);
	}
    else
    {
		cvFlip(frame,iplImg,0);
	}
    cvCvtColor(iplImg,iplImg,CV_BGR2RGB);
    this->update();
	}  
} 

主函数里面调用

int main(int argc,char* argv[])  
{
   QApplication app(argc,argv);
   char *filename = "test.avi";   
	myWidget *mw = new myWidget(filename);   
	mw->show();
	int re = app.exec();
	return re;  
} 



 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值