http://mobile.51cto.com/symbian-271265.htm
http://blog.youkuaiyun.com/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;
}