天天看點

directshow的視訊采集

 directxShow的視訊采集,9.0版本之後的sdk把directshow從中分離出來了,是以本人下了後面的幾個版本都沒有找到要的SDK,汗啊.......

 是以下載下傳9.0b,在微軟的網站上有,後面好像有個(2003)字樣.去下吧.

好了,從裡面提出一個隻采集視訊的程式.

1.

directshow的視訊采集

//  TestVideoDlg.h : 頭檔案

directshow的視訊采集

//

directshow的視訊采集
directshow的視訊采集

#pragma  once

directshow的視訊采集

#include  " afxwin.h "

directshow的視訊采集
directshow的視訊采集
directshow的視訊采集

//  CTestVideoDlg 對話框

directshow的視訊采集

class  CTestVideoDlg :  public  CDialog

directshow的視訊采集
directshow的視訊采集

... {

directshow的視訊采集

// 構造

directshow的視訊采集

public:

directshow的視訊采集

    CTestVideoDlg(CWnd* pParent = NULL);    // 标準構造函數

directshow的視訊采集
directshow的視訊采集

// 對話框資料

directshow的視訊采集
directshow的視訊采集

    enum ...{ IDD = IDD_TESTVIDEO_DIALOG };

directshow的視訊采集
directshow的視訊采集

    protected:

directshow的視訊采集

    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支援

directshow的視訊采集
directshow的視訊采集
directshow的視訊采集

// 實作

directshow的視訊采集

protected:

directshow的視訊采集

    HICON m_hIcon;

directshow的視訊采集
directshow的視訊采集

    // 生成的消息映射函數

directshow的視訊采集

    virtual BOOL OnInitDialog();

directshow的視訊采集

    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);

directshow的視訊采集

    afx_msg void OnPaint();

directshow的視訊采集

    afx_msg HCURSOR OnQueryDragIcon();

directshow的視訊采集

    DECLARE_MESSAGE_MAP()

directshow的視訊采集

public:

directshow的視訊采集

    CStatic m_wndVideo;

directshow的視訊采集

protected:

directshow的視訊采集

    CComPtr< IGraphBuilder > m_pGraph;

directshow的視訊采集

    CComPtr< ISampleGrabber > m_pGrabber;

directshow的視訊采集
directshow的視訊采集

    HRESULT InitStillGraph( );

directshow的視訊采集
directshow的視訊采集

    void Error( TCHAR * pText );

directshow的視訊采集
directshow的視訊采集

    void GetDefaultCapDevice( IBaseFilter ** ppCap );

directshow的視訊采集

} ;

directshow的視訊采集

2.實作部分

directshow的視訊采集

//  TestVideoDlg.cpp : 實作檔案

directshow的視訊采集

//

directshow的視訊采集
directshow的視訊采集

#include  " stdafx.h "

directshow的視訊采集

#include  " TestVideo.h "

directshow的視訊采集

#include  " TestVideoDlg.h "

directshow的視訊采集
directshow的視訊采集

#include  " ......commondshowutil.h "

directshow的視訊采集
directshow的視訊采集

#ifdef _DEBUG

directshow的視訊采集

#define  new DEBUG_NEW

directshow的視訊采集

#endif

directshow的視訊采集
directshow的視訊采集
directshow的視訊采集

#ifdef DEBUG

directshow的視訊采集

#define  REGISTER_FILTERGRAPH

directshow的視訊采集

#endif

directshow的視訊采集
directshow的視訊采集

//  用于應用程式“關于”菜單項的 CAboutDlg 對話框

directshow的視訊采集
directshow的視訊采集

class  CAboutDlg :  public  CDialog

directshow的視訊采集
directshow的視訊采集

... {

directshow的視訊采集

public:

directshow的視訊采集

    CAboutDlg();

directshow的視訊采集
directshow的視訊采集

// 對話框資料

directshow的視訊采集
directshow的視訊采集

    enum ...{ IDD = IDD_ABOUTBOX };

directshow的視訊采集
directshow的視訊采集

    protected:

directshow的視訊采集

    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支援

directshow的視訊采集
directshow的視訊采集

// 實作

directshow的視訊采集

protected:

directshow的視訊采集

    DECLARE_MESSAGE_MAP()

directshow的視訊采集

} ;

directshow的視訊采集
directshow的視訊采集

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)

directshow的視訊采集
directshow的視訊采集

... {

directshow的視訊采集

}

directshow的視訊采集
directshow的視訊采集

void  CAboutDlg::DoDataExchange(CDataExchange *  pDX)

directshow的視訊采集
directshow的視訊采集

... {

directshow的視訊采集

    CDialog::DoDataExchange(pDX);

directshow的視訊采集

}

directshow的視訊采集
directshow的視訊采集

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)

directshow的視訊采集

END_MESSAGE_MAP()

directshow的視訊采集
directshow的視訊采集
directshow的視訊采集

//  CTestVideoDlg 對話框

directshow的視訊采集
directshow的視訊采集

HWND g_hwnd = 0 ;

directshow的視訊采集

DWORD g_dwGraphRegister = 0 ;   //  For running object table

directshow的視訊采集
directshow的視訊采集
directshow的視訊采集

CTestVideoDlg::CTestVideoDlg(CWnd *  pParent  )

directshow的視訊采集

    : CDialog(CTestVideoDlg::IDD, pParent)

directshow的視訊采集
directshow的視訊采集

... {

directshow的視訊采集

    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

directshow的視訊采集

}

directshow的視訊采集
directshow的視訊采集

void  CTestVideoDlg::DoDataExchange(CDataExchange *  pDX)

directshow的視訊采集
directshow的視訊采集

... {

directshow的視訊采集

    CDialog::DoDataExchange(pDX);

directshow的視訊采集

    DDX_Control(pDX, IDC_STATIC_VIDEO, m_wndVideo);

directshow的視訊采集

}

directshow的視訊采集
directshow的視訊采集

BEGIN_MESSAGE_MAP(CTestVideoDlg, CDialog)

directshow的視訊采集

    ON_WM_SYSCOMMAND()

directshow的視訊采集

    ON_WM_PAINT()

directshow的視訊采集

    ON_WM_QUERYDRAGICON()

directshow的視訊采集

     // }}AFX_MSG_MAP

directshow的視訊采集

END_MESSAGE_MAP()

directshow的視訊采集
directshow的視訊采集
directshow的視訊采集

//  CTestVideoDlg 消息處理程式

directshow的視訊采集
directshow的視訊采集

BOOL CTestVideoDlg::OnInitDialog()

directshow的視訊采集
directshow的視訊采集

... {

directshow的視訊采集

    CDialog::OnInitDialog();

directshow的視訊采集
directshow的視訊采集

    // 将“關于...”菜單項添加到系統菜單中。

directshow的視訊采集
directshow的視訊采集

    // IDM_ABOUTBOX 必須在系統指令範圍内。

directshow的視訊采集

    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);

directshow的視訊采集

    ASSERT(IDM_ABOUTBOX < 0xF000);

directshow的視訊采集
directshow的視訊采集

    CMenu* pSysMenu = GetSystemMenu(FALSE);

directshow的視訊采集

    if (pSysMenu != NULL)

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        CString strAboutMenu;

directshow的視訊采集

        strAboutMenu.LoadString(IDS_ABOUTBOX);

directshow的視訊采集

        if (!strAboutMenu.IsEmpty())

directshow的視訊采集
directshow的視訊采集

        ...{

directshow的視訊采集

            pSysMenu->AppendMenu(MF_SEPARATOR);

directshow的視訊采集

            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);

directshow的視訊采集

        }

directshow的視訊采集

    }

directshow的視訊采集
directshow的視訊采集

    // 設定此對話框的圖示。當應用程式主視窗不是對話框時,架構将自動

directshow的視訊采集

    //  執行此操作

directshow的視訊采集

    SetIcon(m_hIcon, TRUE);            // 設定大圖示

directshow的視訊采集

    SetIcon(m_hIcon, FALSE);        // 設定小圖示

directshow的視訊采集
directshow的視訊采集

    // TODO: 在此添加額外的初始化代碼

directshow的視訊采集
directshow的視訊采集

    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

directshow的視訊采集
directshow的視訊采集

    g_hwnd = GetSafeHwnd();

directshow的視訊采集
directshow的視訊采集

    // start up the still image capture graph

directshow的視訊采集

    //

directshow的視訊采集

    HRESULT hr = InitStillGraph( );

directshow的視訊采集
directshow的視訊采集

    m_wndVideo.ModifyStyle(0, WS_CLIPCHILDREN);

directshow的視訊采集
directshow的視訊采集

    return TRUE;  // 除非設定了控件的焦點,否則傳回 TRUE

directshow的視訊采集

}

directshow的視訊采集
directshow的視訊采集

void  CTestVideoDlg::OnSysCommand(UINT nID, LPARAM lParam)

directshow的視訊采集
directshow的視訊采集

... {

directshow的視訊采集

    if ((nID & 0xFFF0) == IDM_ABOUTBOX)

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        CAboutDlg dlgAbout;

directshow的視訊采集

        dlgAbout.DoModal();

directshow的視訊采集

    }

directshow的視訊采集

    else

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        CDialog::OnSysCommand(nID, lParam);

directshow的視訊采集

    }

directshow的視訊采集

}

directshow的視訊采集
directshow的視訊采集

//  如果向對話框添加最小化按鈕,則需要下面的代碼

directshow的視訊采集

//   來繪制該圖示。對于使用文檔/視圖模型的 MFC 應用程式,

directshow的視訊采集

//   這将由架構自動完成。

directshow的視訊采集
directshow的視訊采集

void  CTestVideoDlg::OnPaint() 

directshow的視訊采集
directshow的視訊采集

... {

directshow的視訊采集

    if (IsIconic())

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        CPaintDC dc(this); // 用于繪制的裝置上下文

directshow的視訊采集
directshow的視訊采集

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

directshow的視訊采集
directshow的視訊采集

        // 使圖示在工作矩形中居中

directshow的視訊采集

        int cxIcon = GetSystemMetrics(SM_CXICON);

directshow的視訊采集

        int cyIcon = GetSystemMetrics(SM_CYICON);

directshow的視訊采集

        CRect rect;

directshow的視訊采集

        GetClientRect(&rect);

directshow的視訊采集

        int x = (rect.Width() - cxIcon + 1) / 2;

directshow的視訊采集

        int y = (rect.Height() - cyIcon + 1) / 2;

directshow的視訊采集
directshow的視訊采集

        // 繪制圖示

directshow的視訊采集

        dc.DrawIcon(x, y, m_hIcon);

directshow的視訊采集

    }

directshow的視訊采集

    else

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        CDialog::OnPaint();

directshow的視訊采集

    }

directshow的視訊采集

}

directshow的視訊采集
directshow的視訊采集

// 當使用者拖動最小化視窗時系統調用此函數取得光标顯示。

directshow的視訊采集

HCURSOR CTestVideoDlg::OnQueryDragIcon()

directshow的視訊采集
directshow的視訊采集

... {

directshow的視訊采集

    return static_cast<HCURSOR>(m_hIcon);

directshow的視訊采集

}

directshow的視訊采集

void  CTestVideoDlg::Error( TCHAR  *  pText )

directshow的視訊采集
directshow的視訊采集

... {

directshow的視訊采集

    ::MessageBox( NULL, pText, TEXT("Error!"), MB_OK | MB_TASKMODAL | MB_SETFOREGROUND );

directshow的視訊采集

}

directshow的視訊采集
directshow的視訊采集
directshow的視訊采集

HRESULT CTestVideoDlg::InitStillGraph( )

directshow的視訊采集
directshow的視訊采集

... {

directshow的視訊采集

    HRESULT hr;

directshow的視訊采集
directshow的視訊采集

    // create a filter graph

directshow的視訊采集

    //

directshow的視訊采集

    hr = m_pGraph.CoCreateInstance( CLSID_FilterGraph );

directshow的視訊采集

    if( !m_pGraph )

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        Error( TEXT("Could not create filter graph") );

directshow的視訊采集

        return E_FAIL;

directshow的視訊采集

    }

directshow的視訊采集
directshow的視訊采集

    // get whatever capture device exists

directshow的視訊采集

    //

directshow的視訊采集

    CComPtr< IBaseFilter > pCap;

directshow的視訊采集

    GetDefaultCapDevice( &pCap );

directshow的視訊采集

    if( !pCap )

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        Error( TEXT("No video capture device was detected on your system. ")

directshow的視訊采集

               TEXT("This sample requires a functional video capture device, such ")

directshow的視訊采集

               TEXT("as a USB web camera.  Video capture will be disabled.") );

directshow的視訊采集

        return E_FAIL;

directshow的視訊采集

    }

directshow的視訊采集
directshow的視訊采集

    // add the capture filter to the graph

directshow的視訊采集

    //

directshow的視訊采集

    hr = m_pGraph->AddFilter( pCap, L"Cap" );

directshow的視訊采集

    if( FAILED( hr ) )

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        Error( TEXT("Could not put capture device in graph"));

directshow的視訊采集

        return E_FAIL;

directshow的視訊采集

    }

directshow的視訊采集
directshow的視訊采集

    // create a sample grabber

directshow的視訊采集

    //

directshow的視訊采集

    hr = m_pGrabber.CoCreateInstance( CLSID_SampleGrabber );

directshow的視訊采集

    if( !m_pGrabber )

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        Error( TEXT("Could not create SampleGrabber (is qedit.dll registered?)"));

directshow的視訊采集

        return hr;

directshow的視訊采集

    }

directshow的視訊采集

    CComQIPtr< IBaseFilter, &IID_IBaseFilter > pGrabBase( m_pGrabber );

directshow的視訊采集
directshow的視訊采集

    // force it to connect to video, 24 bit

directshow的視訊采集

    //

directshow的視訊采集

    CMediaType VideoType;

directshow的視訊采集

    VideoType.SetType( &MEDIATYPE_Video );

directshow的視訊采集

    VideoType.SetSubtype( &MEDIASUBTYPE_RGB24 );

directshow的視訊采集

    hr = m_pGrabber->SetMediaType( &VideoType ); // shouldn't fail

directshow的視訊采集

    if( FAILED( hr ) )

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        Error( TEXT("Could not set media type"));

directshow的視訊采集

        return hr;

directshow的視訊采集

    }

directshow的視訊采集
directshow的視訊采集

    // add the grabber to the graph

directshow的視訊采集

    //

directshow的視訊采集

    hr = m_pGraph->AddFilter( pGrabBase, L"Grabber" );

directshow的視訊采集

    if( FAILED( hr ) )

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        Error( TEXT("Could not put sample grabber in graph"));

directshow的視訊采集

        return hr;

directshow的視訊采集

    }

directshow的視訊采集
directshow的視訊采集

    // build the graph

directshow的視訊采集

    CComPtr<ICaptureGraphBuilder2> pCGB2;

directshow的視訊采集

    hr = pCGB2.CoCreateInstance (CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC);

directshow的視訊采集

    if (FAILED( hr ))

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        Error(TEXT ("Can't get a ICaptureGraphBuilder2 reference"));

directshow的視訊采集

        return hr;

directshow的視訊采集

    }

directshow的視訊采集
directshow的視訊采集

    hr = pCGB2->SetFiltergraph( m_pGraph );

directshow的視訊采集

    if (FAILED( hr ))

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        Error(TEXT("SetGraph failed"));

directshow的視訊采集

        return hr;

directshow的視訊采集

    }

directshow的視訊采集
directshow的視訊采集
directshow的視訊采集

    // If there is a VP pin present on the video device, then put the 

directshow的視訊采集

    // renderer on CLSID_NullRenderer

directshow的視訊采集

    CComPtr<IPin> pVPPin;

directshow的視訊采集

    hr = pCGB2->FindPin(

directshow的視訊采集

                        pCap, 

directshow的視訊采集

                        PINDIR_OUTPUT, 

directshow的視訊采集

                        &PIN_CATEGORY_VIDEOPORT, 

directshow的視訊采集

                        NULL, 

directshow的視訊采集

                        FALSE, 

directshow的視訊采集

                        0, 

directshow的視訊采集

                        &pVPPin);

directshow的視訊采集
directshow的視訊采集
directshow的視訊采集

    // If there is a VP pin, put the renderer on NULL Renderer

directshow的視訊采集

    CComPtr<IBaseFilter> pRenderer;

directshow的視訊采集

    if (S_OK == hr)

directshow的視訊采集
directshow的視訊采集

    ...{   

directshow的視訊采集

        hr = pRenderer.CoCreateInstance(CLSID_NullRenderer);    

directshow的視訊采集

        if (S_OK != hr)

directshow的視訊采集
directshow的視訊采集

        ...{

directshow的視訊采集

            Error(TEXT("Unable to make a NULL renderer"));

directshow的視訊采集

            return S_OK;

directshow的視訊采集

        }

directshow的視訊采集

        hr = m_pGraph->AddFilter(pRenderer, L"NULL renderer");

directshow的視訊采集

        if (FAILED (hr))

directshow的視訊采集
directshow的視訊采集

        ...{

directshow的視訊采集

            Error(TEXT("Can't add the filter to graph"));

directshow的視訊采集

            return hr;

directshow的視訊采集

        }

directshow的視訊采集

    }

directshow的視訊采集
directshow的視訊采集

    hr = pCGB2->RenderStream(

directshow的視訊采集

                            &PIN_CATEGORY_PREVIEW,

directshow的視訊采集

                            &MEDIATYPE_Interleaved, 

directshow的視訊采集

                            pCap, 

directshow的視訊采集

                            pGrabBase, 

directshow的視訊采集

                            pRenderer);

directshow的視訊采集

    if (FAILED (hr))

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        // try to render preview pin

directshow的視訊采集

        hr = pCGB2->RenderStream( 

directshow的視訊采集

                                &PIN_CATEGORY_PREVIEW, 

directshow的視訊采集

                                &MEDIATYPE_Video,

directshow的視訊采集

                                pCap, 

directshow的視訊采集

                                pGrabBase, 

directshow的視訊采集

                                pRenderer);

directshow的視訊采集
directshow的視訊采集

        // try to render capture pin

directshow的視訊采集

        if( FAILED( hr ) )

directshow的視訊采集
directshow的視訊采集

        ...{

directshow的視訊采集

            hr = pCGB2->RenderStream( 

directshow的視訊采集

                                    &PIN_CATEGORY_CAPTURE, 

directshow的視訊采集

                                    &MEDIATYPE_Video,

directshow的視訊采集

                                    pCap, 

directshow的視訊采集

                                    pGrabBase, 

directshow的視訊采集

                                    pRenderer);

directshow的視訊采集

        }

directshow的視訊采集

    }

directshow的視訊采集
directshow的視訊采集

    if( FAILED( hr ) )

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        Error( TEXT("Can't build the graph") );

directshow的視訊采集

        return hr;

directshow的視訊采集

    }

directshow的視訊采集
directshow的視訊采集

    // ask for the connection media type so we know how big

directshow的視訊采集

    // it is, so we can write out bitmaps

directshow的視訊采集

    //

directshow的視訊采集

    AM_MEDIA_TYPE mt;

directshow的視訊采集

    hr = m_pGrabber->GetConnectedMediaType( &mt );

directshow的視訊采集

    if ( FAILED( hr) )

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        Error( TEXT("Could not read the connected media type"));

directshow的視訊采集

        return hr;

directshow的視訊采集

    }

directshow的視訊采集
directshow的視訊采集

    VIDEOINFOHEADER * vih = (VIDEOINFOHEADER*) mt.pbFormat;

directshow的視訊采集
directshow的視訊采集
directshow的視訊采集

    FreeMediaType( mt );

directshow的視訊采集
directshow的視訊采集

    // don't buffer the samples as they pass through

directshow的視訊采集

    //

directshow的視訊采集

    hr = m_pGrabber->SetBufferSamples( FALSE );

directshow的視訊采集
directshow的視訊采集

    // only grab one at a time, stop stream after

directshow的視訊采集

    // grabbing one sample

directshow的視訊采集

    //

directshow的視訊采集

    hr = m_pGrabber->SetOneShot( FALSE );

directshow的視訊采集
directshow的視訊采集

    // set the callback, so we can grab the one sample

directshow的視訊采集

    //

directshow的視訊采集

 // hr = m_pGrabber->SetCallback( &mCB, 1 );

directshow的視訊采集
directshow的視訊采集

    // find the video window and stuff it in our window

directshow的視訊采集

    //

directshow的視訊采集

    CComQIPtr< IVideoWindow, &IID_IVideoWindow > pWindow = m_pGraph;

directshow的視訊采集

    if( !pWindow )

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        Error( TEXT("Could not get video window interface"));

directshow的視訊采集

        return E_FAIL;

directshow的視訊采集

    }

directshow的視訊采集
directshow的視訊采集

    // set up the preview window to be in our dialog

directshow的視訊采集

    // instead of floating popup

directshow的視訊采集

    //

directshow的視訊采集

    HWND hwndPreview = NULL;

directshow的視訊采集

    GetDlgItem( IDC_STATIC_VIDEO, &hwndPreview );

directshow的視訊采集

    RECT rc;

directshow的視訊采集

    ::GetWindowRect( hwndPreview, &rc );

directshow的視訊采集
directshow的視訊采集

    hr = pWindow->put_Owner( (OAHWND) hwndPreview );

directshow的視訊采集

    hr = pWindow->put_Left( 0 );

directshow的視訊采集

    hr = pWindow->put_Top( 0 );

directshow的視訊采集

    hr = pWindow->put_Width( rc.right - rc.left );

directshow的視訊采集

    hr = pWindow->put_Height( rc.bottom - rc.top );

directshow的視訊采集

    hr = pWindow->put_WindowStyle( WS_CHILD | WS_CLIPSIBLINGS );

directshow的視訊采集

    hr = pWindow->put_Visible( OATRUE );

directshow的視訊采集
directshow的視訊采集

    // Add our graph to the running object table, which will allow

directshow的視訊采集

    // the GraphEdit application to "spy" on our graph

directshow的視訊采集

//#ifdef REGISTER_FILTERGRAPH

directshow的視訊采集

//    hr = AddGraphToRot(m_pGraph, &g_dwGraphRegister);

directshow的視訊采集

//    if (FAILED(hr))

directshow的視訊采集

//    {

directshow的視訊采集

//        Error(TEXT("Failed to register filter graph with ROT!"));

directshow的視訊采集

//        g_dwGraphRegister = 0;

directshow的視訊采集

//    }

directshow的視訊采集

//#endif

directshow的視訊采集
directshow的視訊采集

    // run the graph

directshow的視訊采集

    //

directshow的視訊采集

    CComQIPtr< IMediaControl, &IID_IMediaControl > pControl = m_pGraph;

directshow的視訊采集

    hr = pControl->Run( );

directshow的視訊采集

    if( FAILED( hr ) )

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        Error( TEXT("Could not run graph"));

directshow的視訊采集

        return hr;

directshow的視訊采集

    }

directshow的視訊采集
directshow的視訊采集

// UpdateStatus(_T("Previewing Live Video"));

directshow的視訊采集

    return 0;

directshow的視訊采集

}

directshow的視訊采集
directshow的視訊采集

void  CTestVideoDlg::GetDefaultCapDevice( IBaseFilter  **  ppCap )

directshow的視訊采集
directshow的視訊采集

... {

directshow的視訊采集

    HRESULT hr;

directshow的視訊采集
directshow的視訊采集

    ASSERT(ppCap);

directshow的視訊采集

    if (!ppCap)

directshow的視訊采集

        return;

directshow的視訊采集
directshow的視訊采集

    *ppCap = NULL;

directshow的視訊采集
directshow的視訊采集

    // create an enumerator

directshow的視訊采集

    //

directshow的視訊采集

    CComPtr< ICreateDevEnum > pCreateDevEnum;

directshow的視訊采集

    pCreateDevEnum.CoCreateInstance( CLSID_SystemDeviceEnum );

directshow的視訊采集
directshow的視訊采集

    ASSERT(pCreateDevEnum);

directshow的視訊采集

    if( !pCreateDevEnum )

directshow的視訊采集

        return;

directshow的視訊采集
directshow的視訊采集

    // enumerate video capture devices

directshow的視訊采集

    //

directshow的視訊采集

    CComPtr< IEnumMoniker > pEm;

directshow的視訊采集

    pCreateDevEnum->CreateClassEnumerator( CLSID_VideoInputDeviceCategory, &pEm, 0 );

directshow的視訊采集
directshow的視訊采集

    ASSERT(pEm);

directshow的視訊采集

    if( !pEm )

directshow的視訊采集

        return;

directshow的視訊采集
directshow的視訊采集

    pEm->Reset( );

directshow的視訊采集
directshow的視訊采集

    // go through and find first video capture device

directshow的視訊采集

    //

directshow的視訊采集

    while( 1 )

directshow的視訊采集
directshow的視訊采集

    ...{

directshow的視訊采集

        ULONG ulFetched = 0;

directshow的視訊采集

        CComPtr< IMoniker > pM;

directshow的視訊采集
directshow的視訊采集

        hr = pEm->Next( 1, &pM, &ulFetched );

directshow的視訊采集

        if( hr != S_OK )

directshow的視訊采集

            break;

directshow的視訊采集
directshow的視訊采集

        // get the property bag interface from the moniker

directshow的視訊采集

        //

directshow的視訊采集

        CComPtr< IPropertyBag > pBag;

directshow的視訊采集

        hr = pM->BindToStorage( 0, 0, IID_IPropertyBag, (void**) &pBag );

directshow的視訊采集

        if( hr != S_OK )

directshow的視訊采集

            continue;

directshow的視訊采集
directshow的視訊采集

        // ask for the english-readable name

directshow的視訊采集

        //

directshow的視訊采集

        CComVariant var;

directshow的視訊采集

        var.vt = VT_BSTR;

directshow的視訊采集

        hr = pBag->Read( L"FriendlyName", &var, NULL );

directshow的視訊采集

        if( hr != S_OK )

directshow的視訊采集

            continue;

directshow的視訊采集
directshow的視訊采集

        // set it in our UI

directshow的視訊采集

        //

directshow的視訊采集

        USES_CONVERSION;

directshow的視訊采集

//        SetDlgItemText( IDC_CAPOBJ, W2T( var.bstrVal ) );

directshow的視訊采集
directshow的視訊采集

        // ask for the actual filter

directshow的視訊采集

        //

directshow的視訊采集

        hr = pM->BindToObject( 0, 0, IID_IBaseFilter, (void**) ppCap );

directshow的視訊采集

        if( *ppCap )

directshow的視訊采集

            break;

directshow的視訊采集

    }

directshow的視訊采集
directshow的視訊采集

    return;

directshow的視訊采集

}

directshow的視訊采集

3.其中要的類庫都在directshow的SDK例子中能找到.

directshow的視訊采集

繼續閱讀