在网上找到了一段视频显示的代码,如下:
class PictureDisplay : public QWidget // picture display widget
{
public:
PictureDisplay(QWidget* parent, const char* name=0, int f=0);
~PictureDisplay();
protected:
void paintEvent( QPaintEvent * );
public :
QPixmap pixmap;
private:
};
PictureDisplay::PictureDisplay(QWidget* parent, const char* name, int f):QWidget(parent, name, f)
{
;
}
PictureDisplay::~PictureDisplay()
{
;
}
void PictureDisplay::paintEvent( QPaintEvent * )
{
QPainter paint( this ); // paint widget
m.lock();
paint.drawPixmap( 0, 0, pixmap );
m.unlock();
}
void foo(IplImage *ipl_img ,QImage *q)
{
int x;
int y;
char *data = ipl_img->imageData;
for( y = 0; y < ipl_img->height; y++, data += ipl_img->widthStep )
for( x = 0; x < ipl_img->width; x++ )
{
uint *p = (uint*)q->scanLine(y) + x;
*p = qRgb(data[x * ipl_img->nChannels+2],data[x * ipl_img->nChannels +1],data[x * ipl_img->nChannels]);
}
}
class Capturer : public QThread
{
public:
Capturer(int camnr);
~Capturer();
virtual void run();
PictureDisplay *p;
IplImage *i;
CCamera c;
QImage *q;
};
Capturer :: Capturer(int camnr)
{
c.connect(camnr);
CvSize sz = cvSize(320,240);
c.setSize(sz);
i = cvCreateImage(sz,8,3);
q = new QImage();
q->create(320,240,32);
}
Capturer :: ~Capturer()
{
c.disconnect();
delete(q);
}
void Capturer ::run()
{
while(true)
{
c.capture(i);
foo(i,q);
m.lock();
p->pixmap.convertFromImage(*q);
m.unlock();
p->repaint();
}
}
int main( int argc, char **argv )
{
QApplication a( argc, argv ); // QApplication required!
cvvInitSystem(0,0);
QSplitter *s1 = new QSplitter( QSplitter::Vertical, 0 , "main" );
PictureDisplay *test = new PictureDisplay(s1); // create picture display
a.setMainWidget(s1); // set main widget
s1->show(); // show it
Capturer t1(0);
t1.p = test;
t1.start();
return a.exec(); // start event loop
}
我写的程序大概思想和它的一样,现在出现了一个问题
视频图像的闪烁 ,不知如果解决。
[ 此贴被XChinux在2006-09-11 20:50重新编辑 ]