16,472
社区成员
发帖
与我相关
我的任务
分享
BOOL CVFWcameraDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
// 设置预览窗口
CWnd *pWnd=AfxGetMainWnd()->GetDlgItem(IDC_VIDEO);//得到预览窗口指针,为Animate控件
CRect rect;
pWnd->GetWindowRect(&rect); // 得到窗口大小
m_hCapWnd=capCreateCaptureWindow("Capture",
WS_CHILD|WS_VISIBLE,
0,0,
rect.Width(),rect.Width(),
pWnd->GetSafeHwnd(),0); // 设置预览窗口
if(!m_hCapWnd)
{
MessageBox("Set preview window failed!");
return FALSE;
}
// 连接摄像头
if(!capDriverConnect(m_hCapWnd,0))
{
MessageBox("Connect to Camera failed!");
return FALSE;
}
//得到驱动器的属性
capDriverGetCaps(m_hCapWnd,sizeof(CAPDRIVERCAPS), &m_CapDrvCap);
if(m_CapDrvCap.fCaptureInitialized) // 查看摄像头是否初始化成功
{
capGetStatus(m_hCapWnd, &m_CapStatus,sizeof(m_CapStatus)); // 得到驱动器状态
capPreviewRate(m_hCapWnd,30); // 设置预览帧频,30 ms
}
else{
AfxMessageBox("Camera initilize failed!");
return FALSE;
}
isPreview = FALSE;
return TRUE; // return TRUE unless you set the focus to a control
}
void CVFWcameraDlg::OnOpen()
{
// TODO: Add your control notification handler code here
capPreview(m_hCapWnd,TRUE); // 开始预览
isPreview = TRUE;
}
void CVFWcameraDlg::OnPause()
{
// TODO: Add your control notification handler code here
capPreview(m_hCapWnd,FALSE); // 暂停
}
void CVFWcameraDlg::OnImage()
{
// TODO: Add your control notification handler code here
capFileSaveDIB(m_hCapWnd,"pic.bmp");
}
void CVFWcameraDlg::OnRecord()
{
// TODO: Add your control notification handler code here
capFileSetCaptureFile(m_hCapWnd,"video.avi"); //设置保存路径
capFileAlloc(m_hCapWnd,(1024L*1024L*5)); //设置文件缓存
capCaptureSequence(m_hCapWnd); //开始捕捉
}
void CVFWcameraDlg::OnOK()
{
// TODO: Add extra validation here
if(isPreview == TRUE)
capPreview(m_hCapWnd,FALSE);
// Stop the capture process
capCaptureStop(m_hCapWnd);
capCaptureAbort(m_hCapWnd);
Sleep(500);
// Disable the callback function..
capSetCallbackOnVideoStream(m_hCapWnd, NULL);
Sleep(300); // This delay is important...
// Finally disconnect the driver
capDriverDisconnect(m_hCapWnd);
CDialog::OnOK();
}