通过摄像头采集QCIF格式.
//************************** cap.h ******************/
class CCapture
{
private:
HWND m_hCapWin;
S_WND *m_pWin;
public:
void ShowBIH(BITMAPINFOHEADER *pBIH);
BITMAPINFOHEADER m_BIH;
int m_Stat
void SetupCompress();
void SetupSource();
void SetupVideo();
int Stop();
int Start();
CCapture(S_WND *pWin, int frames,S_AUDIO_CONFIG ac);
~CCapture();
};
#endif
/******************************cap.cpp ****************/
// Cap.cpp: implementation of the CCap class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "..//X.h"
#include "Cap.h"
//#include "..//XDlg.h "
#include "vfw.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern CEdit *pEdit;
//-----------------------------------------------------------------
LRESULT CALLBACK VideoStreamCallback(HWND hWnd, LPVIDEOHDR lpVHdr)
{
// pEdit->ReplaceSel("CallBack!/r/n");
//CXDlg *pWnd=(CXDlg*)AfxGetMainWnd( );
//if(pWnd->m_bSendVideo || pWnd->m_bDir)
// pWnd->CompressVideo(lpVHdr);
return true;
}
LRESULT CALLBACK WaveStreamCallback(HWND hWnd, LPWAVEHDR lpWHdr)
{
//pEdit->ReplaceSel("____CallBack!/r/n");
// CXDlg *pWnd=(CXDlg*)AfxGetMainWnd( );
// if(pWnd->m_bSendAudio)
// pWnd->CompressAudio(lpWHdr);
return true;
}
CCapture::CCapture(S_WND *pWin, int frames, S_AUDIO_CONFIG ac)
{
m_pWin=pWin;
m_Stat=0;
//Not initialized
// AfxMessageBox("Create win ...");
m_hCapWin=capCreateCaptureWindow("CC-Win",
WS_CHILD|WS_VISIBLE,
pWin->rect.left , pWin->rect.top,
pWin->rect.right , pWin->rect.bottom,
pWin->Handle ,0);
if(!m_hCapWin)
{
AfxMessageBox("capCreateCaptureWindow: Fail !");
return
}
m_Stat=1;
//capCreateCaptureWindow OK!
// AfxMessageBox("Create win ok ,to set callback ...");
if(!capSetCallbackOnVideoStream(m_hCapWin,VideoStreamCallback))
{
AfxMessageBox("capSetCallbackOnVideoStream: Fail!");
return
}
if(ac.valid)
{
if(!capSetAudioFormat( m_hCapWin,&(ac.wf),sizeof(ac.wf) ))
{
AfxMessageBox("capSetAudioFormat: Fail!");
return
}
if(!capSetCallbackOnWaveStream(m_hCapWin,WaveStreamCallback))
{
AfxMessageBox("capSetCallbackOnWaveStream: Fail!");
return
}
}
m_Stat=2;
//Callback OK!
if(!capDriverConnect(m_hCapWin,0))
{
AfxMessageBox("设备连接错误:/n 请检查'视频卡'是否正确安装 !");
return
}
m_Stat=3;
//Connect OK!
// AfxMessageBox("To set preview ...");
if(!capPreviewScale(m_hCapWin,TRUE))
AfxMessageBox("capPreviewScale: Fail! ");
if(!capPreview(m_hCapWin,TRUE) )
AfxMessageBox("capPreview: Fail! ");
if( !capOverlay(m_hCapWin, TRUE))
{
AfxMessageBox("无法启动'Overlay'功能 ! ");
return
}
m_Stat=4;
//AfxMessageBox("To Set Para...");
CAPTUREPARMS cpCaptureParms;
//----
capCaptureGetSetup(m_hCapWin,&cpCaptureParms,sizeof(cpCaptureParms));
cpCaptureParms.dwRequestMicroSecPerFrame=(1000000/frames);
//66667;
// Requested capture rate
cpCaptureParms.fMakeUserHitOKToCapture=0;
// Show "Hit OK to cap" dlg?
cpCaptureParms.wPercentDropForError=10;
// Give error msg if > (10%)
cpCaptureParms.fYield=true;
// Capture via background task?
cpCaptureParms.dwIndexSize=34952;
// Max index size in frames (32K)
cpCaptureParms.wChunkGranularity=2048;
// Junk chunk granularity (2K)
cpCaptureParms.fUsingDOSMemory=0;
// Usedo
S buffers?
cpCaptureParms.wNumVideoRequested=5;
// # video buffers, If 0, autocalc
cpCaptureParms.fCaptureAudio=ac.valid;
// Capture audio?
if(ac.valid)
{
cpCaptureParms.wNumAudioRequested=4;
// # audio buffers, If 0, autocalc
cpCaptureParms.dwAudioBufferSize=ac.bufsize;
// Size of audio bufs (0 = default)
}
else
{
cpCaptureParms.wNumAudioRequested=0;
cpCaptureParms.dwAudioBufferSize=0;
}
cpCaptureParms.vKeyAbort=0;
// Virtual key causing abort
cpCaptureParms.fAbortLeftMouse=0;
// Abort on left mouse?
cpCaptureParms.fAbortRightMouse=0;
// Abort on right mouse?
cpCaptureParms.fLimitEnabled=0;
// Use wTimeLimit?
cpCaptureParms.wTimeLimit=5;
// Seconds to capture
cpCaptureParms.fMCIControl=0;
// Use MCI video source?
cpCaptureParms.fStepMCIDevice=0;
// Step MCI device?
cpCaptureParms.dwMCIStartTime=0;
// Time to start in MS
cpCaptureParms.dwMCIStopTime=0;
// Time to stop in MS
cpCaptureParms.fStepCaptureAt2x=0;
// Perform spatial averaging 2x
cpCaptureParms.wStepCaptureAverageFrames=5;
// Temporal average n Frames
cpCaptureParms.fDisableWriteCache=1;
// Attempt to disable write cache
//------
if(!capCaptureSetSetup(m_hCapWin,&cpCaptureParms, sizeof(cpCaptureParms)))
{
AfxMessageBox("capCaptureSetSetup: Fail! ");
return
}
m_Stat=5;
//-----------------
//AfxMessageBox("To Get BIH...");
int wSize=capGetVideoFormatSize(m_hCapWin);
int mysize=sizeof(BITMAPINFO);
LPBITMAPINFO pbi;
if (mysize>wSize)
wSize=mysize;
pbi=(LPBITMAPINFO)malloc(wSize);
if(!capGetVideoFormat(m_hCapWin,pbi,wSize))
{
AfxMessageBox("capGetVideoFormat: Fail! ");
free(pbi); return
}
m_BIH=pbi->bmiHeader;
free(pbi);
if(pWin->bShowBIH)
ShowBIH(&m_BIH);
if( m_BIH.biWidth==176 &&
m_BIH.biHeight==144)
{}
else
{
AfxMessageBox("视频格式不是 176x144 ! ");
return
}
m_Stat=6;
}
CCapture::~CCapture()
{
if(m_Stat>=1)
{
capCaptureStop(m_hCapWin);
capCaptureAbort(m_hCapWin);
if(m_Stat>=3)
capDriverDisconnect(m_hCapWin);
DestroyWindow(m_hCapWin);
}
}
int CCapture::Start()
{
if(m_Stat<6)
return -m_Stat;
if(!capCaptureSequenceNoFile(m_hCapWin))
AfxMessageBox("start capture: Fail !");
return 0;
}
int CCapture::Stop()
{
if(m_Stat<6)
return -m_Stat;
capCaptureStop(m_hCapWin);
capCaptureAbort(m_hCapWin );
return 1;
}
void CCapture::SetupVideo()
{
capDlgVideoFormat(m_hCapWin);
}
void CCapture::SetupSource()
{
capDlgVideoSource(m_hCapWin);
}
void CCapture::SetupCompress()
{
capDlgVideoCompression(m_hCapWin);
}
void CCapture::ShowBIH(BITMAPINFOHEADER *pBIH)
{
char s[300];
wsprintf(s,"biWidth=%d/nbiHeight=%d/nbiPlanes=%d/nbiBitCount=%d/nbiCompression=%x/nbiSizeImage=%d/nbiXPelsPerMeter=%d/nbiYPelsPerMeter=%d/n",
pBIH->biWidth,
pBIH->biHeight,
pBIH->biPlanes,
pBIH->biBitCount,
pBIH->biCompression,
pBIH->biSizeImage,
pBIH->biXPelsPerMeter,
pBIH->biYPelsPerMeter)
AfxMessageBox(s);
}