直接给代码:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int g_delay_ms=33;//
int g_CurrentFrame;//当前图片的帧数
int g_TotalFrameNunber;//整个视频总帧数
int g_CurrentPercent;//当前播放的进度,用帧数除以总帧数
char g_nTrackbarName[50]; //用于存储显示内容
bool f_Capture_Update = false; //根据进度切换标志位,滑动条更新后的状态位
int g_nsetframe; //移动滑动条以后,滑动条位置对应的视频的第几帧,就是设置当前帧
void on_Trackbar(int, void*)//回调函数
{
f_Capture_Update = true;
}
void ShowHelpText()//
{
printf("\n\n\n...........");
printf("\n\n\t\t当前使用OPENCV版本为:"CV_VERSION);
printf("\n\n\n...........");
}
//-----------------------------------【ImageText( )函数】----------------------------------
// 描述:把文字加在图像上
//----------------------------------------------------------------------------------------------
void ImageText(Mat* img, const char* text, int x, int y)
{
Point pt(x, y);
Scalar color = CV_RGB(255, 255, 255);
//purText()
putText(*img, text, pt, CV_FONT_HERSHEY_SIMPLEX, 1, color, 1, 20);
}
int main()
{
ShowHelpText();
VideoCapture capture("1.AVI");
if (capture.isOpened() == false)
{
cout << "fail to show Video!" << endl;
return 0;
}
g_TotalFrameNunber =static_cast<int> (capture.get(CV_CAP_PROP_FRAME_COUNT));//获取图片的总图像帧数
cout << "视频总帧数:" << g_TotalFrameNunber << endl;
//计算播放进度
int g_CurrentPercent = g_CurrentFrame / g_TotalFrameNunber * 100;
namedWindow("播放窗口");
//创建一个进度条(下面两句话)
sprintf(g_nTrackbarName, "最大%d",100);//将g_nTrackbarName赋值“已播放”,
createTrackbar(g_nTrackbarName, "播放窗口", &g_CurrentPercent, 100, on_Trackbar);
while (capture.isOpened()==true )
{
Mat frame;
capture >> frame;
if (f_Capture_Update == true)
{
g_nsetframe = g_CurrentPercent * g_TotalFrameNunber / 100;
capture.set(CV_CAP_PROP_POS_FRAMES, g_nsetframe);//取出滑动条对应位置对应的第几帧图片
g_CurrentFrame = g_nsetframe;
f_Capture_Update = false;//表示滑动条没有动了
}
g_CurrentFrame++;
//播放一帧图片,进度就会更新
g_CurrentPercent = int(g_CurrentFrame * 100 / g_TotalFrameNunber);
//然后滑动条自动往后
setTrackbarPos(g_nTrackbarName, "播放窗口", g_CurrentPercent);
//播放到头循环播放
if (g_CurrentPercent == 100)
{
g_CurrentPercent = 0;
g_CurrentFrame = 0;
capture.set(CV_CAP_PROP_POS_FRAMES, g_CurrentFrame);
}
//在图片上显示当前帧数
char text[5];
sprintf(text, "%d", g_CurrentFrame);
ImageText(&frame, text, 5, 25);
imshow("播放窗口", frame);
waitKey(30);
}
return 0;
}
本代码有参考。完成了进度条控制视频播放的基本功能。具体参考为http://blog.csdn.net/u012348655/article/details/50524441