本文介绍了一起捕获视频和图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的

我想制作一个用于从网络摄像头捕获图像的应用程序,然后捕获视频5到10秒钟.我已经下载了DirectShow示例(CaptureNET和SampleGrabberNET),但无法将其加入1个示例中.

I want make an application for capturing image from webcam, after that capturing video for 5 or 10 seconds. I've download sample DirectShow (CaptureNET and SampleGrabberNET) but I can't join that into 1 sample.

实际上,我已经使用sourceforge.net的一些示例进行了合并,但是它仅适用于第一次图像捕获和第一次视频捕获,当我尝试再次捕获或抓取时,它会显示错误.

Actually I've made combine using some samples from sourceforge.net, but it only works for 1st image capture and 1st video capture, when I tried to capture or grab again, it shows an error.

有人做到了吗?

 

推荐答案

 

 

 

IPin * smart_outcappin;
    IPin * base_capturepin;
    IPin * base_previewpin;
    IPin * smart_inpin;
    IPin * smart_outprewpin;
    IPin * b_cfillOutc;
    IPin * PgrabIn;
    IPin * PgrabOut;
    IPin * PRenderIn;
    IPin * pin;
           
    HWND hwnd;
    hwnd =(HWND)hand;

    hr = CoCreateInstance(CLSID_CaptureGraphBuilder2,NULL,CLSCTX_INPROC,IID_ICaptureGraphBuilder2,(void **)&capturebuilder);
    hr = CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC,IID_IGraphBuilder,(void **)& graphbuilder);
    hr = graphbuilder-> QueryInterface(IID_IVideoWindow,(void **)& videowindow);
    hr = graphbuilder-> QueryInterface(IID_IMediaControl,(void **)& mediacontrol);

    hr = CoCreateInstance(CLSID_SystemDeviceEnum,NULL,CLSCTX_INPROC,IID_ICreateDevEnum,(void **)&devenum);
    hr = devenum-> CreateClassEnumerator(CLSID_VideoInputDeviceCategory,& enummoniker,NULL);
    hr = capturebuilder-> SetFiltergraph(graphbuilder);

    hr = CoCreateInstance(CLSID_SmartTee,NULL,CLSCTX_INPROC,IID_IBaseFilter,(void **)& smartteefilter);

   

   变量var;
    CComBSTR str = NULL;
    while(enummoniker-> Next(1,& mon,& l)== S_OK)
    {
        VariantInit(& var);
        mon-> BindToStorage(0,0,IID_IPropertyBag,(void **)& bag);
        bag-> Read(L"Friendlyname",& var,0);
        str = SysAllocString(var.bstrVal);
        if(str ==设备名称)
        {
            hr = mon-> BindToObject(0,0,IID_IBaseFilter,(void **)& basefilter);
           休息;
        }
    }
    CComPtr< IEnumPins> enumpin;
    hr = basefilter-> EnumPins(& enumpin);
    ULONG ul;


    while(enumpin-> Next(1,& pin,& ul)== S_OK)
    {
        PIN_INFO pininfo;
        pindir =(PIN_DIRECTION)2;
        pin-> QueryDirection(& pindir);
        pin-> QueryPinInfo(& pininfo);
        pintyp = SysAllocString(pininfo.achName);
            if(pintyp == pintype)
            {
               休息;
            }
           其他
            {
             继续;
            }
    }
   
    hr = graphbuilder-> AddFilter(basefilter,L"videoname");
    hr = graphbuilder-> AddFilter(smartteefilter,L"smart");
   

            AM_MEDIA_TYPE *  fmt;
            hr = pin-> QueryInterface(IID_IAMStreamConfig,(void **)& streamconfig);
           
            VIDEO_STREAM_CONFIG_CAPS上限;
            int sz,  max_res;
            hr = streamconfig-> GetNumberOfCapabilities(& max_res,& sz);


            if(str == L"WCSA400 Video 0&")
            {
                hr = streamconfig-> GetStreamCaps(2,& fmt,(BYTE *)& caps);
            }
           否则if(str == L"Logitech QuickCam Easy/Cool")
            {
                hr = streamconfig-> GetStreamCaps(4,& fmt,(BYTE *)& caps);
            }
            hr = streamconfig-> SetFormat(fmt);

    if(str =="WCSA400视频0")
    {
        if(pintyp == L捕获")
        {
       
            hr = CoCreateInstance(CLSID_SampleGrabber,NULL,CLSCTX_INPROC,IID_ISampleGrabber,(void **)& SG);
            CComQIPtr< IBaseFilter,& IID_IBaseFilter> PGrabase(SG);
            hr = graphbuilder-> AddFilter(PGrabase,L"SAMPLE");
   
            CComPtr< IBaseFilter> pRender;
            hr = CoCreateInstance(CLSID_NullRenderer,NULL,CLSCTX_INPROC,IID_IBaseFilter,(void **)& pRender);
            hr = graphbuilder-> AddFilter(pRender,L"NULL");

               
            hr = GetPin(basefilter,PINDIR_OUTPUT,0,& base_capturepin);
            hr = GetPin(PGrabase,PINDIR_INPUT,0,& PgrabIn);

            hr = graphbuilder-> Connect(base_capturepin,PgrabIn);

            hr = GetPin(PGrabase,PINDIR_OUTPUT,1,& PgrabOut);
            hr = GetPin(pRender,PINDIR_INPUT,0,& PRenderIn);

            AM_MEDIA_TYPE mt;
            hr = SG-> GetConnectedMediaType(& mt);                    //设置抓取程序的媒体类型以生成流.
  
                    VIDEOINFOHEADER * vih =(VIDEOINFOHEADER *)mt.pbFormat;    //设置宽度& BMP文件缓冲区的高度.
                    Grp_Obj.lWidth  = vih-> bmiHeader.biWidth;
                    Grp_Obj.lHeight = vih-> bmiHeader.biHeight;


                    hr = SG-> SetBufferSamples(FALSE);

                    hr = SG-> SetOneShot(FALSE);

                    hr = SG-> SetCallback(& Grp_Obj,1);


        }
       否则if(pintyp == L预览")
        {
            hr = capturebuilder-> RenderStream(& PIN_CATEGORY_PREVIEW,& MEDIATYPE_Video,basefilter,NULL,NULL);
        }
    }
devenum-> Release();
        basefilter-> Release();
        enummoniker-> Release();
        mon-> Release();   
        bag-> Release();
   
   

    hr = videowindow-> put_Owner((OAHWND)hwnd);
    hr = videowindow-> put_WindowStyle(WS_CLIPCHILDREN);
    hr = videowindow-> put_Visible(OATRUE);

    RECT记录;
    GetClientRect(hwnd,& rec);
    videowindow-> SetWindowPosition(0,0,rec.right,rec.bottom);
    hr = mediacontrol-> Run();



GetPin(IBaseFilter * pFilter,需要输入PIN_DIRECTION,int iNum,IPin ** ppPin)
{
    CComPtr< IEnumPins> pEnum;
    * ppPin = NULL;
    HRESULT hr = pFilter-> EnumPins(& pEnum);
    if(FAILED(hr))
      返回hr;

    ULONG ulFound;
    IPin * pPin;
    hr = E_FAIL;

    while(S_OK == pEnum-> Next(1,& pPin,& ulFound))
    {
       PIN_DIRECTION pindir =(PIN_DIRECTION)3;
       pPin-> QueryDirection(& pindir);
       if(pindir ==必填)
       {
           if(iNum == 0)
           {
                             * ppPin = pPin;
                             //找到了请求的引脚,所以清除错误
                             hr = S_OK;
                            休息;
           }
           iNum--;
       }

       pPin-> Release();
    }

   返回hr;






IPin* smart_outcappin;
    IPin* base_capturepin;
    IPin* base_previewpin;
    IPin* smart_inpin;
    IPin* smart_outprewpin;
    IPin * b_cfillOutc;
    IPin * PgrabIn;
    IPin * PgrabOut;
    IPin * PRenderIn;
    IPin *pin;
           
    HWND hwnd;
    hwnd=(HWND)hand;

    hr=CoCreateInstance(CLSID_CaptureGraphBuilder2,NULL,CLSCTX_INPROC,IID_ICaptureGraphBuilder2,(void**)&capturebuilder);
    hr=CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC,IID_IGraphBuilder,(void**)&graphbuilder);
    hr=graphbuilder->QueryInterface(IID_IVideoWindow,(void**)&videowindow);
    hr=graphbuilder->QueryInterface(IID_IMediaControl,(void**)&mediacontrol);

    hr=CoCreateInstance(CLSID_SystemDeviceEnum,NULL,CLSCTX_INPROC,IID_ICreateDevEnum,(void**)&devenum);
    hr=devenum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,&enummoniker,NULL);
    hr=capturebuilder->SetFiltergraph(graphbuilder);

    hr=CoCreateInstance(CLSID_SmartTee,NULL,CLSCTX_INPROC,IID_IBaseFilter,(void**)&smartteefilter);

   

    VARIANT var;
    CComBSTR str=NULL;
    while(enummoniker->Next(1,&mon,&l)==S_OK)
    {
        VariantInit(&var);
        mon->BindToStorage(0,0,IID_IPropertyBag,(void**)&bag);
        bag->Read(L"Friendlyname",&var,0);
        str=SysAllocString(var.bstrVal);
        if(str==devicename)
        {
            hr=mon->BindToObject(0,0,IID_IBaseFilter,(void**)&basefilter);
            break;
        }
    }
    CComPtr < IEnumPins > enumpin;
    hr=basefilter->EnumPins(&enumpin);
    ULONG ul;


    while(enumpin->Next(1,&pin,&ul)==S_OK)
    {
        PIN_INFO pininfo;
        pindir=(PIN_DIRECTION)2;
        pin->QueryDirection(&pindir);
        pin->QueryPinInfo(&pininfo);
        pintyp=SysAllocString(pininfo.achName);
            if(pintyp==pintype)
            {
                break;
            }
            else
            {
              continue;
            }
    }
   
    hr=graphbuilder->AddFilter(basefilter,L"videoname");
    hr=graphbuilder->AddFilter(smartteefilter,L"smart");
   

            AM_MEDIA_TYPE*  fmt;
            hr=pin->QueryInterface(IID_IAMStreamConfig,(void**)&streamconfig);
           
            VIDEO_STREAM_CONFIG_CAPS caps;
            int sz,  max_res;
            hr=streamconfig->GetNumberOfCapabilities(&max_res,&sz);


            if(str==L"WCSA400 Video 0")
            {
                hr =streamconfig->GetStreamCaps(2,&fmt,(BYTE*)&caps);
            }
            else if(str==L"Logitech QuickCam Easy/Cool")
            {
                hr =streamconfig->GetStreamCaps(4,&fmt,(BYTE*)&caps);
            }
            hr=streamconfig->SetFormat(fmt);

    if(str=="WCSA400 Video 0")
    {
        if(pintyp==L"Capture")
        {
       
            hr=CoCreateInstance(CLSID_SampleGrabber,NULL,CLSCTX_INPROC,IID_ISampleGrabber,(void**)&SG);
            CComQIPtr<IBaseFilter,&IID_IBaseFilter>PGrabase(SG);
            hr=graphbuilder->AddFilter(PGrabase,L"SAMPLE");
   
            CComPtr<IBaseFilter> pRender;
            hr=CoCreateInstance(CLSID_NullRenderer,NULL,CLSCTX_INPROC,IID_IBaseFilter,(void**)&pRender);
            hr=graphbuilder->AddFilter(pRender,L"NULL");

               
            hr=GetPin(basefilter,PINDIR_OUTPUT,0,&base_capturepin);
            hr=GetPin(PGrabase,PINDIR_INPUT,0,&PgrabIn);

            hr=graphbuilder->Connect(base_capturepin,PgrabIn);

            hr=GetPin(PGrabase,PINDIR_OUTPUT,1,&PgrabOut);
            hr=GetPin(pRender,PINDIR_INPUT,0,&PRenderIn);

            AM_MEDIA_TYPE mt;
            hr = SG->GetConnectedMediaType(&mt);                    //Set mediatype for grabber to make stream.
  
                    VIDEOINFOHEADER * vih = (VIDEOINFOHEADER*) mt.pbFormat;    //Set width & Height of BMP File buffer.
                    Grp_Obj.lWidth  = vih->bmiHeader.biWidth;
                    Grp_Obj.lHeight = vih->bmiHeader.biHeight;


                    hr = SG->SetBufferSamples(FALSE);

                    hr = SG->SetOneShot(FALSE);

                    hr = SG->SetCallback(&Grp_Obj,1);


        }
        else if(pintyp==L"Preview")
        {
            hr=capturebuilder->RenderStream(&PIN_CATEGORY_PREVIEW,&MEDIATYPE_Video,basefilter,NULL,NULL);
        }
    }
devenum->Release();
        basefilter->Release();
        enummoniker->Release();
        mon->Release();   
        bag->Release();
   
   

    hr=videowindow->put_Owner((OAHWND)hwnd);
    hr=videowindow->put_WindowStyle(WS_CLIPCHILDREN);
    hr=videowindow->put_Visible(OATRUE);

    RECT rec;
    GetClientRect(hwnd,&rec);
    videowindow->SetWindowPosition(0,0,rec.right,rec.bottom);
    hr = mediacontrol->Run();



GetPin(IBaseFilter * pFilter,PIN_DIRECTION dirrequired,int iNum, IPin **ppPin)
{
    CComPtr< IEnumPins > pEnum;
    *ppPin = NULL;
    HRESULT hr = pFilter->EnumPins(&pEnum);
    if(FAILED(hr))
        return hr;

    ULONG ulFound;
    IPin *pPin;
    hr = E_FAIL;

    while(S_OK == pEnum->Next(1, &pPin, &ulFound))
    {
        PIN_DIRECTION pindir = (PIN_DIRECTION)3;
        pPin->QueryDirection(&pindir);
        if(pindir == dirrequired)
        {
            if(iNum == 0)
            {
                *ppPin = pPin;
                // Found requested pin, so clear error
                hr = S_OK;
                break;
            }
            iNum--;
        }

        pPin->Release();
    }

    return hr;







这篇关于一起捕获视频和图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:03