gusucode.com > 基于VC++的局域网视频聊天系统源码程序 > 基于VC++的局域网视频聊天系统源码程序/code/ChatClient/VideoCapture.cpp
// VideoCapture.cpp: implementation of the VideoCapture class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "ChatClientDlg.h" #include "VideoCapture.h" #include <afxmt.h> #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif #pragma comment(lib,"vfw32") #pragma comment(lib,"winmm") ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// VideoCapture::VideoCapture() { m_capwnd=NULL; } VideoCapture::~VideoCapture() { DestroyWindow(m_capwnd); } void VideoCapture::SetDialog(CDialog *dialog) { dlg=dialog; } //捕获窗口 BOOL VideoCapture::Initialize() { char devname[128]={0},devversion[128]={0}; int index=0; BOOL ret = TRUE, ret1 = TRUE, ret2 = TRUE, ret3 = TRUE; TRACE("VideoCapture::Initialize\n"); //创建一个AVICap捕获窗口 m_capwnd = capCreateCaptureWindow("Capture",WS_POPUP,0,0,1,1,0,0); if(!m_capwnd) { return FALSE; } //connect callback functions ret = capSetUserData(m_capwnd,this); //Change destroy functions also........ ret1 = capSetCallbackOnVideoStream(m_capwnd,OnCaptureVideo); //得到已安装的捕获设备的名称及版本 ret2 = capGetDriverDescription(index,devname,100,devversion,100); // Connect to webcam driver //使一个捕获窗口与一个捕获设备连接或关联 ret3 = capDriverConnect(m_capwnd,index); if(!(ret && ret1 && ret2 && ret3)) { // Device may be open already or it may not have been // closed properly last time. AfxMessageBox("Unable to open Video Capture Device"); // log.WriteString("\n Unable to connect driver to the window"); m_capwnd=NULL; return FALSE; } // Set the capture parameters if(SetCapturePara()==FALSE) { // log.WriteString("\n Setting capture parameters failed"); capDriverDisconnect(m_capwnd); //使捕获窗口与一个捕获设备断开 return FALSE; } return TRUE; } //Start capturing frames from webcam BOOL VideoCapture::StartCapture() { // Start live capturing ... return(capCaptureSequenceNoFile(m_capwnd)); } //终止一个捕获任务 BOOL VideoCapture::StopCapture() { capCaptureStop(m_capwnd); capCaptureAbort(m_capwnd); Sleep(500); return TRUE; } //Stop the catpure process and disconnect the driver BOOL VideoCapture::Destroy() { BOOL ret=TRUE,ret1=TRUE,ret2=TRUE; // Stop the capturing process capCaptureAbort(m_capwnd) ; // Disable the callback function. ret = capSetCallbackOnVideoStream(m_capwnd, NULL); ret1 = capSetUserData(NULL,this); Sleep(300); // important... // Finally disconnect the driver ret2 = capDriverDisconnect(m_capwnd) ; return (ret && ret1 && ret2); } //Set various capture parameters... BOOL VideoCapture::SetCapturePara() { CAPTUREPARMS CapParms={0}; capCaptureGetSetup(m_capwnd,&CapParms,sizeof(CapParms)); //得到当前的捕获速度 CapParms.fAbortLeftMouse = FALSE; CapParms.fAbortRightMouse = FALSE; CapParms.fYield = TRUE; CapParms.fCaptureAudio = FALSE; CapParms.wPercentDropForError = 80; if(!capCaptureSetSetup(m_capwnd,&CapParms,sizeof(CapParms))) { // log.WriteString("\n Failed to set the capture parameters "); return FALSE; } // Set Video Format capGetVideoFormat(m_capwnd,&m_bmpinfo,sizeof(m_bmpinfo)); m_bmpinfo.bmiHeader.biWidth=IMAGE_WIDTH; m_bmpinfo.bmiHeader.biHeight=IMAGE_HEIGHT; BOOL ret=capSetVideoFormat(m_capwnd,&m_bmpinfo,sizeof(m_bmpinfo)); // log.WriteString("\n Video parameters set properly"); return ret; } // Invoked when the video frame is captured by the driver LRESULT CALLBACK OnCaptureVideo(HWND mwnd,LPVIDEOHDR lphdr) { VideoCapture *vidcap=(VideoCapture *)capGetUserData(mwnd); if(vidcap!=NULL ) ((CChatClientDlg *) (vidcap->dlg))->SendVideo(lphdr->lpData,lphdr->dwBytesUsed); return TRUE; }