1.0.2-学习Opencv与MFC混合编程之---为播放AVI视频添加滑动条

源代码地址:http://download.csdn.net/detail/nuptboyzhb/3961642

版本1.0.2新增内容

Ø  全局变量和函数的添加:

在CVMFCview.cpp文件中增加全局变量和全局函数

//-----------------全局变量和函数---------------------------------------

int g_slider_position=0;

CvCapture *g_capture;

void onTrackbarSlide(int pos)

{

   cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos);

}

//--效果如下:

注意:上述全局函数,不能作为类得成员变量的形式添加

Ø  改写void CCVMFCView::OnPlAYAVITEST()函数

具体代码如下:

void CCVMFCView::OnPlAYAVITEST()

{

         // TODO: Add your command handler code here

         //郑海波 2011年12月9日

         //参考:learning opencv  Page21

         CString csFilter="AVI Files(*.AVI)|*.AVI||";

         CString strFileName;

 

         CFileDialog FileDlg(true,NULL,NULL,OFN_HIDEREADONLY,csFilter);//  文件存盘对话框

         if (FileDlg.DoModal()==IDOK )

         {         //  选择了文件名

                   strFileName = FileDlg.m_ofn.lpstrFile;

                   if (FileDlg.m_ofn.nFileExtension == 0)

                   {  //  无文件后缀

                            strFileName = strFileName + ".avi";

                            //  加文件后缀

                   }

                   CString WindowName1="Display by CV";//创建窗口的名称

                   cvNamedWindow(WindowName1,CV_WINDOW_AUTOSIZE);//创建窗口

                  //CvCapture *capture =cvCreateFileCapture(strFileName);//由待播放的视频名,创建CvCapture指针

                   //----------------------------------------------------------------

                   g_capture =cvCreateFileCapture(strFileName);//由待播放的视频名,创建CvCapture指针

                   int frames=(int)cvGetCaptureProperty(g_capture,CV_CAP_PROP_FRAME_COUNT);//得到视频中帧的总数

                   if(frames!=0)

                   {

                            cvCreateTrackbar("position",WindowName1,&g_slider_position,frames,onTrackbarSlide);

                   }

                   //----------------------------------------------------------------

                   IplImage *frame;//创建图像指针 用作显示的图片

                   while(1)

                   {

                            frame=cvQueryFrame(g_capture);//从g_capture 获取一帧图像

                            if(!frame)

                            {

                                     break;

                            }

                            cvShowImage(WindowName1,frame);

                            char c=cvWaitKey(33);//等待33毫秒,如果无按键,返回值c=-1。如果有按键,返回按键的ASCII值

                            g_slider_position++;//

            cvCreateTrackbar("position",WindowName1,&g_slider_position,frames,onTrackbarSlide);

            if(c==27)//27是Esc按键的ASCII值

                            {

                                     break;//按下ESc键,停止播放

                            }

                   }

                   cvReleaseCapture(&g_capture);

                   cvDestroyWindow(WindowName1);

         }

}

 

from:http://blog.csdn.net/nupt123456789/article/details/7097896

你可能感兴趣的:(opencv)