I have a problem in my coding, any body help me what my mistake.
I want capture the image from live webcam video. I am using directx9
in vc++.
I am using RenderFile() method it is work fine but not work in
RenderStrem().
please help me..
this is my code
//global declarations
ICreateDevEnum *pSysDevEnum ;
IBaseFilter *pFilter;
IPropertyBag *pPropBag;
IMoniker *pMoniker ;
IEnumMoniker *pEnumCat ;
IMediaControl *pControl;
IMediaEvent *pEvent;
IVideoWindow *pVideo;
IBasicVideo *pBasic;
IBaseFilter *pGrabberF;
ISampleGrabber *pGrabber;
ISampleGrabber *pGrabber1;
AM_MEDIA_TYPE mt;
VIDEOINFOHEADER *pVih;
//button click code
void CstillcaptureDlg::OnBnClickedButton2()
{
// TODO: Add your control notification handler code here
CString strName;
GetDlgItemText(IDC_COMBO1,strName);
//find the particular type CAM and view the video
BOOL bFlag=false;
ICaptureGraphBuilder2 *pBuild=NULL;
IGraphBuilder *pGraph=NULL;
//create instance of capture builder graph
HRESULT hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL,
CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2,
(void**)&pBuild);
//creat instance of Graph Builder
hr = CoCreateInstance(CLSID_FilterGraph, 0, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void**)&pGraph);
// Initialize the Capture Graph Builder.
pBuild->SetFiltergraph(pGraph);
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL,
CLSCTX_INPROC_SERVER,
IID_ICreateDevEnum, (void **)&pSysDevEnum);
// Obtain a class enumerator for the video Input device category.
hr = pSysDevEnum-
>CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnumCat, 0);
if(pEnumCat==NULL)
AfxMessageBox("No camera device found");
if (hr == S_OK)
{
// Enumerate the monikers.
ULONG cFetched;
while(pEnumCat->Next(1, &pMoniker, &cFetched)==S_OK)
{
bFlag=true;
hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag,
(void **)&pPropBag);
if (SUCCEEDED(hr))
{
// To retrieve the filter's friendly name, do the
following:
VARIANT varName;
VariantInit(&varName);
TCHAR devName[256];
hr = pPropBag->Read(L"FriendlyName", &varName, 0);
if (SUCCEEDED(hr))
{
// Display the name of the input device.
WideCharToMultiByte(CP_ACP, 0,
varName.bstrVal, -1, devName, sizeof(devName), 0, 0);
if(devName==strName)
{
hr = pMoniker->BindToObject(NULL, NULL, IID_IBaseFilter,
(void**)&pFilter);
break;
}
}
VariantClear(&varName);
}
}
if(pFilter!=NULL)
{
hr = CoCreateInstance(CLSID_SampleGrabber, NULL,
CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (void**)&pGrabberF);
pGrabberF->QueryInterface(IID_ISampleGrabber,
(void**)&pGrabber);
ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
mt.majortype = MEDIATYPE_Video;
mt.subtype = MEDIASUBTYPE_RGB24;
mt.formattype=FORMAT_VideoInfo;
hr = pGrabber->SetMediaType(&mt);
hr = pGraph->AddFilter(pGrabberF, L"Sample Grabber");
hr=pGraph->QueryInterface(IID_IMediaControl,(LPVOID *)&pControl);
hr=pGraph->AddFilter(pFilter,L"Video Capture");
//hr=pBuild->SetFiltergraph(pGraph);
hr = pBuild->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
pFilter, NULL,NULL);
hr=pGrabber->GetConnectedMediaType(&mt);
pVih=(VIDEOINFOHEADER *)mt.pbFormat;
hr=pGrabber->SetBufferSamples(TRUE);
hr=pControl->Run();
}
pPropBag->Release();
}
}
void CstillcaptureDlg::OnBnClickedButton3()
{
// TODO: Add your control notification handler code here
long nBufferSize = mt.lSampleSize;
HRESULT h=pGrabber->GetCurrentBuffer(&nBufferSize,NULL);
long *pBuffer = (long *)malloc(nBufferSize);
// grab image data.
HRESULT hr=pGrabber->GetCurrentBuffer(&nBufferSize,
pBuffer);
// Save image data as Bitmap.
// This is just to make this sample easily understandable.
//
HANDLE fh;
BITMAPFILEHEADER bmphdr;
DWORD nWritten;
memset(&bmphdr, 0, sizeof(bmphdr));
bmphdr.bfType = ('M' << 8) | 'B';
bmphdr.bfSize = sizeof(bmphdr) + sizeof(BITMAPINFOHEADER) +
nBufferSize;
bmphdr.bfOffBits = sizeof(bmphdr) + sizeof(BITMAPINFOHEADER);
CString strName;
static int i=0;
strName.Format("d:\\images\\result%d.bmp",i);
i++;
fh = CreateFile(strName,
GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
WriteFile(fh, &bmphdr, sizeof(bmphdr), &nWritten, NULL);
WriteFile(fh,
&pVih->bmiHeader,
sizeof(BITMAPINFOHEADER), &nWritten, NULL);
WriteFile(fh, pBuffer, nBufferSize, &nWritten, NULL);
CloseHandle(fh);
free(pBuffer);
}
Thanks and Regards
mani