在windows中,提供了一整套视频采集函数,要做一个视频采集软件,先要创建一个采集窗口
hCapWnd = capCreateCaptureWindow((LPSTR)"Capture Window",
WS_CHILD | WS_VISIBLE,
15, 129, VideoW, VideoH,
m_hWnd, (int) 0) ;
采集窗口的大小一般与摄像头有关,然后指定回调函数,这个函数是用来接收采集的数据的,
FARPROC fpVideoCallback = MakeProcInstance((FARPROC)VideoCallbackProc, m_hInstance);
capSetCallbackOnVideoStream(hCapWnd, fpVideoCallback);
if(!capDriverConnect(hCapWnd, 0))
MessageBox("Error! Not found video card!");
设置视频格式。
ZeroMemory(&m_biInput, sizeof(BITMAPINFO));
m_biInput.bmiHeader.biSize = sizeof(BITMAPINFO);
m_biInput.bmiHeader.biWidth = VideoW;
m_biInput.bmiHeader.biHeight = VideoH;
m_biInput.bmiHeader.biPlanes = 1;
m_biInput.bmiHeader.biBitCount = 24;
m_biInput.bmiHeader.biCompression = BI_RGB;
m_biInput.bmiHeader.biSizeImage = VideoW * VideoH * m_biInput.bmiHeader.biBitCount / 8;
m_biInput.bmiHeader.biXPelsPerMeter = 0;
m_biInput.bmiHeader.biYPelsPerMeter = 0;
m_biInput.bmiHeader.biClrUsed = 0;
m_biInput.bmiHeader.biClrImportant = 0;
capSetVideoFormat(hCapWnd, &m_biInput, sizeof(m_biInput));
设置其它一些参数,如帧速率等。
CAPTUREPARMS CapParam;
capCaptureGetSetup(hCapWnd, &CapParam, sizeof(CapParam));
CapParam.fAbortLeftMouse = FALSE;
CapParam.fAbortRightMouse = FALSE;
CapParam.fYield = TRUE;
CapParam.dwRequestMicroSecPerFrame = 1000000/25; //25 FPS
capCaptureSetSetup(hCapWnd, &CapParam, sizeof(CapParam));
capOverlay(hCapWnd, TRUE);
开始视频采集
time1 = GetTickCount();
capCaptureSequenceNoFile(hCapWnd);
停止视频采集
capCaptureStop(hCapWnd);
视频采集回调函数
LRESULT FAR PASCAL VideoCallbackProc(HWND hWnd, LPVIDEOHDR lpVHdr)
{
//lpVHdr就是一帧图象的数据
char s[120];
wsprintf(s, "%ld", FrameNo++);
SetDlgItemText(hMainWnd, IDC_CAPFRAME, s);
if(fpCode)
{
memcpy(EncodeSrc, lpVHdr->lpData, lpVHdr->dwBytesUsed);
//MP4压缩
int length = EncEncode(EncodeSrc, EncodeDest);
//写AVI文件
fwrite(&length, sizeof(int), 1, fpCode);
fwrite(EncodeDest, length, 1, fpCode);
time2 = GetTickCount();
WORD dTime = WORD(time2-time1);
fwrite(&dTime, sizeof(WORD), 1, fpCode);
time1 = GetTickCount();
}
return (LRESULT)TRUE;
}
这是实现视频采集的框架,具体代码大家只有自己写了,因为MP4压缩算法现在美国禁止出口,在Windows中不含有该算法,
采用这种算法每帧图象的数据量只有800字节,如果每秒按15帧来处理,在互联网上传送应该可以实现,如果采用H263协议,数据量还会更小一些,但图象清晰度会下降很多。