directX显示采集源(摄像头)filter 分类: DirectX 2014-09-20 08:43 548人阅读 评论(0) 收藏



IGraphBuilder * g_pGraphBuilder = NULL;
IBaseFilter *Pbf=0;
IVideoWindow  * g_pVWindow = NULL;
IMediaControl * g_pMControl = NULL;
IMediaEventEx * g_pMEvent = NULL;
ICaptureGraphBuilder2 * g_pCaptureBuilder = NULL;
IVideoWindow  *g_pivw  = NULL;


void CTestDlg::OnButton5() 
{
// TODO: Add your control notification handler code here
HRESULT hr;
IBaseFilter *pSrcFilter=NULL;
     CoInitialize(NULL);
    // Create the filter graph
    hr = CoCreateInstance (CLSID_FilterGraph, NULL, CLSCTX_INPROC,
                           IID_IGraphBuilder, (void **) &g_pGraphBuilder);
    // Create the capture graph builder
    hr = CoCreateInstance (CLSID_CaptureGraphBuilder2 , NULL, CLSCTX_INPROC,
                           IID_ICaptureGraphBuilder2, (void **) &g_pCaptureBuilder);
    // Obtain interfaces for media control and Video Window
    hr = g_pGraphBuilder->QueryInterface(IID_IMediaControl,(LPVOID *) &g_pMControl);
    hr = g_pGraphBuilder->QueryInterface(IID_IVideoWindow, (LPVOID *) &g_pVWindow);
    hr = g_pGraphBuilder->QueryInterface(IID_IMediaEvent, (LPVOID *) &g_pMEvent);
         g_pGraphBuilder->QueryInterface(IID_IVideoWindow, (void **)&g_pivw);
    // Set the window handle used to process graph events
    hr = g_pMEvent->SetNotifyWindow((OAHWND)ghApp, WM_GRAPHNOTIFY, 0);
//
hr = g_pCaptureBuilder->SetFiltergraph(g_pGraphBuilder);
hr = FindCaptureDevice(&pSrcFilter);
    if (FAILED(hr))
    {
        // Don't display a message because FindCaptureDevice will handle it
        exit(1);
    }
    Pbf = pSrcFilter;
    // Add Capture filter to our graph.
    hr = g_pGraphBuilder->AddFilter(pSrcFilter, L"Video Capture");
hr = g_pCaptureBuilder->RenderStream (&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video,
                                   pSrcFilter, NULL, NULL);
pSrcFilter->Release();


// hr = SetupVideoWindow(0);
///* //////////////////////////////////////////////////
    HWND m_hwnd = m_Screen2.GetSafeHwnd();
    m_Screen2.ModifyStyle(0, WS_CLIPCHILDREN);
    RECT rc;
m_Screen2.GetClientRect(&rc);

hr = g_pVWindow->put_Owner((OAHWND)m_hwnd);
hr = g_pVWindow->put_WindowStyle(WS_CHILD | WS_CLIPCHILDREN);

hr = g_pVWindow->SetWindowPosition(0, 0, (rc.right), (rc.bottom));
hr = g_pVWindow->put_Visible(OATRUE);//*/
//////////////////////////////////////////////////
DWORD g_dwGraphRegister=0;
hr = g_pMControl->Run();
//


}
int rate=1;
void CTestDlg::OnButton2() 
{   
if (pims)
{    
rate++;
if(rate==4)
{
rate=1;

}
pims->SetRate(rate);
CString str;
str.Format("快进>> X %d",rate);
(GetDlgItem(IDC_BUTTON2))->SetWindowText(str);
}


// pivw->GetCurrentImage(&lpCurrImage) ;

}



HRESULT FindCaptureDevice(IBaseFilter ** ppSrcFilter)
{
    HRESULT hr;
    IBaseFilter * pSrc = NULL;
    IMoniker *pMoniker;
    ULONG cFetched;


    if (!ppSrcFilter)
        return E_POINTER;
   
    // Create the system device enumerator
    CComPtr <ICreateDevEnum> pDevEnum =NULL;


    hr = CoCreateInstance (CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
                           IID_ICreateDevEnum, (void **) &pDevEnum);
    if (FAILED(hr))
    {
    //    Msg(TEXT("Couldn't create system enumerator!  hr=0x%x"), hr);
        return hr;
    }


    // Create an enumerator for the video capture devices
   // CComPtr <IEnumMoniker> pClassEnum = NULL;
IEnumMoniker *pClassEnum=0;
    hr = pDevEnum->CreateClassEnumerator (CLSID_VideoInputDeviceCategory, &pClassEnum, 0);
    if (FAILED(hr))
    {
      //  Msg(TEXT("Couldn't create class enumerator!  hr=0x%x"), hr);
        return hr;
    }


    // If there are no enumerators for the requested type, then 
    // CreateClassEnumerator will succeed, but pClassEnum will be NULL.
    if (pClassEnum == NULL)
    {
        MessageBox(ghApp,TEXT("No video capture device was detected.\r\n\r\n")
                   TEXT("This sample requires a video capture device, such as a USB WebCam,\r\n")
                   TEXT("to be installed and working properly.  The sample will now close."),
                   TEXT("No Video Capture Hardware"), MB_OK | MB_ICONINFORMATION);
        return E_FAIL;
    }


    // Use the first video capture device on the device list.
    // Note that if the Next() call succeeds but there are no monikers,
    // it will return S_FALSE (which is not a failure).  Therefore, we
    // check that the return code is S_OK instead of using SUCCEEDED() macro.
    /*if (S_OK == (pClassEnum->Next (1, &pMoniker, &cFetched)))
    {
        // Bind Moniker to a filter object
        hr = pMoniker->BindToObject(0,0,IID_IBaseFilter, (void**)&pSrc);
        if (FAILED(hr))
        {
          //  Msg(TEXT("Couldn't bind moniker to filter object!  hr=0x%x"), hr);
            return hr;
        }
    }
    else
    {
       // Msg(TEXT("Unable to access video capture device!"));   
        return E_FAIL;
    }*/


while(S_OK == (pClassEnum->Next (1, &pMoniker, &cFetched)))
    {
IPropertyBag *pProp= 0;
pMoniker->BindToStorage(0,0,IID_IPropertyBag,(void**)&pProp);
VARIANT varName;
varName.vt = VT_BSTR;
hr=pProp->Read(L"FriendlyName",&varName,0);
if(hr == NOERROR)
        {
CString str(varName.bstrVal);


if (str.Find("Virtual Camera",0)!=-1)
{
hr = pMoniker->BindToObject(0,0,IID_IBaseFilter, (void**)&pSrc);
if (FAILED(hr))
{
//  Msg(TEXT("Couldn't bind moniker to filter object!  hr=0x%x"), hr);
break;
}
break;
}

}
pMoniker->Release();
    }
    // Copy the found filter pointer to the output parameter.
    // Do NOT Release() the reference, since it will still be used
    // by the calling function.
    *ppSrcFilter = pSrc;


    return hr;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2014-09-20 08:43  毛毛虫的薄刻  阅读(235)  评论(0编辑  收藏  举报