功能实现:
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);
}
}