⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 capvideodlg.cpp

📁 视频捕捉API源码
💻 CPP
字号:
// CapVideoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CapVideo.h"
#include "CapVideoDlg.h"
#include "vfw.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

HWND ghCapWnd;
CAPDRIVERCAPS gCapDrvCaps;
CString gCapFilename; 

LRESULT CALLBACK FrameCallbackProc(HWND ghWnd, LPVIDEOHDR lpVData)
{
	if (!ghCapWnd) 
       return FALSE; 
 
    //wsprintf(gachBuffer, "Preview frame# %ld ", gdwFrameNum++); 
    //SetWindowText(ghWndMain, (LPSTR)gachBuffer); 
    return (LRESULT) TRUE ; 
}

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCapVideoDlg dialog

CCapVideoDlg::CCapVideoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCapVideoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCapVideoDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCapVideoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCapVideoDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCapVideoDlg, CDialog)
	//{{AFX_MSG_MAP(CCapVideoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCapVideoDlg message handlers

BOOL CCapVideoDlg::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;
	CRect rect;
	//TCHAR sCapWndText[] = _T("视频窗");
	pWnd = AfxGetMainWnd()->GetDlgItem(IDC_PIC);
	pWnd->GetWindowRect(&rect);
	pWnd->GetSafeHwnd();
	ghCapWnd = capCreateCaptureWindow((LPCTSTR)(_T("视频窗")),
											WS_CHILD|WS_VISIBLE|WS_EX_CLIENTEDGE|WS_EX_DLGMODALFRAME,
											0,
											0,
											rect.Width(),
											rect.Height(),
											pWnd->GetSafeHwnd(),
											0);
	ASSERT(ghCapWnd);
	if (capDriverConnect(ghCapWnd, 0))/*判断采集窗口是否与0号捕获卡驱动程序相连接,这里采用简化的方法,因只一块捕获卡,计算机自动登记号码通常是为0*/
	{
		capDriverGetCaps(ghCapWnd, &gCapDrvCaps, sizeof(CAPDRIVERCAPS)); /*作默认值初始化,并得到驱动器的性能,存入CAPDRIVERCAPS结构中*/
		if (gCapDrvCaps.fCaptureInitialized) //如初始化成功
		{
			capPreviewRate(ghCapWnd, 33); //设置预视帧频
			capPreview(ghCapWnd, TRUE); /*设置成预视模式(preview),该方式是通过内存作为缓冲区来存放视频数据,它是获得视频数据的必要条件。另一种称为Overlay模式,它是不经过内存而直接将数据传入显存中。它不符合我们要求。*/
			capSetCallbackOnFrame(ghCapWnd, FrameCallbackProc); //设置每帧结束后所调用的回调函数(第二部作解释)
		}
		else{//初始化不成功
			AfxMessageBox("捕获卡初始化失败"); //初始化不成功的消息框显示
			AfxGetMainWnd()->PostMessage(WM_CLOSE);//发送WM_CLOSE消息,关闭对话框
		}
	}
	else{//连接不成功
		AfxMessageBox("捕获卡连接失败"); //连接不成功的消息框显示
		AfxGetMainWnd()->PostMessage(WM_CLOSE); //发送WM_CLOSE消息,关闭对话框
	}
	return  TRUE;
}
void CCapVideoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCapVideoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCapVideoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCapVideoDlg::OnOK() 
{
	capDlgVideoFormat( ghCapWnd); /*产生一视频格式对话框,这是捕获卡驱动程序中提供的,用户可通过它来选择视频格式*/
}

void CCapVideoDlg::OnButton1() 
{
	capDlgVideoSource( ghCapWnd); //产生一视频源选择对话框产生一视频源选择对话框,它也是驱动程序中带有的	
}

void CCapVideoDlg::OnButton2() 
{
	capGrabFrameNoStop(ghCapWnd); //该函数从捕获卡获得的帧数据不被压缩地存入视频缓冲区中,之后将其显示出来,而采用capGrabCapFrame( )会产生图象冻结效果。
	capEditCopy(ghCapWnd); //将单帧图象复制到粘帖板上
}

void CCapVideoDlg::OnButton3() 
{
	// 写视频数据存盘程序
	capCaptureSequence(ghCapWnd); //开始保存AVI文件到默认的文件中"C:\Capture.avi"。	
}

void CCapVideoDlg::OnButton4() 
{
	capCaptureSequenceNoFile(ghCapWnd); //不存盘操作,但视频仍然显示	
}

void CCapVideoDlg::OnCancel() 
{
	capDriverDisconnect(ghCapWnd); //断开视频窗口与捕获驱动程序的连接
	CDialog::OnCancel(); //退出函数
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -