directshow摄像头抓图有bug_错误提示截图
directshow摄像头抓图有bug由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“错误提示截图”。
最近在VC6.0平台下写一个用directshow做一个从免驱动UBS摄像头上抓取图片的程序,预览视频没问题,但抓取图像时老出现内存不能读的问题,抓取图像的代码如下,BOOL CCaptureCla::CaptureBitmap(const char * outFile)//const char * outFile){
HRESULT hr=0;AM_MEDIA_TYPE mt;hr = pGrabber->GetConnectedMediaType(&mt);
if(FAILED(hr)){ AfxMeageBox(“Failt to read the connected media type”);return FALSE;
}// Examine the format block.VIDEOINFOHEADER *pVih;
if((mt.formattype == FORMAT_VideoInfo)&&(mt.cbFormat >= sizeof(VIDEOINFOHEADER))&&(mt.pbFormat!= NULL))
{
pVih =(VIDEOINFOHEADER*)mt.pbFormat;}else
{
// Wrong format.Free the format block and return an error.return VFW_E_INVALIDMEDIATYPE;}// Set one-shot mode and buffering.hr = pGrabber->SetOneShot(TRUE);
if(SUCCEEDED(pGrabber->SetBufferSamples(TRUE))){
bool pa = false;
m_pMC->Run();long EvCode;AfxMeageBox(“18”);
hr = pEvent->WaitForCompletion(INFINITE,&EvCode);
AfxMeageBox(“6”);//find the required buffer size
long cbBuffer = 0;if(SUCCEEDED(pGrabber->GetCurrentBuffer(&cbBuffer, NULL)))
{//Allocate the array and call the method a second time to copy the buffer:
char *pBuffer = new char[cbBuffer];
if(!pBuffer)
{ // Out of memory.Return an error code.AfxMeageBox(_T(“Out of Memory”));
}
hr = pGrabber->GetCurrentBuffer(&cbBuffer,(long*)(pBuffer));
//写到BMP文件中
HANDLE hf = CreateFile(LPCTSTR(outFile), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
if(hf == INVALID_HANDLE_VALUE)
{ return 0;
}
// Write the file header.BITMAPFILEHEADER bfh;
ZeroMemory(&bfh, sizeof(bfh));
bfh.bfType = 'MB';// Little-endian for “MB”.bfh.bfSize = sizeof(bfh)+ cbBuffer + sizeof(BITMAPINFOHEADER);
bfh.bfOffBits = sizeof(BITMAPFILEHEADER)+ sizeof(BITMAPINFOHEADER);
DWORD dwWritten = 0;
WriteFile(hf, &bfh, sizeof(bfh), &dwWritten, NULL);
// Write the bitmap format
BITMAPINFOHEADER bih;
ZeroMemory(&bih, sizeof(bih));
bih.biSize = sizeof(bih);bih.biWidth = pVih->bmiHeader.biWidth;
bih.biHeight = pVih->bmiHeader.biHeight;
bih.biPlanes = pVih->bmiHeader.biPlanes;
bih.biBitCount = pVih->bmiHeader.biBitCount;
dwWritten = 0;
WriteFile(hf, &bih, sizeof(bih), &dwWritten, NULL);
//write the bitmap bits
dwWritten = 0;
WriteFile(hf, pBuffer, cbBuffer, &dwWritten, NULL);
CloseHandle(hf);
pa = true;
}
return pa;
}
hr = pGrabber->SetOneShot(FALSE);
return true;
}
问题补充:编译可以通过,就是运行时出现内存不能读的问题,经调试发现执行hr = pEvent->WaitForCompletion(INFINITE,&EvCode);这行代码是出现没存不能读的问题。各位大牛大虾们指点下,急用哦!