明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

使用DirectShow驅(qū)動(dòng)攝像頭

[摘要]對(duì)于小攝像頭的驅(qū)動(dòng)有幾種方法,通過使用DirectShow來驅(qū)動(dòng)攝像頭靈活性比較好,有簡(jiǎn)單的方法也有比較復(fù)雜但更有效的方法,本文只介紹簡(jiǎn)單方法,希望與大家交流! 用DirectShow來使用攝像頭,一般要求攝像頭的驅(qū)動(dòng)是WDM格式的,當(dāng)然,一些比較老的驅(qū)動(dòng)格式DirectShow也可支...
       對(duì)于小攝像頭的驅(qū)動(dòng)有幾種方法,通過使用DirectShow來驅(qū)動(dòng)攝像頭靈活性比較好,有簡(jiǎn)單的方法也有比較復(fù)雜但更有效的方法,本文只介紹簡(jiǎn)單方法,希望與大家交流!

       用DirectShow來使用攝像頭,一般要求攝像頭的驅(qū)動(dòng)是WDM格式的,當(dāng)然,一些比較老的驅(qū)動(dòng)格式DirectShow也可支持。DirectShow通過圖形過濾管理器(Filter Graph Manager)來與上層應(yīng)用程序和下層的驅(qū)動(dòng)進(jìn)行聯(lián)系。DirectShow通過一種叫作捕獲過濾器(Capture Filter)的東東來支持對(duì)攝像頭的捕獲,一個(gè)捕獲過濾器有多個(gè)插口(pin),其中的預(yù)覽(preview)插口可用來進(jìn)行顯示禎圖象。

       DirectShow通過幾個(gè)COM接口來對(duì)視頻捕獲的全過程進(jìn)行控制,其中IGraphBuilder 用于建立過濾器,ICaptureGraphBuilder2用于與下層的驅(qū)動(dòng)程序建立聯(lián)系,IVideoWindow,IMediaControl,IMediaEventEx分別對(duì)整個(gè)過程的視頻窗口,播放過程和事件響應(yīng)進(jìn)行控制,

下面是例程:

    CComQIPtr<IVideoWindow>   m_pVW;

    CComQIPtr<IMediaControl>  m_pMC;

    CComQIPtr<IMediaEventEx>  m_pME;

    CComPtr<IGraphBuilder>  m_pGraph;

    CComPtr<ICaptureGraphBuilder2>  m_pCapture;

    DWORD m_dwGraphRegister;

bool bInit(HWND hWnd)

{

    HRESULT hr;

//獲得接口

    hr=CoCreateInstance (CLSID_FilterGraph, NULL, CLSCTX_INPROC,

                           IID_IGraphBuilder, (void **) &m_pGraph);

    if (FAILED(hr))

        return false;



    hr=CoCreateInstance (CLSID_CaptureGraphBuilder2 , NULL, CLSCTX_INPROC,

                           IID_ICaptureGraphBuilder2, (void **) &m_pCapture);

    if (FAILED(hr))

        return false;



    m_pMC=m_pGraph;

    m_pVW=m_pGraph;

    m_pME=m_pGraph;

    //取得消息

    m_pME->SetNotifyWindow((OAHWND)(m_hWnd=hWnd), WM_GRAPHNOTIFY, 0);

    //將過濾和捕獲進(jìn)行連接

    m_pCapture->SetFiltergraph(m_pGraph);

//設(shè)備聯(lián)接

    //枚舉設(shè)備

    CComPtr <ICreateDevEnum> pCde=NULL;

    CComPtr <IEnumMoniker> pEm=NULL;



    hr=CoCreateInstance (CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,

                           IID_ICreateDevEnum, (void **) &pCde);

    if (FAILED(hr))

        return false;

    pCde->CreateClassEnumerator(CLSID_VideoInputDeviceCategory

        , &pEm, 0);

    if(pEm==NULL)

        return false;



    CComPtr <IMoniker> pM=NULL;

    ULONG cFetched;

    CComPtr<IBaseFilter> pBf=NULL;

    if(pEm->Next(1,&pM,&cFetched)==S_OK)

    {

        pM->BindToObject(0,0,IID_IBaseFilter, (void**)&pBf);

        pM.Release();

    }

    else

    {

        return false;

    }

    //將設(shè)備添加到graph

    hr = m_pGraph->AddFilter(pBf, L"Video Capture");

    if (FAILED(hr))

        return false;

    //連接一個(gè)源插口

    hr=m_pCapture->RenderStream(&PIN_CATEGORY_PREVIEW,&MEDIATYPE_Video

        ,pBf,NULL,NULL);

    if (FAILED(hr))

        return false;



    pBf.Release();

//設(shè)定視頻窗口

    //設(shè)定視頻窗口為主窗口的一個(gè)子窗口

    hr=m_pVW->put_Owner((OAHWND)hWnd);

    if (FAILED(hr))

        return false;

    //設(shè)定窗口樣式

    m_pVW->put_WindowStyle(WS_CHILD WS_CLIPCHILDREN);

    if (FAILED(hr))

        return false;

    //設(shè)定窗口大小

    CRect rectClient;

    GetClientRect(hWnd,rectClient);

    m_pVW->SetWindowPosition(0, 0, 320, 240);

    //設(shè)定可視

    hr=m_pVW->put_Visible(OATRUE);

    if (FAILED(hr))

        return false;

    //將對(duì)象加入到運(yùn)行對(duì)象列表中

    CComPtr<IRunningObjectTable> pROT;

    GetRunningObjectTable(0,&pROT);

    WCHAR c[128];

    wsprintfW(c, L"FilterGraph %08x pid %08x\0", (DWORD_PTR)m_pGraph.p,

              GetCurrentProcessId());



    hr = CreateItemMoniker(L"!",c,&pM);

    if (FAILED(hr))

        return false;



    hr = pROT->Register(ROTFLAGS_REGISTRATIONKEEPSALIVE,m_pGraph,

        pM,&m_dwGraphRegister);

    pM.Release();



    return false;

}