发表于 2015-5-17 10:32:56

Win7下使用VFW库连接摄像头

  本文转自:http://hi.baidu.com/blogofivan/blog/item/bc28009bb8ee45036f068c6d.html
  VFW库在XP下很好用,但是移到Vista或者Win7下,不正常了.首先是摄像头设备连接不上,再有摄像头显示大小不能变.
  1.   HWND m_hWndCap = capCreateCaptureWindow(Name, WS_VISIBLE | WS_CHILD, left, top, width, height, hWnd, 1);
  其中Name是子窗体名称, 第二个参数是窗口样式,这里是可见+字窗口,后面四个参数是窗口显示位置和大小,hWnd是父窗口句柄,最后一个参数窗口ID,返回值为新建窗体句柄
  2.   capDriverConnect(m_hWndCap, 0),这里0是指默认摄像头设备,但是如果您的电脑上有多个摄像头,就要用循环了:
  for(int Index=0; IndexlpData中
         // 而大小保存在lpVHdr->dwBytesUsed
      return TRUE;
}
  
  
  其它的例子:
  连接vfw32.lib的库,并在对话框头文件中加#include"vfw.h"
  对上面的控件分别双击产生其对应的响应函数如下写代码:
  void CUSBCameraDlg::OnVideo()
{
// TODO: Add your control notification handler code here
//create a window for captureWindow
CWnd *mywnd=new CWnd;
mywnd->Create(_T("STATIC"),"",WS_CHILD | WS_VISIBLE,CRect(0,0,250,250),this,1234);
mywnd->ShowWindow(SW_SHOW);
CRect rect;
mywnd->GetWindowRect(rect);
//create capture window
ghCapWnd=capCreateCaptureWindow("My Own Capture Window",WS_CHILD | WS_VISIBLE,0,0,(rect.right-rect.left),(rect.bottom-rect.top),mywnd->GetSafeHwnd(),1235);
//连接设备
capDriverConnect(ghCapWnd,0);
//获得参数
CAPTUREPARMS CapParms;
capCaptureGetSetup(ghCapWnd,&CapParms,sizeof(CAPTUREPARMS));
//设置帧数
CapParms.fLimitEnabled=FALSE;
//是否捕捉音频
CapParms.fCaptureAudio=FALSE;
//MCI Device支持
CapParms.fMCIControl=FALSE;
//设置窗口,如果为false,捕捉画面在桌面上
CapParms.fYield=TRUE;
//停止捕捉键设置
CapParms.vKeyAbort=VK_ESCAPE;
CapParms.fAbortLeftMouse=FALSE;
CapParms.fAbortRightMouse=FALSE;
capCaptureGetSetup(ghCapWnd,&CapParms,sizeof(CAPTUREPARMS));
//设置预览时的比例
capPreviewScale(ghCapWnd,66);
//是否支持比例变化
capPreviewScale(ghCapWnd,FALSE);
//打开预览
capPreview(ghCapWnd,1);
}
  void CUSBCameraDlg::OnCapture()
{
// TODO: Add your control notification handler code here
capCaptureSequence(ghCapWnd);
}
  void CUSBCameraDlg::OnStopvideo()
{
// TODO: Add your control notification handler code here
capDriverDisconnect(ghCapWnd);
}
  void CUSBCameraDlg::OnStopcapture()
{
// TODO: Add your control notification handler code here
capCaptureAbort(ghCapWnd);
}
页: [1]
查看完整版本: Win7下使用VFW库连接摄像头