在MFC中通过opencv显示摄像头视频或者文件视频

来源:互联网 发布:三维软件开发 编辑:程序博客网 时间:2024/05/29 02:38
 

在MFC中通过opencv显示摄像头视频或者文件视频

这里通过MFC显示摄像头视频,同样要用到CvvImage类,本人用的opencv2.3.1的版本,这里没有这个类,所以仍然需要手动加入这个类的头文件和代码文件。

关于CvvImage类的说明请看:

http://blog.csdn.net/weixingstudio/article/details/7357651

http://blog.csdn.net/weixingstudio/article/details/7357558

 

然后就是搭建工程了。这里就不多说界面的设计了。我这里的界面如图所示,有一个开始打开摄像头的按钮,一个关闭摄像头的按钮。有一个PictureBox的控件。

 

为了能够在PictureBox里面显示图片,我们需要定义一些变量来获取PictureBox的句柄。在主窗口的cpp文件中,添加如下的全局变量:

[cpp] view plain copy
  1. CvCapture* capture;  
  2. CRect rect;  
  3. CDC *pDC;  
  4. HDC hDC;  
  5. CWnd *pwnd;  


 

这里特别注意,这些变量一定要是全局变量。再来看一下这些变量的添加位置:

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include "VideoMFC.h"  
  3. #include "VideoMFCDlg.h"  
  4. #include "afxdialogex.h"  
  5.   
  6. #ifdef _DEBUG  
  7. #define new DEBUG_NEW  
  8. #endif  
  9.   
  10.   
  11. CvCapture* capture;  
  12. CRect rect;  
  13. CDC *pDC;  
  14. HDC hDC;  
  15. CWnd *pwnd;  
  16.   
  17. // CAboutDlg dialog used for App About  
  18.   
  19. class CAboutDlg : public CDialogEx  
  20. {  
  21. public:  


 

然后在窗口的初始化函数中进行句柄的初始化:

[cpp] view plain copy
  1. OnInitDialog()  


 

这个函数,BOOL CVideoMFCDlg::OnInitDialog()

初始化代码:

[cpp] view plain copy
  1. // CVideoMFCDlg message handlers  
  2.   
  3. BOOL CVideoMFCDlg::OnInitDialog()  
  4. {  
  5.     CDialogEx::OnInitDialog();  
  6.   
  7.     // Add "About..." menu item to system menu.  
  8.   
  9.     // IDM_ABOUTBOX must be in the system command range.  
  10.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);  
  11.     ASSERT(IDM_ABOUTBOX < 0xF000);  
  12.   
  13.     CMenu* pSysMenu = GetSystemMenu(FALSE);  
  14.     if (pSysMenu != NULL)  
  15.     {  
  16.         BOOL bNameValid;  
  17.         CString strAboutMenu;  
  18.         bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);  
  19.         ASSERT(bNameValid);  
  20.         if (!strAboutMenu.IsEmpty())  
  21.         {  
  22.             pSysMenu->AppendMenu(MF_SEPARATOR);  
  23.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);  
  24.         }  
  25.     }  
  26.   
  27.       
  28.     // Set the icon for this dialog.  The framework does this automatically  
  29.     //  when the application's main window is not a dialog  
  30.     SetIcon(m_hIcon, TRUE);         // Set big icon  
  31.     SetIcon(m_hIcon, FALSE);        // Set small icon  
  32.   
  33.     // TODO: Add extra initialization here  
  34.     pwnd = GetDlgItem(IDC_ShowImage);  
  35.     //pwnd->MoveWindow(35,30,352,288);  
  36.         pDC =pwnd->GetDC();  
  37.     //pDC =GetDC();  
  38.         hDC= pDC->GetSafeHdc();  
  39.     pwnd->GetClientRect(&rect);  
  40.   
  41.   
  42.   
  43.     return TRUE;  // return TRUE  unless you set the focus to a control  
  44. }  


这里的初始化代码只有Todo后面的是自己添加的,目的是获得图像控件的句柄,将来好在上面显示图像。这一步也可以放在具体的显示图像的时候在进行,但是就需要每显示一帧,都获得一次句柄。

在控制台程序中,我们可以很简单的通过for(;;)的空循环来不停的实现获取摄像头的每一帧,但是我发现这么做在MFC里面是不可行的。一个是因为MFC是用户界面程序,如果这么写的话,所有的界面都会卡住,而且这么写的话其他的功能按钮就失去作用了。

这里为了实现获取摄像头的每一帧,我们要通过设定一个时间事件,让每隔一定时间,比如20ms,就调用一个函数,通过这个时间调用来获取摄像头的帧。

这样,我们就可以实现在图像控件中显示视频,并且用户界面不会卡住了。

看一下打开摄像头按钮的代码:

[cpp] view plain copy
  1. void CVideoMFCDlg::OnBnClickedButton1()  
  2. {  
  3.     // TODO: Add your control notification handler code here  
  4.     //AfxMessageBox("OK");  
  5.     if(!capture)  
  6.     {  
  7.         capture = cvCaptureFromCAM(0);  
  8.         //AfxMessageBox("OK");  
  9.     }  
  10.   
  11.     if (!capture)  
  12.     {  
  13.         AfxMessageBox("无法打开摄像头");  
  14.         return;  
  15.     }  
  16.   
  17.     // 测试  
  18.     IplImage* m_Frame;  
  19.     m_Frame=cvQueryFrame(capture);  
  20.     CvvImage m_CvvImage;  
  21.     m_CvvImage.CopyOf(m_Frame,1);     
  22.     if (true)  
  23.     {  
  24.         m_CvvImage.DrawToHDC(hDC, &rect);  
  25.         //cvWaitKey(10);  
  26.     }  
  27.   
  28.     // 设置计时器,每10ms触发一次事件  
  29.     SetTimer(1,10,NULL);  
  30. }  


这里有一个SetTimer();函数,这个函数就是调用win32函数实现每隔指定的时间调用一次我们指定的事件。这个函数有两种用法,一种是指定一个回调函数,一个是通过MFC的ClassWizard指定的回调函数。

SetTimer()的具体用法请见:http://baike.baidu.com/view/998104.htm

这里我们使用了通过MFC的ClassWizard设定回调函数。

在VS中,用户界面设计右击打开Class Wizard, 切换到Message选项卡,然后找到WM_TIMER这个message, 双击右边的OnTimer句柄,然后进入回调函数的代码。

回调函数的代码如下:

[cpp] view plain copy
  1. void CVideoMFCDlg::OnTimer(UINT_PTR nIDEvent)  
  2. {  
  3.     // TODO: Add your message handler code here and/or call default  
  4.     /************************************************************************/  
  5.     /* 显示摄像头                                                           */  
  6.     /************************************************************************/  
  7.     IplImage* m_Frame;  
  8.     m_Frame=cvQueryFrame(capture);  
  9.     CvvImage m_CvvImage;  
  10.     m_CvvImage.CopyOf(m_Frame,1);     
  11.     if (true)  
  12.     {  
  13.         m_CvvImage.DrawToHDC(hDC, &rect);  
  14.         //cvWaitKey(10);  
  15.     }  
  16.   
  17.     CDialogEx::OnTimer(nIDEvent);  
  18. }  


 

关闭摄像头代码:

[cpp] view plain copy
  1. void CVideoMFCDlg::OnBnClickedButton2()  
  2. {  
  3.     // TODO: Add your control notification handler code here  
  4.     cvReleaseCapture(&capture);  
  5.     CDC MemDC;   
  6.     CBitmap m_Bitmap1;  
  7.     m_Bitmap1.LoadBitmap(IDB_BITMAP1);   
  8.     MemDC.CreateCompatibleDC(NULL);  
  9.     MemDC.SelectObject(&m_Bitmap1);  
  10.     pDC->StretchBlt(rect.left,rect.top,rect.Width(),rect.Height(),&MemDC,0,0,48,48,SRCCOPY);   
  11. }  


 

系统运行截图:

 

 

如果需要保存摄像头视屏的操作,请看链接中的文章:http://blog.csdn.net/weixingstudio/article/details/7588505

 

整个工程的下载地址:http://download.csdn.net/detail/weixingstudio/4284066。在这个工程中保存视频的功能。

阅读全文
0 0
原创粉丝点击