用DX 播放器的GrabberSample抓图,本人仿做的,但是总抓不到图,希望各位大侠指点!

izardMan 2011-02-25 01:42:27
http://download.csdn.net/source/3042008,如果分不够可以加,做好的发我邮箱wuzongman1@sina.com
...全文
244 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
izardMan 2011-03-02
  • 打赏
  • 举报
回复
好了,但是和另一个视频的SDK一起用好像冲突,总是getpin内存访问冲突,这个怎么办?希望有人帮忙,
izardMan 2011-02-28
  • 打赏
  • 举报
回复
能不能给我发你的程序给我看看,我找找什么不同
izardMan 2011-02-28
  • 打赏
  • 举报
回复
我的执行到mEvent->WaitForCompletion(INFINITE, &evCode);要等好久,控制窗口才能反应,如果屏蔽这句得到的BufferSize也是0
无水先生 2011-02-28
  • 打赏
  • 举报
回复
我的增加了一个对象,m_Tif,专门将图片导出来:

memcpy(m_Tif.pImage,pBuffer,m_BMHEAD.biSizeImage);
无水先生 2011-02-28
  • 打赏
  • 举报
回复
表面上看和我的一样,在试试吧,只要cbBuffer被填满,一般不会错的.

long CDXGraph::GetSample(char *pBuffer)
{
long cbBuffer = 0;
HRESULT hr = pGrabber->GetCurrentBuffer(&cbBuffer, NULL);
if(FAILED(hr))return hr;
pBuffer = new char[cbBuffer];
if (!pBuffer)
{
return E_OUTOFMEMORY;
}
hr = pGrabber->GetCurrentBuffer(&cbBuffer,
reinterpret_cast<long*>(pBuffer));
if(FAILED(hr))
return hr;
return cbBuffer;

}

无水先生 2011-02-28
  • 打赏
  • 举报
回复

请参考参考吧!

LONG CPlayStamDlg::Grasp()
{
HRESULT hr;
long cbBuffer = 0;
hr = pGrabber->GetCurrentBuffer(&cbBuffer, NULL);//NULL得到Buffer尺寸
ASSERT(cbBuffer>0);
if(!pBuffer) pBuffer = new char[cbBuffer];
ASSERT(pBuffer!=NULL);
hr = pGrabber->GetCurrentBuffer(&cbBuffer, (long*)pBuffer); //NULL得到Buffer数据
memcpy(m_Tif.pImage,pBuffer,m_BMHEAD.biSizeImage);
fix(); //BMP处理接口
return S_OK;
}
sxqinge 2011-02-28
  • 打赏
  • 举报
回复
LZ的代码好复杂,一个类里面的代码量应该超过一千行了吧?该重构下了
我解决不了问题,帮顶!
izardMan 2011-02-27
  • 打赏
  • 举报
回复
mFilterGraph是CDXGraph的对象
izardMan 2011-02-27
  • 打赏
  • 举报
回复
bool CDXGraph::CreateSampleGrabber()
{
if(!pGrabberF)
{


HRESULT hr=CoCreateInstance(CLSID_SampleGrabber,NULL,CLSCTX_INPROC_SERVER,
IID_IBaseFilter,reinterpret_cast<void**>(&pGrabberF));
if(!SUCCEEDED(hr))
{
return hr;
}
hr=mGraph->AddFilter(pGrabberF,L"Sample Grabber");
if(FAILED(hr))
{
return hr;
}
pGrabberF->QueryInterface(IID_ISampleGrabber,(void**)&pGrabber);
}

}

bool CDXGraph::SpecifyMediaType()
{
HDC hdc = GetDC(NULL);
int iBitDepth = GetDeviceCaps(hdc, BITSPIXEL);
ReleaseDC(NULL, hdc);
// Set the media type.
AM_MEDIA_TYPE mt;
ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
mt.majortype = MEDIATYPE_Video;
switch (iBitDepth)
{
case 8:
mt.subtype = MEDIASUBTYPE_RGB8;
break;
case 16:
mt.subtype = MEDIASUBTYPE_RGB555;
break;
case 24:
mt.subtype = MEDIASUBTYPE_RGB24;
break;
case 32:
mt.subtype = MEDIASUBTYPE_RGB32;
break;
default:
return E_FAIL;
}
HRESULT hr= pGrabber->SetMediaType(&mt);
return hr;

}

HRESULT CDXGraph::CompleteBuildGraph(const char * inFile)
{
WCHAR szFilePath[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, inFile, -1, szFilePath, MAX_PATH);
IBaseFilter *pSrc;
HRESULT hr=mGraph->AddSourceFilter(szFilePath,L"Source", &pSrc);

hr = ConnectFilters(mGraph, pSrc, pGrabberF);
if(FAILED(hr))return hr;

IBaseFilter *pNull = NULL;
hr = CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, reinterpret_cast<void**>(&pNull));
if(FAILED(hr))return hr;
hr = mGraph->AddFilter(pNull, L"NullRenderer");
if(FAILED(hr))return hr;
hr = ConnectFilters(mGraph, pGrabberF, pNull);
return hr;

}
HRESULT CDXGraph::GetPin(IBaseFilter *pFilter, PIN_DIRECTION PinDir, IPin **ppPin)
{
IEnumPins *pEnum;
IPin *pPin;
pFilter->EnumPins(&pEnum);
while(pEnum->Next(1, &pPin, 0) == S_OK)
{
PIN_DIRECTION PinDirThis;
pPin->QueryDirection(&PinDirThis);
if (PinDir == PinDirThis)
{
pEnum->Release();
*ppPin = pPin;
return S_OK;
}
pPin->Release();
}
pEnum->Release();
return E_FAIL;
}

HRESULT CDXGraph::ConnectFilters(IGraphBuilder *pGraph, IBaseFilter *pFirst, IBaseFilter *pSecond)
{
IPin *pOut = NULL, *pIn = NULL;
HRESULT hr = GetPin(pFirst, PINDIR_OUTPUT, &pOut);
if (FAILED(hr)) return hr;
hr = GetPin(pSecond, PINDIR_INPUT, &pIn);
if (FAILED(hr))
{
pOut->Release();
return E_FAIL;
}
hr = pGraph->Connect(pOut, pIn);
pIn->Release();
pOut->Release();
return hr;
}


HRESULT CDXGraph::RunGrabberGraph()
{
// Set one-shot mode and buffering.
HRESULT hr= pGrabber->SetOneShot(TRUE);
hr = pGrabber->SetBufferSamples(TRUE);

if (FAILED(hr))
return hr;
mMediaControl->Run(); // Run the graph.

LONG FAR evCode=0xFFFFFFFF;
mEvent->WaitForCompletion(INFINITE, &evCode); // Wait till it's done.
return evCode;


}

long CDXGraph::GetSample(char *pBuffer)
{
long cbBuffer = 0;
HRESULT hr = pGrabber->GetCurrentBuffer(&cbBuffer, NULL);
if(FAILED(hr))return hr;
pBuffer = new char[cbBuffer];
if (!pBuffer)
{
return E_OUTOFMEMORY;
}
hr = pGrabber->GetCurrentBuffer(&cbBuffer,
reinterpret_cast<long*>(pBuffer));
if(FAILED(hr))
return hr;
return cbBuffer;

}

HRESULT CDXGraph::Display(HDC hdc)
{
AM_MEDIA_TYPE mt;
HRESULT hr = pGrabber->GetConnectedMediaType(&mt);
VIDEOINFOHEADER *pVih;
long bmSize=0;
pGrabber->GetCurrentBuffer(&bmSize,NULL);
long *pBuffer=new long [bmSize];
pGrabber->GetCurrentBuffer(&bmSize,pBuffer);

if ((mt.formattype == FORMAT_VideoInfo) &&
(mt.cbFormat>=sizeof(VIDEOINFOHEADER))&&
(mt.pbFormat!=NULL))
{
pVih = reinterpret_cast<VIDEOINFOHEADER*>(mt.pbFormat);

}
else
{
// Free the format block when you are done:
FreeMediaType(mt);
return VFW_E_INVALIDMEDIATYPE; // Something went wrong
// pVih->bmiHeader is the BITMAPINFOHEADER for the frame.
}
SetDIBitsToDevice(
hdc,0,0,
pVih->bmiHeader.biWidth,
pVih->bmiHeader.biHeight,
0,0,
0,
pVih->bmiHeader.biHeight,
pBuffer,
(BITMAPINFO*)&pVih->bmiHeader,
DIB_RGB_COLORS
);
FreeMediaType(mt);
return hr;


}
//-------------
void CSimplePlayerDlg::OnButtonOpen()
{
// TODO: Add your control notification handler code here
CString strFilter = "AVI File (*.avi)|*.avi|";
strFilter += "MPEG File (*.mpg;*.mpeg)|*.mpg;*.mpeg|";
strFilter += "Mp3 File (*.mp3)|*.mp3|";
strFilter += "Wave File (*.wav)|*.wav|";
strFilter += "All Files (*.*)|*.*|";
CFileDialog dlgOpen(TRUE, NULL, NULL, OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
strFilter, this);
if (IDOK == dlgOpen.DoModal())
{
mSourceFile = dlgOpen.GetPathName();
// Rebuild the file playback filter graph
CreateGraph();
}
}

void CSimplePlayerDlg::OnButtonPlay()
{
if (mFilterGraph)
{
mFilterGraph->Run();
//mFilterGraph->RunGrabberGraph();


// Start a timer
if (mSliderTimer == 0)
{
mSliderTimer = SetTimer(SLIDER_TIMER, 100, NULL);
}
}
}
void CSimplePlayerDlg::CreateGraph(void)
{
DestroyGraph();

mFilterGraph = new CDXGraph();

if (mFilterGraph->Create())
{mFilterGraph->CreateSampleGrabber();
mFilterGraph->CompleteBuildGraph(mSourceFile);
mFilterGraph->SpecifyMediaType();

mFilterGraph->RenderFile(mSourceFile);
// Set video window and notification window
mFilterGraph->SetDisplayWindow(mVideoWindow.GetSafeHwnd());
mFilterGraph->SetNotifyWindow(this->GetSafeHwnd());
// Show the first frame
mFilterGraph->Pause();
}
}
void CSimplePlayerDlg::OnButtonGrab()
{
if (mFilterGraph)
{
// Firstly grab a bitmap to a temp file
char szTemp[] = "C:\\mysnapshot.bmp";
//mFilterGraph-> RunGrabberGraph();
// long szbuffer=mFilterGraph->GetSample(NULL);unsigned
char * buffer=new char [640*480*3];//
if (mFilterGraph->Display(((CStatic *)GetDlgItem(IDC_STATIC_1))->GetDC()->m_hDC));//,buffer
{

}
//mFilterGraph->SnapshotBitmap(szTemp);
}
}
rageliu 2011-02-26
  • 打赏
  • 举报
回复
改代码就算了
izardMan 2011-02-26
  • 打赏
  • 举报
回复
关键是代码太多,贴的怕搞不清楚

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧