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; Index<MAX_VFW_DEVICES;Index ++){

              if(capGetDriverDescription(Index,szDeviceName,sizeof(szDeviceName),szDeviceVersion,

                    sizeof(szDeviceVersion))){

                            try{

                                   if(capDriverConnect(m_hWndVideo,Index)){

                                        m_ValidDriverIndex[m_TotalVideoDrivers]=Index;

                                        capDriverDisconnect(m_hWndVideo);

                           }...

                 }

          }

这里Win7和XP处理上有些不一样,Win7上capDriverConnect需要用死循环,就是把capDriverConnect写到while里。

3. capPreviewRate(m_hWndCap, 50);设置采集频率,每秒多少帧。

4.   capPreview(hCap, TRUE); 开始采集了,呵呵

5. 程序退出了别忘了调用:capPreview(hCap, FALSE);

                                            capDriverDisconnect(m_hWndCap);

OK,大功告成。

6.忘了说了,我们怎么操作捕捉到的画面呢?

答案是使用capSetCallbackOnVideoStream(HWND, CAPVIDEOCALLBACK),其中,HWND是画面显示的子窗体,CAPVIDEOCALLBACK是处理函数地址

处理函数这么写:

LRESULT CALLBACK VideoStreamCallbackProc(HWND hWnd,LPVIDEOHDR lpVHdr){

        // 这里添加你自己的处理代码,其中图像数据保存在结构lpVHdr->lpData中

       // 而大小保存在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);
}

你可能感兴趣的:(c,XP,callback)