Qt + VS2013利用Opencv实现视频播放器功能

功能实现:

1.弹窗,手动选择打开视频文件;
2.视频播放,
3.视频播放暂停,继续播放
4.快进;
5.截图
demo下载:https://download.csdn.net/download/birenxiaofeigg/12089199

头文件包含:

#include <QLabel>
#include <qtimer.h>
#include <QFileDialog>
#include <QLayout>

UI显示

打开视频文件后:
在这里插入图片描述

代码实现:

打开视频文件
// 打开视频文件
void tVideoCapture::on_btnOpen_clicked()
{
	QString deftPath = "E:/OfficeVideos_hiv2018",
		deftSuffic = "Videos(*.avi *.mp4 *.wmv);;Films(*.mkv *.rmvb *.mpeg);;Alls(*.*)";
	QString VideoName = QFileDialog::getOpenFileName(nullptr, "Open-Video", deftPath, deftSuffic);
	if (VideoName.isEmpty())  return;

	mLabel1->setText(VideoName);


	vc = new VideoCapture;
	String cStr(VideoName.toStdString());
	vc->open(cStr);

	QString str;
	if (vc->isOpened())
	{
		str = QString::fromLocal8Bit("视频读取成功!");
	}
	else
	{
		str = QString::fromLocal8Bit("视频读取失败!");
		qDebug() << str;
		return;
	}
	qDebug() << str;
	double Ratio = vc->get(CV_CAP_PROP_FPS),//帧率
		wid = vc->get(CV_CAP_PROP_FRAME_WIDTH),//每帧图像宽度
		heit = vc->get(CV_CAP_PROP_FRAME_HEIGHT);//每帧图像高度
	int cnt = 0;
	cnt = vc->get(cv::CAP_PROP_FRAME_COUNT);//帧数	
	qDebug() << "Ratio: " << Ratio << "Width: " << wid << "Height: " << heit << "count: " << cnt;
	Mat fm;
	if (vc->read(fm))
	{
		mInter->sendVideoName(fm);
	}
}

播放

// 播放
void tVideoCapture::on_btnRun_clicked()
{
	if (ui.btnRun->text() == QString::fromLocal8Bit("播放"))
	{
		ui.btnRun->setText(QString::fromLocal8Bit("暂停"));		
		mLabel2->setText(QString::fromLocal8Bit("正常播放!"));
		
		mTimer->start();
	}
	else
	{
		ui.btnRun->setText(QString::fromLocal8Bit("播放"));

		mTimer->stop();
		mRunFast = false;
		mLabel2->setText(QString::fromLocal8Bit("暂停播放!"));
	}
}

快进

// 快进
void tVideoCapture::on_btnFast_clicked()
{
	if (ui.btnFast->text() == QString::fromLocal8Bit("快进"))
	{
		ui.btnFast->setText(QString::fromLocal8Bit("停止快进"));
		mRunFast = true;
		mLabel2->setText(QString::fromLocal8Bit("快进播放!"));
	}
	else
	{
		ui.btnFast->setText(QString::fromLocal8Bit("快进"));
		mRunFast = false;
		mLabel2->setText(QString::fromLocal8Bit("正常播放!"));
	}
	
}

截图

// 截图
void tVideoCapture::on_btnGrab_clicked()
{
	mTimer->stop();
	mRunFast = false;
	mLabel2->setText(QString::fromLocal8Bit("暂停播放!"));
	ui.btnRun->setText(QString::fromLocal8Bit("播放"));

	QString deftPath = "",
		deftSuffic = "Imgs(*.jpg *.bmp *.png *.tiff);;Alls(*.*)";

	QString SavePth = QFileDialog::getSaveFileName(nullptr, "Save-Images", deftPath, deftSuffic);
	if (SavePth.isEmpty()) return;
	Mat fm;
	if (vc->read(fm))
	{
		imwrite(SavePth.toStdString(), fm);
	}
}

代码截图

在这里插入图片描述

demo源代码下载

https://download.csdn.net/download/birenxiaofeigg/12089199

发布了56 篇原创文章 · 获赞 10 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/birenxiaofeigg/article/details/103888610