QT+Opencv实现简单的视频播放及抓拍

原理很简单,采用opencv库cvQueryFrame函数获取一帧,然后将该帧图像在QLabel显示出来。为实现连续播放,采用定时器。

QT+Opencv实现简单的视频播放及抓拍_第1张图片

#ifndef CAMERA_H
#define CAMERA_H

#include 
#include 
#include 
#include 
#include "ui_camera.h"

//namespace Ui
//{
//	class camera;
//}
class camera : public QWidget
{
	Q_OBJECT

public:
	camera(QWidget *parent = 0);
	~camera();

private Q_SLOTS:
	void OnTimerOut();
	void OnClicked();
	void OnCaptureClicked();
	void OnCaptureClose();

private:
	Ui::cameraClass ui;
	IplImage*	m_pFrame;
	QTimer*		m_pTimer;
	CvCapture*	m_pCapture;
	QImage*		m_pImage;
};

#endif // CAMERA_H
#include "camera.h"

camera::camera(QWidget *parent)
	: QWidget(parent)
	//ui(new Ui::camera)
{
	ui.setupUi(this);
	m_pCapture = NULL;
	m_pImage = new QImage();
	m_pTimer = new QTimer(this);
	connect(m_pTimer, SIGNAL(timeout()), this, SLOT(OnTimerOut()));
	connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(OnClicked()));
	connect(ui.btnCapture, SIGNAL(clicked()), this, SLOT(OnCaptureClicked()));
	connect(ui.btnclose, SIGNAL(clicked()), this, SLOT(OnCaptureClose()));
}

camera::~camera()
{

}

void camera::OnClicked()
{
	m_pCapture = cvCreateFileCapture("D://learn//1.mp4");
	m_pTimer->start(33);

}

void camera::OnTimerOut()
{
	m_pFrame = cvQueryFrame(m_pCapture);
	if (m_pFrame == NULL)
		return;
	QImage image((const uchar*)m_pFrame->imageData, m_pFrame->width, m_pFrame->height, QImage::Format_RGB888);
	ui.label->setPixmap(QPixmap::fromImage(image));
}

void camera::OnCaptureClicked()
{
	m_pFrame = cvQueryFrame(m_pCapture);
	if (m_pFrame == NULL)
		return;
	QImage image((const uchar*)m_pFrame->imageData, m_pFrame->width, m_pFrame->height, QImage::Format_RGB888);
	ui.label_2->setPixmap(QPixmap::fromImage(image));
}

void camera::OnCaptureClose()
{
	m_pTimer->stop();
	cvReleaseCapture(&m_pCapture);
}

QT+Opencv实现简单的视频播放及抓拍_第2张图片

你可能感兴趣的:(opencv,c/c++)