VS2008编写MFC程序--使用opencv2.4()

开始记录VS2008环境下学习OPENCV2.4

头文件:

1 #pragma once
2 #include "CvvImage.h"
3 #include "opencv/cv.h"
4 #include "opencv/highgui.h"

定义全局变量:

在*dlg.cpp

1 CvCapture* capture;
2 CRect rect;
3 CDC *pDC;
4 HDC hDC;
5 CWnd *pwnd;

 

CPP文件:

 1 void Cvedio_mfcDlg::OnBnClickedButton1()
 2 {
 3     // TODO: Add your control notification handler code here
 4     if(!capture)
 5     {
 6         capture = cvCaptureFromCAM(0);
 7         //AfxMessageBox("OK");
 8     }
 9 
10     if (!capture)
11     {
12         AfxMessageBox("无法打开摄像头");
13         return;
14     }
15 
16     // 测试
17     IplImage* m_Frame;
18     m_Frame=cvQueryFrame(capture);
19     CvvImage m_CvvImage;
20     m_CvvImage.CopyOf(m_Frame,1);    
21     if (true)
22     {
23         m_CvvImage.DrawToHDC(hDC, &rect);
24         //cvWaitKey(10);
25     }
26 
27     // 设置计时器,每10ms触发一次事件
28     SetTimer(1,10,NULL);
29 }
30 
31 void Cvedio_mfcDlg::OnTimer(UINT_PTR nIDEvent)
32 {
33     // TODO: Add your message handler code here and/or call default
34     /************************************************************************/
35     /* 显示摄像头                                                           */
36     /************************************************************************/
37     IplImage* m_Frame;
38     m_Frame=cvQueryFrame(capture);
39     CvvImage m_CvvImage;
40     m_CvvImage.CopyOf(m_Frame,1);    
41     if (true)
42     {
43         m_CvvImage.DrawToHDC(hDC, &rect);
44         //cvWaitKey(10);
45     }
46 
47 
48     CDialog::OnTimer(nIDEvent);
49 }
50 
51 void Cvedio_mfcDlg::OnBnClickedButton2()
52 {
53     // TODO: Add your control notification handler code here
54     cvReleaseCapture(&capture);
55     CDC MemDC; 
56     CBitmap m_Bitmap1;
57     m_Bitmap1.LoadBitmap(IDB_BITMAP1); 
58     MemDC.CreateCompatibleDC(NULL);
59     MemDC.SelectObject(&m_Bitmap1);
60     pDC->StretchBlt(rect.left,rect.top,rect.Width(),rect.Height(),&MemDC,0,0,48,48,SRCCOPY); 
61 }

 

 

托管在github上的源代码:

https://github.com/yuliyang-bupt/simple_camera_mfc

效果:

 

posted @ 2013-09-16 17:38  小菜鸟_yang  阅读(1128)  评论(0编辑  收藏  举报