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

视频捕捉/采集

开发平台:

Visual C++

  1. // VideoMoveDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "VideoMove.h"
  5. #include "VideoMoveDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. #define TIMER_DELAY 50
  12. #define TIMER_ID 1
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CAboutDlg dialog used for App About
  15. BOOL iMark=FALSE;
  16. int iPicNum;
  17. class CAboutDlg : public CDialog
  18. {
  19. public:
  20. CAboutDlg();
  21. // Dialog Data
  22. //{{AFX_DATA(CAboutDlg)
  23. enum { IDD = IDD_ABOUTBOX };
  24. //}}AFX_DATA
  25. // ClassWizard generated virtual function overrides
  26. //{{AFX_VIRTUAL(CAboutDlg)
  27. protected:
  28. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  29. //}}AFX_VIRTUAL
  30. // Implementation
  31. protected:
  32. //{{AFX_MSG(CAboutDlg)
  33. //}}AFX_MSG
  34. DECLARE_MESSAGE_MAP()
  35. };
  36. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  37. {
  38. //{{AFX_DATA_INIT(CAboutDlg)
  39. //}}AFX_DATA_INIT
  40. }
  41. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  42. {
  43. CDialog::DoDataExchange(pDX);
  44. //{{AFX_DATA_MAP(CAboutDlg)
  45. //}}AFX_DATA_MAP
  46. }
  47. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  48. //{{AFX_MSG_MAP(CAboutDlg)
  49. // No message handlers
  50. //}}AFX_MSG_MAP
  51. END_MESSAGE_MAP()
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CVideoMoveDlg dialog
  54. CVideoMoveDlg::CVideoMoveDlg(CWnd* pParent /*=NULL*/)
  55. : CDialog(CVideoMoveDlg::IDD, pParent)
  56. {
  57. //{{AFX_DATA_INIT(CVideoMoveDlg)
  58. //}}AFX_DATA_INIT
  59. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  60. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  61. }
  62. void CVideoMoveDlg::DoDataExchange(CDataExchange* pDX)
  63. {
  64. CDialog::DoDataExchange(pDX);
  65. //{{AFX_DATA_MAP(CVideoMoveDlg)
  66. DDX_Control(pDX, IDC_LIVEVIDEO, m_ctrlLiveVideo);
  67. DDX_Control(pDX, IDC_DEVICE, m_ctrlDevice);
  68. //}}AFX_DATA_MAP
  69. }
  70. BEGIN_MESSAGE_MAP(CVideoMoveDlg, CDialog)
  71. //{{AFX_MSG_MAP(CVideoMoveDlg)
  72. ON_WM_SYSCOMMAND()
  73. ON_WM_PAINT()
  74. ON_WM_QUERYDRAGICON()
  75. ON_BN_CLICKED(ID_OK, OnOk)
  76. ON_WM_ERASEBKGND()
  77. ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  78. ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  79. ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
  80. ON_WM_TIMER()
  81. ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
  82. //}}AFX_MSG_MAP
  83. END_MESSAGE_MAP()
  84. /////////////////////////////////////////////////////////////////////////////
  85. // CVideoMoveDlg message handlers
  86. BOOL CVideoMoveDlg::OnInitDialog()
  87. {
  88. CDialog::OnInitDialog();
  89. // Add "About..." menu item to system menu.
  90. // IDM_ABOUTBOX must be in the system command range.
  91. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  92. ASSERT(IDM_ABOUTBOX < 0xF000);
  93. CMenu* pSysMenu = GetSystemMenu(FALSE);
  94. if (pSysMenu != NULL)
  95. {
  96. CString strAboutMenu;
  97. strAboutMenu.LoadString(IDS_ABOUTBOX);
  98. if (!strAboutMenu.IsEmpty())
  99. {
  100. pSysMenu->AppendMenu(MF_SEPARATOR);
  101. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  102. }
  103. }
  104. // Set the icon for this dialog.  The framework does this automatically
  105. //  when the application's main window is not a dialog
  106. SetIcon(m_hIcon, TRUE); // Set big icon
  107. SetIcon(m_hIcon, FALSE); // Set small icon
  108. // TODO: Add extra initialization here
  109. if(m_VideoCapture.EnumDevices(m_ctrlDevice.m_hWnd )>0)
  110. {
  111. m_ctrlDevice.SetCurSel (0);
  112. // this->GetDlgItem (IDC_INITCAM)->EnableWindow(TRUE);
  113. }
  114. iPicNum=0;
  115. return TRUE;  // return TRUE  unless you set the focus to a control
  116. }
  117. void CVideoMoveDlg::OnSysCommand(UINT nID, LPARAM lParam)
  118. {
  119. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  120. {
  121. CAboutDlg dlgAbout;
  122. dlgAbout.DoModal();
  123. }
  124. else
  125. {
  126. CDialog::OnSysCommand(nID, lParam);
  127. }
  128. }
  129. // If you add a minimize button to your dialog, you will need the code below
  130. //  to draw the icon.  For MFC applications using the document/view model,
  131. //  this is automatically done for you by the framework.
  132. void CVideoMoveDlg::OnPaint() 
  133. {
  134. if (IsIconic())
  135. {
  136. CPaintDC dc(this); // device context for painting
  137. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  138. // Center icon in client rectangle
  139. int cxIcon = GetSystemMetrics(SM_CXICON);
  140. int cyIcon = GetSystemMetrics(SM_CYICON);
  141. CRect rect;
  142. GetClientRect(&rect);
  143. int x = (rect.Width() - cxIcon + 1) / 2;
  144. int y = (rect.Height() - cyIcon + 1) / 2;
  145. // Draw the icon
  146. dc.DrawIcon(x, y, m_hIcon);
  147. }
  148. else
  149. {
  150. CDialog::OnPaint();
  151. }
  152. }
  153. // The system calls this to obtain the cursor to display while the user drags
  154. //  the minimized window.
  155. HCURSOR CVideoMoveDlg::OnQueryDragIcon()
  156. {
  157. return (HCURSOR) m_hIcon;
  158. }
  159. void CVideoMoveDlg::OnOk() //视频开始
  160. {
  161. // TODO: Add your control notification handler code here m_ctrlVideoDisplay.m_hWnd
  162. int iSel=-1;
  163. iSel=m_ctrlDevice.GetCurSel ();
  164. m_VideoCapture.Init(iSel,m_ctrlLiveVideo.m_hWnd, 640, 480);
  165. // m_VideoCapture.EnableAnyVideo();
  166. }
  167. BOOL CVideoMoveDlg::OnEraseBkgnd(CDC* pDC) 
  168. {
  169. // TODO: Add your message handler code here and/or call default
  170. return CDialog::OnEraseBkgnd(pDC);
  171. }
  172. void CVideoMoveDlg::OnButton1() //重新开始
  173. {
  174. // TODO: Add your control notification handler code here
  175. m_VideoCapture.Pause ();
  176. // this->SetTimer (TIMER_ID,TIMER_DELAY,NULL);
  177. // m_VideoCapture.Init(-1,m_hWnd,320,240);
  178. // m_VideoCapture.DisableVideo();
  179. // m_VideoCapture.CaptureEnd();
  180. }
  181. void CVideoMoveDlg::OnCancel() //结束
  182. {
  183. // TODO: Add extra cleanup here
  184. /// m_VideoCapture.CaptureEnd();
  185. this->KillTimer (TIMER_ID);
  186. CDialog::OnCancel();
  187. }
  188. void CVideoMoveDlg::OnButton2() //捕获图片
  189. {
  190. // TODO: Add your control notification handler code here
  191. // m_VideoCapture.m_bStorageData = TRUE;
  192. this->KillTimer (TIMER_ID);
  193. this->SetTimer (TIMER_ID,TIMER_DELAY,NULL);
  194. }
  195. void CVideoMoveDlg::OnButton3() //暂停
  196. {
  197. // TODO: Add your control notification handler code here
  198. // this->KillTimer (TIMER_ID);
  199. m_VideoCapture.Pause ();
  200. }
  201. void CVideoMoveDlg::OnTimer(UINT nIDEvent) 
  202. {
  203. // TODO: Add your message handler code here and/or call default
  204. DWORD dwSize;
  205. iMark=TRUE;
  206. dwSize=this->m_VideoCapture.GrabFrame ();
  207. if(dwSize>0)
  208. {
  209. BYTE *pImage;
  210. this->m_VideoCapture.GetFrame (&pImage);
  211. CString sFileName;
  212. sFileName.Format("pics\pic%5d",iPicNum);
  213. //iDefine=this->m_VideoCapture.Trace(pImage);
  214. WriteAsBMP1(sFileName.GetBuffer(sFileName.GetLength()),pImage,640,480);
  215. iPicNum=iPicNum+1;
  216. }
  217. CDialog::OnTimer(nIDEvent);
  218. }
  219. BOOL CVideoMoveDlg::WriteAsBMP1(char* szFileName,unsigned char* pImgData,int nWidth,int nHeight)
  220. {
  221. char aFileName[255];
  222. int nStrLen;
  223. if(strstr(szFileName,".")==NULL)
  224. {
  225. strcpy(aFileName,szFileName);
  226. strcat(aFileName,".bmp");
  227. }
  228. else
  229. {
  230. nStrLen=strlen(szFileName)-strlen(strstr(szFileName,"."));
  231. strncpy(aFileName,szFileName,nStrLen);
  232. aFileName[nStrLen]='';
  233. strcat(aFileName,".bmp");
  234. }
  235. BITMAPFILEHEADER bmfHdr; // Header for Bitmap file
  236. /* Fill in file type (first 2 bytes must be "BM" for a bitmap) */
  237. bmfHdr.bfType = 0x4d42;  // "BM"
  238. // Calculate the file size by adding the DIB size to sizeof(BITMAPFILEHEADER)
  239. int nImgWidth;
  240. if (nWidth%4==0)
  241. {
  242. nImgWidth=nWidth*3;
  243. }
  244. else
  245. {
  246. nImgWidth=nWidth*3+nWidth%4;
  247. }
  248. bmfHdr.bfSize =(DWORD)( nImgWidth*nHeight + sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) );
  249. bmfHdr.bfReserved1 = 0;
  250. bmfHdr.bfReserved2 = 0;
  251. bmfHdr.bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));
  252. BITMAPINFOHEADER BMIH;   // Pointer to DIB info structure
  253. BMIH.biSize=sizeof(BITMAPINFOHEADER);
  254. BMIH.biHeight=nHeight;
  255. BMIH.biWidth=nWidth;
  256. BMIH.biPlanes=1;
  257. BMIH.biBitCount=24;
  258. BMIH.biCompression=BI_RGB;
  259. BMIH.biSizeImage=nImgWidth*nHeight;
  260. BMIH.biXPelsPerMeter=0xec4;
  261. BMIH.biYPelsPerMeter =0xec4;
  262. BMIH.biClrUsed =0;
  263. BMIH.biClrImportant=0;
  264. FILE *pFile;
  265. pFile=fopen(aFileName,"wb");
  266. if (pFile==NULL)
  267. {
  268. return FALSE;
  269. }
  270. BYTE aTemp[12]={0};
  271. try {
  272. // Write the file header
  273. fwrite((BYTE*)&bmfHdr,1,sizeof(BITMAPFILEHEADER),pFile);
  274. // Write the DIB header
  275.         fwrite((BYTE*)&BMIH,1,sizeof(BITMAPINFOHEADER),pFile);
  276. // Write the DIB bits
  277. int j;
  278. for (j=0;j<nHeight;j++)
  279. {
  280. int i;
  281. //i=j;
  282. i=nHeight-j;
  283. fwrite(pImgData+i*nWidth*3,1,nWidth*3,pFile);
  284. fwrite(aTemp,1,nWidth%4,pFile);
  285. }
  286.     }
  287.     catch(...)
  288. {
  289. fclose(pFile);
  290.         return FALSE;
  291.     }
  292. fclose(pFile);
  293.     return TRUE;
  294. }
  295. void CVideoMoveDlg::OnButton4() //文件传送
  296. {
  297. // TODO: Add your control notification handler code here
  298. }