感觉Microsoft Source Reader还是比较坑的,只是由于需要,不得不使用。其实按照Microsoft提供的示例,基本上可以正常的调试出程序来。
下面的例子,简单的给出了Source Reader的代码。同时,HRESULT CreateVideoDeviceSource(IMFMediaSource **ppSource)函数中,使用了指针的指针,能正确的传出ppSource,不是NULL。之前调试时,使用HRESULT CreateVideoDeviceSource(IMFMediaSource *ppSource),出来后,ppSource依旧为NULL。后来直接把代码展开,没有继续深入考虑。感谢前两天@wuruifang 的建议,之后在网上搜了一下,指针的指针可以使得输入参数为NULL时输出正常。因而此处重新写成一个函数(以前不清楚为啥用指针的指针时出错,现在倒是没有)。
主要代码:
int _tmain(int argc, _TCHAR* argv[])
{
IMFMediaSource *ppSource = NULL;
CreateVideoDeviceSource(&ppSource); // hr = EnumerateCaptureFormats(ppSource); // This can show the formats the camera support. Notice that output window of visual studio shows the infomation.
// if (FAILED(hr))
// abort();
HRESULT hr;
IMFSourceReader *pReader;
hr = MFCreateSourceReaderFromMediaSource(ppSource, NULL, &pReader);
if (FAILED(hr))
abort(); hr = SetDeviceFormat(ppSource, ); //I need to configure the camera to format 6.
if (FAILED(hr))
abort(); ProcessSamples(pReader); SafeRelease(&pReader);
SafeRelease(&ppSource);
MFShutdown();
CoUninitialize();
} HRESULT ProcessSamples(IMFSourceReader *pReader)
{
HRESULT hr = S_OK;
IMFSample *pSample = NULL;
size_t cSamples = ; _LARGE_INTEGER time_start; /*begin time */
_LARGE_INTEGER time_over; /*end time*/
double dqFreq; /*timer frequence*/
LARGE_INTEGER f; /*timer frequence*/
QueryPerformanceFrequency(&f);
dqFreq = (double)f.QuadPart; QueryPerformanceCounter(&time_start); bool quit = false;
while (!quit)
{
DWORD streamIndex, flags;
LONGLONG llTimeStamp; hr = pReader->ReadSample(
MF_SOURCE_READER_ANY_STREAM, // Stream index.
, // Flags.
&streamIndex, // Receives the actual stream index.
&flags, // Receives status flags.
&llTimeStamp, // Receives the time stamp.
&pSample // Receives the sample or NULL.
); if (FAILED(hr))
break; if (flags & MF_SOURCE_READERF_ENDOFSTREAM)
{
wprintf(L"\tEnd of stream\n");
quit = true;
} if (pSample)
{
BYTE* data;
IMFMediaBuffer* buffer;
DWORD max, current; // printf(" cSamples = %d\n", cSamples);
++cSamples;
pSample->GetBufferByIndex(, &buffer);
buffer->Lock(&data, &max, ¤t); // saveBMP(data, cSamples, IMGWIDTH, IMGHEIGHT); buffer->Unlock();
SafeRelease(&buffer); QueryPerformanceCounter(&time_over); //In order to find the frames per second of the camera.
double usedtime = ((time_over.QuadPart - time_start.QuadPart) / dqFreq);
if (usedtime>)
{
printf(" cSamples = %d\n", cSamples);
cSamples = ;
QueryPerformanceCounter(&time_start);
}
}
SafeRelease(&pSample);
} SafeRelease(&pSample);
return hr;
} HRESULT CreateVideoDeviceSource(IMFMediaSource **ppSource)
{
HRESULT hr;
hr = CoInitialize(NULL);
if (FAILED(hr))
abort();
hr = MFStartup(MF_VERSION, MFSTARTUP_NOSOCKET);
if (FAILED(hr))
abort(); *ppSource = NULL; IMFMediaSource *pSource = NULL;
IMFAttributes *pAttributes = NULL;
IMFActivate **ppDevices = NULL; // Create an attribute store to specify the enumeration parameters.
/*HRESULT*/ hr = MFCreateAttributes(&pAttributes, );
if (FAILED(hr))
abort(); // Source type: video capture devices
hr = pAttributes->SetGUID( MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID );
if (FAILED(hr))
abort(); // Enumerate devices.
UINT32 count;
hr = MFEnumDeviceSources(pAttributes, &ppDevices, &count);
if (FAILED(hr))
abort();
if (count == )
{
hr = E_FAIL;
return hr;
} // Create the media source object.
hr = ppDevices[]->ActivateObject(IID_PPV_ARGS(&pSource));
if (FAILED(hr))
abort(); *ppSource = pSource;
(*ppSource)->AddRef(); // release part
SafeRelease(&pAttributes); for (DWORD i = ; i < count; i++)
{
SafeRelease(&ppDevices[i]);
}
CoTaskMemFree(ppDevices);
SafeRelease(&pSource); //此处不确定,是否需要SafeRelease。
return hr;
}
得到的图片(程序中保存的是BMP图片,由于cnblogs不支持BMP,因而转成jpg):
完整代码(不会插入超链接,抱歉)见:https://github.com/darkknightzh/Microsoft-Source-Reader
参考(其他的记不清了,见谅):
Microsoft :http://msdn.microsoft.com/en-us/library/windows/desktop/dd389281(v=vs.85).aspx
指针的指针(详见4楼,14楼) :http://bbs.csdn.net/topics/210076970