VideoMoveDlg.cpp
资源名称:VideoMove.rar [点击查看]
上传用户:czshopping
上传日期:2022-05-22
资源大小:5430k
文件大小:8k
源码类别:
视频捕捉/采集
开发平台:
Visual C++
- // VideoMoveDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "VideoMove.h"
- #include "VideoMoveDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define TIMER_DELAY 50
- #define TIMER_ID 1
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- BOOL iMark=FALSE;
- int iPicNum;
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CVideoMoveDlg dialog
- CVideoMoveDlg::CVideoMoveDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CVideoMoveDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CVideoMoveDlg)
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CVideoMoveDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CVideoMoveDlg)
- DDX_Control(pDX, IDC_LIVEVIDEO, m_ctrlLiveVideo);
- DDX_Control(pDX, IDC_DEVICE, m_ctrlDevice);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CVideoMoveDlg, CDialog)
- //{{AFX_MSG_MAP(CVideoMoveDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(ID_OK, OnOk)
- ON_WM_ERASEBKGND()
- ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
- ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
- ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
- ON_WM_TIMER()
- ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CVideoMoveDlg message handlers
- BOOL CVideoMoveDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- // TODO: Add extra initialization here
- if(m_VideoCapture.EnumDevices(m_ctrlDevice.m_hWnd )>0)
- {
- m_ctrlDevice.SetCurSel (0);
- // this->GetDlgItem (IDC_INITCAM)->EnableWindow(TRUE);
- }
- iPicNum=0;
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CVideoMoveDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CVideoMoveDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CVideoMoveDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CVideoMoveDlg::OnOk() //视频开始
- {
- // TODO: Add your control notification handler code here m_ctrlVideoDisplay.m_hWnd
- int iSel=-1;
- iSel=m_ctrlDevice.GetCurSel ();
- m_VideoCapture.Init(iSel,m_ctrlLiveVideo.m_hWnd, 640, 480);
- // m_VideoCapture.EnableAnyVideo();
- }
- BOOL CVideoMoveDlg::OnEraseBkgnd(CDC* pDC)
- {
- // TODO: Add your message handler code here and/or call default
- return CDialog::OnEraseBkgnd(pDC);
- }
- void CVideoMoveDlg::OnButton1() //重新开始
- {
- // TODO: Add your control notification handler code here
- m_VideoCapture.Pause ();
- // this->SetTimer (TIMER_ID,TIMER_DELAY,NULL);
- // m_VideoCapture.Init(-1,m_hWnd,320,240);
- // m_VideoCapture.DisableVideo();
- // m_VideoCapture.CaptureEnd();
- }
- void CVideoMoveDlg::OnCancel() //结束
- {
- // TODO: Add extra cleanup here
- /// m_VideoCapture.CaptureEnd();
- this->KillTimer (TIMER_ID);
- CDialog::OnCancel();
- }
- void CVideoMoveDlg::OnButton2() //捕获图片
- {
- // TODO: Add your control notification handler code here
- // m_VideoCapture.m_bStorageData = TRUE;
- this->KillTimer (TIMER_ID);
- this->SetTimer (TIMER_ID,TIMER_DELAY,NULL);
- }
- void CVideoMoveDlg::OnButton3() //暂停
- {
- // TODO: Add your control notification handler code here
- // this->KillTimer (TIMER_ID);
- m_VideoCapture.Pause ();
- }
- void CVideoMoveDlg::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- DWORD dwSize;
- iMark=TRUE;
- dwSize=this->m_VideoCapture.GrabFrame ();
- if(dwSize>0)
- {
- BYTE *pImage;
- this->m_VideoCapture.GetFrame (&pImage);
- CString sFileName;
- sFileName.Format("pics\pic%5d",iPicNum);
- //iDefine=this->m_VideoCapture.Trace(pImage);
- WriteAsBMP1(sFileName.GetBuffer(sFileName.GetLength()),pImage,640,480);
- iPicNum=iPicNum+1;
- }
- CDialog::OnTimer(nIDEvent);
- }
- BOOL CVideoMoveDlg::WriteAsBMP1(char* szFileName,unsigned char* pImgData,int nWidth,int nHeight)
- {
- char aFileName[255];
- int nStrLen;
- if(strstr(szFileName,".")==NULL)
- {
- strcpy(aFileName,szFileName);
- strcat(aFileName,".bmp");
- }
- else
- {
- nStrLen=strlen(szFileName)-strlen(strstr(szFileName,"."));
- strncpy(aFileName,szFileName,nStrLen);
- aFileName[nStrLen]=' ';
- strcat(aFileName,".bmp");
- }
- BITMAPFILEHEADER bmfHdr; // Header for Bitmap file
- /* Fill in file type (first 2 bytes must be "BM" for a bitmap) */
- bmfHdr.bfType = 0x4d42; // "BM"
- // Calculate the file size by adding the DIB size to sizeof(BITMAPFILEHEADER)
- int nImgWidth;
- if (nWidth%4==0)
- {
- nImgWidth=nWidth*3;
- }
- else
- {
- nImgWidth=nWidth*3+nWidth%4;
- }
- bmfHdr.bfSize =(DWORD)( nImgWidth*nHeight + sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) );
- bmfHdr.bfReserved1 = 0;
- bmfHdr.bfReserved2 = 0;
- bmfHdr.bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));
- BITMAPINFOHEADER BMIH; // Pointer to DIB info structure
- BMIH.biSize=sizeof(BITMAPINFOHEADER);
- BMIH.biHeight=nHeight;
- BMIH.biWidth=nWidth;
- BMIH.biPlanes=1;
- BMIH.biBitCount=24;
- BMIH.biCompression=BI_RGB;
- BMIH.biSizeImage=nImgWidth*nHeight;
- BMIH.biXPelsPerMeter=0xec4;
- BMIH.biYPelsPerMeter =0xec4;
- BMIH.biClrUsed =0;
- BMIH.biClrImportant=0;
- FILE *pFile;
- pFile=fopen(aFileName,"wb");
- if (pFile==NULL)
- {
- return FALSE;
- }
- BYTE aTemp[12]={0};
- try {
- // Write the file header
- fwrite((BYTE*)&bmfHdr,1,sizeof(BITMAPFILEHEADER),pFile);
- // Write the DIB header
- fwrite((BYTE*)&BMIH,1,sizeof(BITMAPINFOHEADER),pFile);
- // Write the DIB bits
- int j;
- for (j=0;j<nHeight;j++)
- {
- int i;
- //i=j;
- i=nHeight-j;
- fwrite(pImgData+i*nWidth*3,1,nWidth*3,pFile);
- fwrite(aTemp,1,nWidth%4,pFile);
- }
- }
- catch(...)
- {
- fclose(pFile);
- return FALSE;
- }
- fclose(pFile);
- return TRUE;
- }
- void CVideoMoveDlg::OnButton4() //文件传送
- {
- // TODO: Add your control notification handler code here
- }