zoukankan      html  css  js  c++  java
  • 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);
    }

  • 相关阅读:
    Ubuntu18.04连不上网的解决办法
    springboot 踩雷 invalid bound statement (not found): com.atguigu.springboot.mapper.employeemapper.getempbyid
    IDEA的pom文件总是出现Failed to read artifact descriptor forXXX:jar:unknow的解决方法
    IDEA快捷键记录
    在ubuntu18.04中使用chorme浏览器,发现鼠标向下滑动速度真的很慢!更改鼠标滚动速度解决!!
    使用docker拉取镜像时,一直报错get https://registry-1.docker.io/v2/: net/http: tls handshake timeout
    启动django服务器访问admin站点时,django服务器自动关闭的问题解决方案。
    mathtype 6.9 setup cannot continue as an installation of mathtype 7 has be detected。彻底删除MathType的方法。
    转载:Microsoft Office 365激活,一键激活,无需登录Microsoft账号,无需下载KMS等激活工具,cmd命令激活。
    SpringBoot快速开发01
  • 原文地址:https://www.cnblogs.com/k1988/p/2165637.html
Copyright © 2011-2022 走看看