记:OpenCV+MFC视频读取显示及进度界面显示

1.在工程.h文件添加所需的变量

        CString FileName;
		
		VideoCapture cap;
		Mat frame;
		afx_msg void OnBnClickedButtoShow();
		afx_msg void OnTimer(UINT_PTR nIDEvent);

2.在工程::OnInitDialog()函数中初始化窗口,获取控件设备: 

        namedWindow("view222",WINDOW_AUTOSIZE);
		HWND hWnd1 = (HWND)cvGetWindowHandle("view222");
		HWND hParent1 = ::GetParent(hWnd1);
		::SetParent(hWnd1,GetDlgItem(IDC_STATIC_two)->m_hWnd);
		::ShowWindow(hParent1,SW_HIDE);

3.添加一个图片控件两个按钮,一个为打开文件,另一个为视频播放,并对按钮进行双击添加控制代码。

打开文件代码

CFileDialog dlg(TRUE);
		if(dlg.DoModal()==IDOK)	
		{
			FileName=dlg.GetPathName();
			//FileName="D:\\QQRecv\\MobileFile\\jj.mp4";
		}
		GetDlgItem(IDC_EDIT1)->SetWindowTextA((LPCTSTR)FileName);

视频播放代码

cap =VideoCapture((LPCTSTR)FileName);
			//判断视频是否打开
			if (cap.isOpened()) {
				//总帧数
				double frameCount = cap.get(CV_CAP_PROP_FRAME_COUNT);
				//帧率
				double fps = cap.get(CV_CAP_PROP_FPS);
				//时间长度
				double len = frameCount / fps;

				/*CString str;
				str.Format(_T("总帧数:%0.1f ,帧率:%0.1f ,总时长:%0.1f s"),frameCount,fps,len);
				GetDlgItem(IDC_EDIT1)->SetWindowTextA((LPCTSTR)str);*/

				//double rate = capture.get(CV_CAP_PROP_FPS);//获取视频文件的帧率
				int delay = cvRound(1000.000 / fps);
				while (true)
				{
					Mat frame;
					cap >> frame;//读出每一帧的图像
					if (frame.empty()) break;
					//imshow("处理前视频", frame);
					Mat iiii;
					CRect rect1;  
					GetDlgItem(IDC_STATIC_two)->GetClientRect(&rect1);  
					Rect dst1(rect1.left,rect1.top,rect1.right,rect1.bottom);  
					resize(frame,iiii,cv::Size(rect1.Width(),rect1.Height()));   
					imshow("view222",iiii);
					waitKey(delay);
					int frame_num = cap.get(CV_CAP_PROP_POS_FRAMES);
					//ShowVectory(frame_num);

					CString str;
					str.Format(_T("总帧数:%0.1f,帧率:%0.1f,总时长:%0.1f s,当前帧数:%d"),frameCount,fps,len,frame_num);
					GetDlgItem(IDC_EDIT1)->SetWindowTextA((LPCTSTR)str);
				}
			}
			else
			{
				AfxMessageBox("读取视频错误!");
			}

			
			cap.release();
			AfxMessageBox("视频读取显示完毕");

 

你可能感兴趣的:(记:OpenCV+MFC视频读取显示及进度界面显示)