MainFrm.cpp
上传用户:czshopping
上传日期:2022-05-22
资源大小:5430k
文件大小:7k
源码类别:

视频捕捉/采集

开发平台:

Visual C++

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3. #include "stdafx.h"
  4. #include "VideoCapture.h"
  5. #include "MainFrm.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CMainFrame
  13. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  14. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  15. //{{AFX_MSG_MAP(CMainFrame)
  16. ON_WM_CREATE()
  17. ON_WM_CLOSE()
  18. ON_WM_ACTIVATE()
  19. ON_COMMAND(ID_APP_EXIT, OnAppExit)
  20. ON_COMMAND(ID_FORMAT, OnFormat)
  21. ON_COMMAND(ID_PREVIEW, OnPreview)
  22. ON_COMMAND(ID_SOURCE, OnSource)
  23. ON_COMMAND(ID_CAPTURE, OnCapture)
  24. //}}AFX_MSG_MAP
  25. END_MESSAGE_MAP()
  26. static UINT indicators[] =
  27. {
  28. ID_SEPARATOR,           // status line indicator
  29. ID_INDICATOR_CAPS,
  30. ID_INDICATOR_NUM,
  31. ID_INDICATOR_SCRL,
  32. };
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CMainFrame construction/destruction
  35. LRESULT CALLBACK EXPORT ErrorCallbackProc(HWND hWnd,int nErrID,LPSTR lpErrorText)
  36. {
  37. if(nErrID==0)
  38. return true;
  39. AfxMessageBox(lpErrorText,MB_OK|NULL);
  40. return true;
  41. }
  42. LRESULT FAR PASCAL StatusCallbackProc(HWND hWnd,int nID,LPSTR lpStatusText)
  43. {
  44. static int CurrentID;
  45. if(nID==IDS_CAP_END)
  46. {
  47. if((CurrentID==IDS_CAP_STAT_VIDEOAUDIO)||(CurrentID==IDS_CAP_STAT_VIDEOONLY))
  48. return true;
  49. }
  50. CurrentID=nID;
  51. return (LRESULT)true;
  52. }
  53. LRESULT FAR PASCAL VideoCallbackProc(HWND hWnd,LPVIDEOHDR lpVHdr)
  54. {
  55. ((CMainFrame *)AfxGetMainWnd())->m_dibinfo.bitmapheader.biSizeImage =lpVHdr->dwBytesUsed ;
  56. memcpy(((CMainFrame *)AfxGetMainWnd())->m_dibinfo.buffer +((CMainFrame *)AfxGetMainWnd())->m_dibinfo.VideoFormatSize,lpVHdr->lpData,lpVHdr->dwBytesUsed);
  57. (((CMainFrame *)AfxGetMainWnd())->GetActiveView())->InvalidateRect(NULL,FALSE);
  58. return (LRESULT)true;
  59. }
  60. void CMainFrame::ChangeSize()
  61. {
  62. static UINT  uiVideoX=0;
  63. static UINT  uiVideoY=0;
  64. CAPSTATUS cs;
  65. RECT  rc;
  66. m_wndSource.GetWindowRect(&rc);
  67. capGetStatus(m_WndCap,&cs,sizeof(cs));
  68. if(uiVideoX!=cs.uiImageWidth||uiVideoY!=cs.uiImageHeight)
  69. {
  70. m_wndSource.MoveWindow(rc.left,rc.top,cs.uiImageWidth+4,cs.uiImageHeight+28);
  71. uiVideoX=cs.uiImageWidth;
  72. uiVideoY=cs.uiImageHeight;
  73. }
  74. m_dibinfo.VideoFormatSize=capGetVideoFormatSize(m_WndCap);
  75. capGetVideoFormat(m_WndCap,&m_dibinfo.bitmapheader,m_dibinfo.VideoFormatSize);
  76. }
  77. CMainFrame::CMainFrame()
  78. {
  79. // TODO: add member initialization code here
  80. }
  81. CMainFrame::~CMainFrame()
  82. {
  83. }
  84. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  85. {
  86. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  87. return -1;
  88. if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
  89. | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
  90. !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  91. {
  92. TRACE0("Failed to create toolbarn");
  93. return -1;      // fail to create
  94. }
  95. if (!m_wndStatusBar.Create(this) ||
  96. !m_wndStatusBar.SetIndicators(indicators,
  97.   sizeof(indicators)/sizeof(UINT)))
  98. {
  99. TRACE0("Failed to create status barn");
  100. return -1;      // fail to create
  101. }
  102. // TODO: Delete these three lines if you don't want the toolbar to
  103. //  be dockable
  104. m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  105. EnableDocking(CBRS_ALIGN_ANY);
  106. DockControlBar(&m_wndToolBar);
  107.     // 创建捕获窗口
  108. if(!m_wndSource.CreateEx(WS_EX_TOPMOST,NULL,"source",WS_CAPTION,CRect(100,100,150,180),NULL,0))
  109.          return -1;
  110. m_wndSource.ShowWindow(SW_HIDE);
  111. m_WndCap=capCreateCaptureWindow((LPSTR)"Video Capture Window",WS_CHILD|WS_VISIBLE,0,0,300,240,m_wndSource.m_hWnd ,0);
  112.    
  113. capSetCallbackOnError(m_WndCap,(FARPROC)ErrorCallbackProc);
  114. capSetCallbackOnStatus(m_WndCap,(FARPROC)StatusCallbackProc);
  115. capSetCallbackOnVideoStream(m_WndCap,(FARPROC)VideoCallbackProc);
  116. capDriverConnect(m_WndCap,0);
  117. capDriverGetCaps(m_WndCap,&m_caps,sizeof(CAPDRIVERCAPS));
  118. ChangeSize();
  119. return 0;
  120. }
  121. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  122. {
  123. if( !CFrameWnd::PreCreateWindow(cs) )
  124. return FALSE;
  125. // TODO: Modify the Window class or styles here by modifying
  126. //  the CREATESTRUCT cs
  127. return TRUE;
  128. }
  129. /////////////////////////////////////////////////////////////////////////////
  130. // CMainFrame diagnostics
  131. #ifdef _DEBUG
  132. void CMainFrame::AssertValid() const
  133. {
  134. CFrameWnd::AssertValid();
  135. }
  136. void CMainFrame::Dump(CDumpContext& dc) const
  137. {
  138. CFrameWnd::Dump(dc);
  139. }
  140. #endif //_DEBUG
  141. /////////////////////////////////////////////////////////////////////////////
  142. // CMainFrame message handlers
  143. void CMainFrame::OnClose() 
  144. {
  145. // TODO: Add your message handler code here and/or call default
  146. OnAppExit();
  147. CFrameWnd::OnClose();
  148. }
  149. void CMainFrame::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) 
  150. {
  151. CFrameWnd::OnActivate(nState, pWndOther, bMinimized);
  152. // TODO: Add your message handler code here
  153. }
  154. void CMainFrame::OnAppExit() 
  155. {
  156. // TODO: Add your command handler code here
  157. capCaptureAbort(m_WndCap);
  158. //callback 是宏,如果出了问题就反馈
  159. capSetCallbackOnError(m_WndCap,NULL);
  160. capSetCallbackOnStatus(m_WndCap,NULL);
  161. capSetCallbackOnVideoStream(m_WndCap,NULL);
  162. capCaptureAbort(m_WndCap);
  163. capDriverDisconnect(m_WndCap);
  164. }
  165. void CMainFrame::OnFormat() 
  166. {
  167. // TODO: Add your command handler code here
  168.    if(m_caps.fHasDlgVideoFormat)
  169.    {
  170.    capDlgVideoFormat(m_WndCap);
  171.    ChangeSize();
  172.    }
  173. }
  174. void CMainFrame::OnPreview() 
  175. {
  176. // TODO: Add your command handler code here
  177. static bool bPreview=FALSE;
  178. bPreview=!bPreview;
  179. capPreviewRate(m_WndCap,1000/10);
  180.     capPreview(m_WndCap,bPreview);
  181. }
  182. void CMainFrame::OnSource() 
  183. {
  184. // TODO: Add your command handler code here
  185. static BOOL bShow=false;
  186. bShow=!bShow;
  187. m_wndSource.ShowWindow(bShow?SW_SHOW:SW_HIDE);
  188. }
  189. void CMainFrame::OnCapture() 
  190. {
  191. // TODO: Add your command handler code here
  192. static BOOL bCapture=false;
  193. CAPTUREPARMS CapParms;
  194. bCapture=!bCapture;
  195. if(!bCapture)
  196. {
  197. capCaptureGetSetup(m_WndCap,&CapParms,sizeof(CAPTUREPARMS));
  198. CapParms.dwIndexSize=324000;
  199. CapParms.fMakeUserHitOKToCapture=!CapParms.fMCIControl;
  200. CapParms.wPercentDropForError=100;
  201. CapParms.wNumVideoRequested=5;
  202. CapParms.wChunkGranularity=0;
  203. CapParms.fYield=true;
  204. CapParms.fCaptureAudio=false;
  205. CapParms.vKeyAbort=0;
  206. CapParms.fAbortLeftMouse=CapParms.fAbortRightMouse=false;
  207. CapParms.dwRequestMicroSecPerFrame=1000000/10;
  208. capSetCallbackOnYield(m_WndCap,NULL);
  209. capCaptureSetSetup(m_WndCap,&CapParms,sizeof(CAPTUREPARMS));
  210. capCaptureSequenceNoFile(m_WndCap);
  211. }
  212. else
  213. capCaptureAbort(m_WndCap);
  214. }