功能界面:
开发环境:
Qt5.8 + VS2013 + opencv3.1.0
Demo 下载:https://download.csdn.net/download/birenxiaofeigg/12089199
头文件包含
#include <qfiledialog.h>
#include <QTimer>
#include <qpixmap.h>
#include <qlabel.h>
#include <qdebug.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
打开视频文件:
QString deftPath = "E:/OfficeVideos_hiv2018",
deftSuffic = "Videos(*.avi *.mp4 *.wmv *.mkv *.rmvb *.mpeg);;Alls(*.*)";
mVideoPath = QFileDialog::getOpenFileName(nullptr, "Open-Video", deftPath, deftSuffic);
if (mVideoPath.isEmpty()) return;
ui.edtVideoPath->setText(mVideoPath);
vc = new VideoCapture;
String cStr(mVideoPath.toStdString());
qDebug() << "Video: " << mVideoPath;
vc->open(cStr);
QString str;
if (vc->isOpened())
{
str = QString::fromLocal8Bit("视频读取成功!");
mLabel->setText(str);
}
else
{
str = QString::fromLocal8Bit("视频读取失败!");
mLabel->setText(str);
return;
}
double Ratio = vc->get(CV_CAP_PROP_FPS),//帧率
wid = vc->get(CV_CAP_PROP_FRAME_WIDTH),//每帧图像宽度
heit = vc->get(CV_CAP_PROP_FRAME_HEIGHT);//每帧图像高度
mFramesCnt = vc->get(cv::CAP_PROP_FRAME_COUNT);//帧数
qDebug() << "Ratio: " << Ratio << "Width: " << wid << "Height: " << heit << "count: " << mFramesCnt;
if (mFramesCnt < 0)
{
ui.hsliderVideo->setEnabled(false);
}
else
{
ui.hsliderVideo->setEnabled(true);
ui.hsliderVideo->setValue(1);
}
开始播放
void myPlayer1::on_btnPlay_clicked()
{
if (!vc) return;
if (mPause)// 暂停
{
if (mTimer->isActive())
{
mTimer->stop();
mPause = false;
}
}
else// 播放
{
if (!mTimer->isActive())
{
mTimer->start();
mPause = true;
}
}
}
快进
// 快进
void myPlayer1::on_btnFast_clicked()
{
if (!vc) return;
if (!mPause) return;
if (ui.btnFast->text() == QString::fromLocal8Bit("快进"))
{
ui.btnFast->setText(QString::fromLocal8Bit("停止快进"));
mRunFast = true;
ui.labelFast->setText("2 X");
}
else
{
ui.btnFast->setText(QString::fromLocal8Bit("快进"));
mRunFast = false;
ui.labelFast->setText("1 X");
}
}
进度条拖动
// 进度条拖动
void myPlayer1::onSliderMoved(int value)
{
if (vc == nullptr) return;
qDebug() << "value: " << value;
double fm = value*mFramesCnt / 100.0;
vc->set(CV_CAP_PROP_POS_FRAMES,fm); //CV_CAP_PROP_POS_FRAMES
}
Demo 下载:https://download.csdn.net/download/birenxiaofeigg/12089199