locked
Change Webcam Resolution RRS feed

  • Question

  •  

    Hi everybodi!!

    I'm writing an application that control an usb 2.0 webcam.

    I' want to change the webcam resolution from the standard 640x480 to another format supported by the cam like 1280x1024 "programmatically", without using the pinPropertyDialog.

    Someone can help me?

    THX a lot!!!

    Raffaele.

    Thursday, April 3, 2008 10:36 AM

Answers

  • Here's a function that might help you.

     

    Code Snippet

    ///
    /// Set video capture parameters.
    ///
    /// @param Pin Output pin of video source.
    /// @param Width Width of video, or -1 to ignore.
    /// @param Height Height of video, or -1 to ignore.
    /// @param FPS Frames per second, or -1.0 to ignore.
    /// @param SubType Media subtype, or GUID_NULL to ignore.
    ///
    /// @return true if successful.

    bool
    SetVideoParams(IPin *Pin, int Width, int Height, double FPS,
           const GUID &SubType)
    {
       assert(Pin);
       assert(FPS != 0);

       HRESULT hr;

     

       CComPtr<IAMStreamConfig> strcfg;
       if (FAILED(Pin->QueryInterface(&strcfg)))
          return false;

     

       AM_MEDIA_TYPE *pmt = 0;
       if (FAILED(strcfg->GetFormat(&pmt)))
          return false;

     

       if (SubType != GUID_NULL)
          pmt->subtype = SubType;

     

       assert(pmt->formattype == FORMAT_VideoInfo);

       VIDEOINFOHEADER *pvi = (VIDEOINFOHEADER *)pmt->pbFormat;

     

       if (FPS != -1.0)
          pvi->AvgTimePerFrame = (LONGLONG)(10000000/FPS);
       if (Width != -1)
          pvi->bmiHeader.biWidth = Width;
       if (Height != -1)
          pvi->bmiHeader.biHeight = Height;

     

       hr = strcfg->SetFormat(pmt);

       DeleteMediaType(pmt);
       return SUCCEEDED(hr);
    }

     

     

    • Marked as answer by The March Hare Friday, December 11, 2009 3:59 AM
    Tuesday, April 8, 2008 2:34 PM

All replies

  • Have a look at using the IAMStreamConfig interface http://msdn2.microsoft.com/en-us/library/ms784114(VS.85).aspx
    Thursday, April 3, 2008 1:40 PM
  • THX for the reply Ralf!!!

     

    I'll look the IAMStreamConfig method and post the result.

    Friday, April 4, 2008 7:59 AM
  • Good stuff Wink

    From what I remember, you'll have to enumerate all the possible media types of the source using the interface and then call SetFormat on the interface with the media type you want.

    Good luck!


    Friday, April 4, 2008 9:27 AM
  • It' doesn't work. Probably i don't understand how it work.

    Here is my code!!! Help me please!!!

     

     

    HRESULT FindCaptureDevice(IBaseFilter ** ppSrcFilter)

    {

    HRESULT hr = S_OK;

    IBaseFilter * pSrc = NULL;

    IMoniker* pMoniker =NULL;

    ICreateDevEnum *pDevEnum =NULL;

    IEnumMoniker *pClassEnum = NULL;

    if (!ppSrcFilter)

    {

    return E_POINTER;

    }

    // Create the system device enumerator

    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);

    }

    // Create an enumerator for the video capture devices

    if (SUCCEEDED(hr))

    {

    hr = pDevEnum->CreateClassEnumerator (CLSID_VideoInputDeviceCategory, &pClassEnum, 0);

    if (FAILED(hr))

    {

    Msg(TEXT("Couldn't create class enumerator! hr=0x%x"), hr);

    }

    }

    if (SUCCEEDED(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);

    hr = 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 (SUCCEEDED(hr))

    {

    hr = pClassEnum->Next (1, &pMoniker, NULL);

    if (hr == S_FALSE)

    {

    Msg(TEXT("Unable to access video capture device!"));

    hr = E_FAIL;

    }

    }

    if (SUCCEEDED(hr))

    {

    // 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);

    }

    }

    // Copy the found filter pointer to the output parameter.

    if (SUCCEEDED(hr))

    {

    *ppSrcFilter = pSrc;

    (*ppSrcFilter)->AddRef();

    /************************************************************** My Test Interface *************************************************************/

    // Find the interface for IAMStreamConfig

    hr = g_pCapture->FindInterface(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,

    *ppSrcFilter, IID_IAMStreamConfig, (void **)&pConfig);

    /********************************************************************* End ********************************************************************/

    }

    SAFE_RELEASE(pSrc);

    SAFE_RELEASE(pMoniker);

    SAFE_RELEASE(pDevEnum);

    SAFE_RELEASE(pClassEnum);

    return hr;

    }

     

    int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hInstP, LPSTR lpCmdLine, int nCmdShow)

    {

    MSG msg={0};

    WNDCLASS wc;

    // Initialize COM

    if(FAILED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)))

    {

    Msg(TEXT("CoInitialize Failed!\r\n"));

    exit(1);

    }

    // Register the window class

    ZeroMemory(&wc, sizeof wc);

    wc.lpfnWndProc = WndMainProc;

    wc.hInstance = hInstance;

    wc.lpszClassName = CLASSNAME;

    wc.lpszMenuName = NULL;

    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);

    wc.hCursor = LoadCursor(NULL, IDC_ARROW);

    wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_VIDPREVIEW));

    if(!RegisterClass(&wc))

    {

    Msg(TEXT("RegisterClass Failed! Error=0x%x\r\n"), GetLastError());

    CoUninitialize();

    exit(1);

    }

    // Create the main window. The WS_CLIPCHILDREN style is required.

    ghApp = CreateWindow(CLASSNAME, APPLICATIONNAME,

    WS_OVERLAPPEDWINDOW | WS_CAPTION | WS_CLIPCHILDREN,

    CW_USEDEFAULT, CW_USEDEFAULT,

    DEFAULT_VIDEO_WIDTH, DEFAULT_VIDEO_HEIGHT,

    0, 0, hInstance, 0);

    if(ghApp)

    {

    HRESULT hr;

    // Create DirectShow graph and start capturing video

    hr = CaptureVideo();

    /**************************************************** My Test Resolution ***************************************************************/

    int iCount, iSize;

    BYTE *pSCC = NULL;

    AM_MEDIA_TYPE *pmt;

    hr = pConfig->GetNumberOfCapabilities(&iCount, &iSize);

    pSCC = new BYTE[iSize];

    if (pSCC == NULL)

    {

    // TODO: Out of memory error.

    }

    // Get the first format.

    //hr = pConfig->GetStreamCaps(0, &pmt, pSCC);

    hr = pConfig->GetFormat(&pmt);

    if (hr == S_OK)

    {

    // TODO: Examine the format. If it's not suitable for some

    // reason, call GetStreamCaps with the next index value (up

    // to iCount). Otherwise, set the format:

    VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)pmt->pbFormat;

    pVih->bmiHeader.biWidth = 640; //Or any other valid width

    pVih->bmiHeader.biHeight = 480;

    pVih->bmiHeader.biSizeImage = DIBSIZE(pVih->bmiHeader);

    hr = pConfig->SetFormat(pmt);

    if (FAILED(hr))

    {

    // TODO: Error handling.

    }

    //DeleteMediaType(pmt);

    }

    delete [] pSCC;

    /************************************************************** End ********************************************************************/

     

    if (FAILED (hr))

    {

    CloseInterfaces();

    DestroyWindow(ghApp);

    }

    else

    {

    // Don't display the main window until the DirectShow

    // preview graph has been created. Once video data is

    // being received and processed, the window will appear

    // and immediately have useful video data to display.

    // Otherwise, it will be black until video data arrives.

    ShowWindow(ghApp, nCmdShow);

    }

    // Main message loop

    while(GetMessage(&msg,NULL,0,0))

    {

    TranslateMessage(&msg);

    DispatchMessage(&msg);

    }

    }

    // Release COM

    CoUninitialize();

    return (int) msg.wParam;

    }

     

     

    My Code Mod are the two section:

    1) My Interface Test

    2) My Resolution Test

    Monday, April 7, 2008 8:39 AM
  • UP !

    Tuesday, April 8, 2008 9:48 AM
  • Here's a function that might help you.

     

    Code Snippet

    ///
    /// Set video capture parameters.
    ///
    /// @param Pin Output pin of video source.
    /// @param Width Width of video, or -1 to ignore.
    /// @param Height Height of video, or -1 to ignore.
    /// @param FPS Frames per second, or -1.0 to ignore.
    /// @param SubType Media subtype, or GUID_NULL to ignore.
    ///
    /// @return true if successful.

    bool
    SetVideoParams(IPin *Pin, int Width, int Height, double FPS,
           const GUID &SubType)
    {
       assert(Pin);
       assert(FPS != 0);

       HRESULT hr;

     

       CComPtr<IAMStreamConfig> strcfg;
       if (FAILED(Pin->QueryInterface(&strcfg)))
          return false;

     

       AM_MEDIA_TYPE *pmt = 0;
       if (FAILED(strcfg->GetFormat(&pmt)))
          return false;

     

       if (SubType != GUID_NULL)
          pmt->subtype = SubType;

     

       assert(pmt->formattype == FORMAT_VideoInfo);

       VIDEOINFOHEADER *pvi = (VIDEOINFOHEADER *)pmt->pbFormat;

     

       if (FPS != -1.0)
          pvi->AvgTimePerFrame = (LONGLONG)(10000000/FPS);
       if (Width != -1)
          pvi->bmiHeader.biWidth = Width;
       if (Height != -1)
          pvi->bmiHeader.biHeight = Height;

     

       hr = strcfg->SetFormat(pmt);

       DeleteMediaType(pmt);
       return SUCCEEDED(hr);
    }

     

     

    • Marked as answer by The March Hare Friday, December 11, 2009 3:59 AM
    Tuesday, April 8, 2008 2:34 PM
  • THX very Much!!!!!

    It work for me.

    Cheers for Thore!!!

     

    Thursday, April 10, 2008 9:43 AM