非常感谢您的及时回复。我发布了用于使用TakePhoto()方法捕获图像的示例代码。此方法返回成功。 OnCaptureEvent()用于处理来自捕获引擎的事件及其返回的MF_CAPTURE_ENGINE_ERROR错误。
HRESULT TakePhoto()
{
HRESULT hr = m_pEngine->GetSink(MF_CAPTURE_ENGINE_SINK_TYPE_PHOTO, &pSink);
if (FAILED(hr))
{
goto done;
}
hr = pSink->QueryInterface(IID_PPV_ARGS(&pPhoto));
if (FAILED(hr))
{
goto done;
}
hr = m_pEngine->GetSource(&pSource);
if (FAILED(hr))
{
goto done;
}
hr = pSource->GetCurrentDeviceMediaType(1, &pMediaType); // 1 is Image stream index.I will get current image stream media type here.
if (FAILED(hr))
{
goto done;
}
//Configure the photo format
hr = CreatePhotoMediaType(pMediaType, &pMediaType2,GUID_ContainerFormatBmp);
if (FAILED(hr))
{
goto done;
}
hr = pPhoto->RemoveAllStreams();
if (FAILED(hr))
{
goto done;
}
DWORD dwSinkStreamIndex;
// Try to connect the first still image stream to the photo sink
if(bHasPhotoStream)
{
hr = pPhoto->AddStream((DWORD)MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_PHOTO, pMediaType2, NULL, &dwSinkStreamIndex); //Instead of MF_CAPTURE_ENGINE_PREFERRED_SOURCE_STREAM_FOR_PHOTO,i gave index as 1.i am getting error
if(FAILED(hr))
{
goto done;
}
}
hr = pPhoto->SetOutputFileName(pszFileName);
if (FAILED(hr))
{
goto done;
}
hr = m_pEngine->TakePhoto();
if (FAILED(hr))
{
goto done;
}
return hr;
}
HRESULT OnCaptureEvent(WPARAM wParam, LPARAM lParam)
{
GUID guidType;
HRESULT hrStatus;
IMFMediaEvent *pEvent = reinterpret_cast<IMFMediaEvent*>(wParam);
hr = pEvent->GetExtendedType(&guidType);
if (SUCCEEDED(hr))
{
if (guidType == MF_CAPTURE_ENGINE_ERROR) //i got this error if i give dwSourceStreamIndex as '1' in Addstream api
{
DestroyCaptureEngine();
}
}
pEvent->Release();
return hrStatus;
}