导航

DirectX中摄像头分辨率的设定

Posted on 2005-01-13 15:29  bullfinch  阅读(5169)  评论(0编辑  收藏  举报

F项目进行到最终界面设计阶段了。在把320x240分辨率的摄像头图像放到640x480的视窗之后,解析度很低,决定添加让用户选择分辨率的界面。

先看了DirectX自带的AmCap Sample,在里面找到这样一段代码:

                IAMStreamConfig *pSC;
                hr 
= gcap.pBuilder->FindInterface(&PIN_CATEGORY_CAPTURE,
                    
&MEDIATYPE_Interleaved, gcap.pVCap,
                    IID_IAMStreamConfig, (
void **)&pSC);

                
if(hr != NOERROR)
                    hr 
= gcap.pBuilder->FindInterface(&PIN_CATEGORY_CAPTURE,
                        
&MEDIATYPE_Video, gcap.pVCap,
                        IID_IAMStreamConfig, (
void **)&pSC);

                ISpecifyPropertyPages 
*pSpec;
                CAUUID cauuid;

                hr 
= pSC->QueryInterface(IID_ISpecifyPropertyPages,
                    (
void **)&pSpec);

                
if(hr == S_OK)
                
{
                    hr 
= pSpec->GetPages(&cauuid);
                    hr 
= OleCreatePropertyFrame(ghwndApp, 3030, NULL, 1,
                        (IUnknown 
**)&pSC, cauuid.cElems,
                        (GUID 
*)cauuid.pElems, 00, NULL);

                    
// !!! What if changing output formats couldn't reconnect
                    
// and the graph is broken?  Shouldn't be possible

                    
if(gcap.pVSC)
                    
{
                        AM_MEDIA_TYPE 
*pmt;
                        
// get format being used NOW
                        hr = gcap.pVSC->GetFormat(&pmt);

                        
// DV capture does not use a VIDEOINFOHEADER
                        if(hr == NOERROR)
                        
{
                            
if(pmt->formattype == FORMAT_VideoInfo)
                            
{
                                
// resize our window to the new capture size
                                ResizeWindow(HEADER(pmt->pbFormat)->biWidth,
                                    abs(HEADER(pmt
->pbFormat)->biHeight));
                            }

                            DeleteMediaType(pmt);
                        }

                    }


                    CoTaskMemFree(cauuid.pElems);
                    pSpec
->Release();
                }


                pSC
->Release();

先是用ICaptureGraphBuilder2::FindInterface将Capture Filter的属性读到IAMStreamConfig中,再通过QueryInterface将IAMStreamConfig与ISpecifyPropertyPages关联起来,然后利用OLE自带的OleCreatePropertyFrame来生成属性页面,供用户选取。
后面的代码是根据读到的信息调整UI,并作些结束处理。

以上实地一种方法,但是每次都要选择才能达到640x480的分辨率,感觉很不人性化,假如用户希望定义默认值为640x480而不是320x240,那就需要别的方法了。
在DirectX的文档中看到这样一篇文章:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/anch_directx.asp

先把Capture Filter的属性读到IAMStreamConfig:
IAMStreamConfig *pConfig = NULL;
hr 
= pBuild->FindInterface(
    
&PIN_CATEGORY_PREVIEW, // Preview pin.
    0,    // Any media type.
    pCap, // Pointer to the capture filter.
    IID_IAMStreamConfig, (void**)&pConfig);

然后将属性中的每个AM_MEDIA_TYPE读出,处理。
int iCount = 0, iSize = 0;
hr 
= pConfig->GetNumberOfCapabilities(&iCount, &iSize);

// Check the size to make sure we pass in the correct structure.
if (iSize == sizeof(VIDEO_STREAM_CONFIG_CAPS)
{
    
// Use the video capabilities structure.

    
for (int iFormat = 0; iFormat < iCount; iFormat++)
    
{
        VIDEO_STREAM_CONFIG_CAPS scc;
        AM_MEDIA_TYPE 
*pmtConfig;
        hr 
= pConfig->GetStreamCaps(iFormat, &pmtConfig, (BYTE*)&scc);
        
if (SUCCEEDED(hr))
        
{
            
/* Examine the format, and possibly use it. */

            
// Delete the media type when you are done.
            DeleteMediaType(pmtConfig);
        }

}

在这里我要做的是,在众多支持的AM_MEDIA_TYPE中挑选一种合适的,作为默认选项。
                if ((pmtConfig->majortype == MEDIATYPE_Video) &&
                    (pmtConfig
->subtype == MEDIASUBTYPE_RGB24) &&
                    (pmtConfig
->formattype == FORMAT_VideoInfo) &&
                    (pmtConfig
->cbFormat >= sizeof (VIDEOINFOHEADER)) &&
                    (pmtConfig
->pbFormat != NULL))
                
{
                    VIDEOINFOHEADER 
*pVih = (VIDEOINFOHEADER*)pmtConfig->pbFormat;
                    
// pVih contains the detailed format information.
                    LONG lWidth = pVih->bmiHeader.biWidth;
                    LONG lHeight 
= pVih->bmiHeader.biHeight;
                    
if(lWidth == 640)
                        pSC
->SetFormat(pmtConfig);
                }
以上代码放在/* Examine the format, and possibly use it. */后。

这样,初始模式就设在了640x480上,但是如果设备不支持640,就不会更改了。

第二种方法也成功了。