char* CCameraDS::QueryFrame() { long evCode, size = 0; #if CALLBACKMODE static double lastSampleTime=0; if( lastSampleTime == cbInfo.dblSampleTime) return NULL; if(cbInfo.lBufferSize == 0) return NULL; if ( m_nBufferSize != cbInfo.lBufferSize) { if (m_pFrame) { free(m_pFrame); } m_nBufferSize = cbInfo.lBufferSize; m_pFrame = (char*)malloc(m_nBufferSize);//cvCreateImage(cvSize(m_nWidth, m_nHeight), IPL_DEPTH_8U, 3); } lastSampleTime = cbInfo.dblSampleTime; m_pFrame2 = (char*)cbInfo.pBuffer; #else m_pMediaControl->Run(); m_pMediaEvent->WaitForCompletion(INFINITE, &evCode); m_pSampleGrabber->GetCurrentBuffer(&size, NULL); if(size == 0) return NULL; //if the buffer size changed if (size != m_nBufferSize) { if (m_pFrame) { free(m_pFrame); } if (m_pFrame2) { free(m_pFrame2); } m_nBufferSize = size; m_pFrame = (char*)malloc(m_nWidth*m_nHeight*3);//cvCreateImage(cvSize(m_nWidth, m_nHeight), IPL_DEPTH_8U, 3); m_pFrame2 = (char*)malloc(m_nWidth*m_nHeight*3); } m_pSampleGrabber->GetCurrentBuffer(&m_nBufferSize, (long*)m_pFrame2); //cvFlip(m_pFrame); #endif for(int i=0;i<m_nHeight;i++) memcpy(m_pFrame+m_nWidth*3*i,m_pFrame2+m_nWidth*3*(m_nHeight-1-i),m_nWidth*3); return m_pFrame; }
1.緩沖區模式
#if !CALLBACKMODE
m_pSampleGrabber->SetBufferSamples(TRUE);
m_pSampleGrabber->SetOneShot(TRUE);
#endif
只能設置SetOneShot為TRUE, 因為使用SetPositions 函數始終返回 E_NOTIMPL:Method is not supported.如果為false,
WaitForCompletion(INFINITE, &evCode)函數會一直等待下去。
2.回調模式
回調模式每采到一直就會進入回調函數,可以在回調函數里面處理采到的數據。
#if CALLBACKMODE
m_pSampleGrabber->SetBufferSamples(FALSE);
m_pSampleGrabber->SetOneShot(FALSE);
// Set the callback, so we can grab the one sample
//
CB.Width = m_nWidth;
CB.Height = m_nHeight;
hr = m_pSampleGrabber->SetCallback( &CB, 1 );
m_pMediaControl->Run();
#endif
只用Run()一次即可。如果SetOneShot(TRUE)的話,也可以每請求一幀Run()一次,然后WaitForCompletion,再從回調函數的buffer中取出數據,這種其實和緩沖區模式一樣,只不過緩沖區變成了在回調函數中。
如果攝像頭采集幀率為30,SetOneShot(FALSE)回調模式,就一秒進去回調函數30次。而OneShot模式會取到重復的幀。