DirectShow CameraControl

#define _CRT_SECURE_NO_DEPRECATE
#include "../common/dshow.h" 
#include "../common/FilterEnum.h" // voir aussi FilterEnum.cpp
#include "../common/StreamUtil.h" // voir aussi StreamUtil.cpp
#include "../common/GraphUtil.h" // voir aussi GraphUtil.cpp
#include 

    HRESULT hr;
    ICaptureGraphBuilder2 *pCaptureGraph = NULL;
    IGraphBuilder         *pGraph = NULL;
    IMediaControl         *pControl = NULL;
    IFileSinkFilter       *pSink = NULL;
    IBaseFilter           *pCameraFilter = NULL;
    IBaseFilter           *pAVIWriter = NULL;
    IBaseFilter           *pVideoCompressionFilter = NULL;
    IAMCameraControl      *pCameraControl = NULL;
    IAMVfwCompressDialogs *pCompressDialogsVideo = NULL;

void ReleaseAll()
{
    if ( pCaptureGraph != NULL ) { pCaptureGraph->Release(); pCaptureGraph = NULL; }
    if ( pGraph != NULL ) { pGraph->Release(); pGraph = NULL; }
    if ( pControl != NULL ) { pControl->Release(); pControl = NULL; }
    if ( pSink != NULL ) { pSink->Release(); pSink = NULL; }
    if ( pCameraFilter != NULL ) { pCameraFilter->Release(); pCameraFilter = NULL; }
    if ( pAVIWriter != NULL ) { pAVIWriter->Release(); pAVIWriter = NULL; }
    if ( pVideoCompressionFilter != NULL ) { pVideoCompressionFilter->Release(); pVideoCompressionFilter = NULL; }
    if ( pCameraControl != NULL ) { pCameraControl->Release(); pCameraControl = NULL; }
    if ( pCompressDialogsVideo != NULL ) { pCompressDialogsVideo->Release(); pCompressDialogsVideo = NULL; }
    CoUninitialize();
}

HRESULT ConfigureCamera(IAMCameraControl *pCameraControl)
{
    HRESULT hr;
    long Min, Max, Step, Default, Flags, Val;

    printf("\nEntrez les paramètres : min - max ; pas [defaut](courant)\n\n");

    // On récupère la plage de valeurs et la valeur par défaut
    hr = pCameraControl->GetRange(CameraControl_Pan,&Min,&Max,&Step,&Default,&Flags);
    if (SUCCEEDED(hr))
    {
        // On récupère la valeur courante
        hr = pCameraControl->Get(CameraControl_Pan, &Val, &Flags);
        printf("Pan : %d - %d ; %d [%d](%d) ? ",Min,Max,Step,Default,Val);
        scanf("%d",&Val); getchar();
        if ( Val < Min && Val > Max ) Val = Default;
        pCameraControl->Set(CameraControl_Pan,Val,Flags);
    }
    else
        printf("Configuration du pan non supportee.\n");

    hr = pCameraControl->GetRange(CameraControl_Tilt,&Min,&Max,&Step,&Default,&Flags);
    if (SUCCEEDED(hr))
    {
        hr = pCameraControl->Get(CameraControl_Tilt, &Val, &Flags);
        printf("Tilt : %d - %d ; %d [%d](%d) ? ",Min,Max,Step,Default,Val);
        scanf("%d",&Val); getchar();
        if ( Val < Min && Val > Max ) Val = Default;
        pCameraControl->Set(CameraControl_Tilt,Val,Flags);
    }
    else
        printf("Configuration du tilt non supportee.\n");

    hr = pCameraControl->GetRange(CameraControl_Roll,&Min,&Max,&Step,&Default,&Flags);
    if (SUCCEEDED(hr))
    {
        hr = pCameraControl->Get(CameraControl_Roll, &Val, &Flags);
        printf("Roll : %d - %d ; %d [%d](%d) ? ",Min,Max,Step,Default,Val);
        scanf("%d",&Val); getchar();
        if ( Val < Min && Val > Max ) Val = Default;
        pCameraControl->Set(CameraControl_Roll,Val,Flags);
    }
    else
        printf("Configuration du roll non supportee.\n");

    hr = pCameraControl->GetRange(CameraControl_Zoom,&Min,&Max,&Step,&Default,&Flags);
    if (SUCCEEDED(hr))
    {
        hr = pCameraControl->Get(CameraControl_Zoom, &Val, &Flags);
        printf("Zoom : %d - %d ; %d [%d](%d) ? ",Min,Max,Step,Default,Val);
        scanf("%d",&Val); getchar();
        if ( Val < Min && Val > Max ) Val = Default;
        pCameraControl->Set(CameraControl_Zoom,Val,Flags);
    }
    else
        printf("Configuration du zoom non supportee.\n");

    hr = pCameraControl->GetRange(CameraControl_Exposure,&Min,&Max,&Step,&Default,&Flags);
    if (SUCCEEDED(hr))
    {
        hr = pCameraControl->Get(CameraControl_Exposure, &Val, &Flags);
        printf("Exposure : %d - %d ; %d [%d](%d) ? ",Min,Max,Step,Default,Val);
        scanf("%d",&Val); getchar();
        if ( Val < Min && Val > Max ) Val = Default;
        pCameraControl->Set(CameraControl_Exposure,Val,Flags);
    }
    else
        printf("Configuration de l'exposure non supportee.\n");

    hr = pCameraControl->GetRange(CameraControl_Iris,&Min,&Max,&Step,&Default,&Flags);
    if (SUCCEEDED(hr))
    {
        hr = pCameraControl->Get(CameraControl_Iris, &Val, &Flags);
        printf("Iris : %d - %d ; %d [%d](%d) ? ",Min,Max,Step,Default,Val);
        scanf("%d",&Val); getchar();
        if ( Val < Min && Val > Max ) Val = Default;
        pCameraControl->Set(CameraControl_Iris,Val,Flags);
    }
    else
        printf("Configuration de l'iris non supportee.\n");

    hr = pCameraControl->GetRange(CameraControl_Focus,&Min,&Max,&Step,&Default,&Flags);
    if (SUCCEEDED(hr))
    {
        hr = pCameraControl->Get(CameraControl_Focus, &Val, &Flags);
        printf("Focus : %d - %d ; %d [%d](%d) ? ",Min,Max,Step,Default,Val);
        scanf("%d",&Val); getchar();
        if ( Val < Min && Val > Max ) Val = Default;
        pCameraControl->Set(CameraControl_Focus,Val,Flags);
    }
    else
        printf("Configuration du focus non supportee.\n");

    return hr;
}

int main()
{
    hr = CoInitialize(NULL);
    if ( hr != S_OK ) ReleaseAll();

    hr = CoCreateInstance(CLSID_CaptureGraphBuilder2,NULL,CLSCTX_INPROC_SERVER,IID_ICaptureGraphBuilder2,(void **)&pCaptureGraph);
    if ( hr != S_OK ) { ReleaseAll(); return hr; }

    hr = pCaptureGraph->SetOutputFileName(&MEDIASUBTYPE_Avi,L"MyWebcam.avi",&pAVIWriter,&pSink);
    hr = pCaptureGraph->GetFiltergraph(&pGraph);
    hr = pGraph->QueryInterface(IID_IMediaControl,(void **)&pControl);
    if ( hr != S_OK ) { ReleaseAll(); return hr; }

    printf("\n\tPeripheriques video :\n\n");
    hr = GetFilter(CLSID_VideoInputDeviceCategory,&pCameraFilter);
    if ( hr == S_OK )   hr = pGraph->AddFilter(pCameraFilter, L"Webcam Video Capture");
    else { ReleaseAll(); return hr; }

    hr = pCameraFilter->QueryInterface(IID_IAMCameraControl,(void**)&pCameraControl);

    if ( SUCCEEDED(hr) )
    {
        printf("ok\n");
        hr = ConfigureCamera(pCameraControl);
    }
    else if ( hr == E_NOINTERFACE )
    {
        printf("\nInterface de configuration non disponible\n");
        ReleaseAll();
        return hr;
    }

    printf("\n\tCodecs video :\n\n");
    hr = GetFilter(CLSID_VideoCompressorCategory,&pVideoCompressionFilter);
    if ( hr == S_OK )   hr = pGraph->AddFilter(pVideoCompressionFilter, L"Video Compression Filter");
    else { ReleaseAll(); return hr; }

    hr = pVideoCompressionFilter->QueryInterface(IID_IAMVfwCompressDialogs,(void**)&pCompressDialogsVideo);
    if ( SUCCEEDED(hr) )
    {
//      hr =  pCompressDialogsVideo->ShowDialog(VfwCompressDialog_Config,0);
    }

    hr = pCaptureGraph->RenderStream(&PIN_CATEGORY_PREVIEW,&MEDIATYPE_Interleaved,pCameraFilter,NULL,NULL);
    hr = pCaptureGraph->RenderStream(&PIN_CATEGORY_CAPTURE,&MEDIATYPE_Interleaved,pCameraFilter,pVideoCompressionFilter,pAVIWriter);

    hr = SaveGraphFile(pGraph, L"MyGraph.grf");

    if ( SUCCEEDED(hr) )
    {
        hr = pControl->Run();
        if ( SUCCEEDED(hr) )
        {
            printf("\n\nEnregistrement en cours... Pressez ENTREE pour arreter.\n");
            getchar();
        }
        hr = pControl->Stop();
        printf("Enregistrement termine.\n");
    }

    ReleaseAll();

    return 0;
}

你可能感兴趣的:(C++)