登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

面包会有的

... ...

 
 
 

日志

 
 

vc++6.0视频捕获源码  

2008-11-19 10:57:11|  分类: VC++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
/********************************************************************\
* CapPicture.c: 类的源码 *
* *
* 注释: 视频信号的采集、显示、存储和一些处理 *
* *
* 函数: *
* WinMain - Application entry point *
* *
* MainWndProc - 主要窗口程序 *
* *
* *
\********************************************************************/

/********************* 头文件 *********************/

#include // windows界面和服务
#include // windows视频库
#include // common dialogs
#include "CapPicture.h" // 资源头文件

/********************* 原型 ***********************/

// 主要窗口程序
LRESULT WINAPI MainWndProc( HWND, UINT, WPARAM, LPARAM );

// 选择捕捉程序驱动程序
LRESULT WINAPI SelCapDrvProc( HWND, UINT, WPARAM, LPARAM );

// 列举捕捉驱动
int EnumCapDrv();

// 右键弹出菜单句柄
VOID APIENTRY HandlePopupMenu(HWND, POINT);

// 视频程序线程
DWORD WINAPI videoThreadProc(LPVOID lParam);
/******************* 全程变量 ********************/

HANDLE ghInstance; //应用程序 instance
HWND hwndMain; //主要窗口句柄
HWND hwndVideo; //视频捕捉窗口
HWND hwndSelCapDrvDlg; //选择捕捉驱动对话
HWND hwndSelCapDrvDlg_LBox; //选择捕捉驱动对话列举框
HWND hwndExit; //推出按钮
HWND hwndMin; //最小化按钮
HWND hwndHelp; //帮助按钮
HWND hwndRecord; //录像按钮
HANDLE hVideoThread; //停止录取视频线程
HRGN hRegion1; //窗口修正区域
CAPDRIVERCAPS CapDrvCaps; // 驱动性能
bool isRecordFileOpen = false; // 录像开始标记
char recordFile[260]; // 保持录像标记
bool isPicFileOpen = false; // flag set if snapshot file is open
char pictureFile[260]; // file to hold snapshot
bool isRecording = false; // 判断是否录像?
bool threadEnd = false; // 判断视频线程是否终止?
/********************************************************************\
* *
* CLASSES, ENUMS, & STRUCTS *
* *
/********************************************************************/

/********************************************************************\
* Function: int PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, int) *
* *
* 用途: 应用程序初始化 *

* *
* 注释: 注册窗口类, 创建显示主要窗口 and enter message loop. *
* *
* *
\********************************************************************/

int PASCAL WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,

LPSTR lpszCmdLine,
int nCmdShow )
{
WNDCLASS wc;
MSG msg;

if( !hPrevInstance )
{
wc.lpszClassName = "GenericAppClass";
wc.lpfnWndProc = MainWndProc;
wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = CreateSolidBrush (RGB(0, 64, 128));
wc.lpszMenuName = "GenericAppMenu";

wc.cbClsExtra = 0;
wc.cbWndExtra = 0;

RegisterClass( &wc );
}

ghInstance = hInstance;

hwndMain = CreateWindow( "GenericAppClass",
"Super Video",
WS_POPUP,
0,
0,
500,
500,
NULL,
NULL,
hInstance,
NULL
);

ShowWindow( hwndMain, nCmdShow );

//将主要的窗户设定为区域
SetWindowRgn(hwndMain,hRegion1,1);

while( GetMessage( &msg, NULL, 0, 0 ) ) {
TranslateMessage( &msg );

DispatchMessage( &msg );
}

return msg.wParam;
}

/********************************************************************\
* Function: LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM) *
* *
* 目的: 程序申请消息 *
* *
* 意见: 下列消息被处理 *

* *
* WM_PAINT *
* WM_CREATE *
* WM_DESTROY *
* *
* *
\********************************************************************/

LRESULT CALLBACK MainWndProc( HWND hwndMain, UINT msg, WPARAM wParam,
LPARAM lParam )
{
HDC hDC = GetDC(hwndMain);
RECT rc; //客户区域
POINT pt; // 鼠标电击位置

switch( msg ) {

/**************************************************************\
* WM_LBUTTONDBLCLK: *
\**************************************************************/

case WM_LBUTTONDBLCLK:
SetFocus(hwndMain);
break;
/**************************************************************\
* WM_LBUTTONDOWN: *
\**************************************************************/

case WM_RBUTTONDOWN:
// Get the bounding rectangle of the client area. 获得正要启动的区域

GetClientRect(hwndMain, (LPRECT) &rc);

// Get the client coordinates for the mouse click. 获得用户

pt.x = LOWORD(lParam);

pt.y = HIWORD(lParam);

// 如果鼠标点击发生在用户
// 区域, 运行申请定义的功能
// 显示捷径菜单。


if (PtInRect((LPRECT) &rc, pt))
HandlePopupMenu(hwndMain, pt);
break;

/**************************************************************\
* WM_PAINT: *
\**************************************************************/

case WM_PAINT:
//给区域红色的边缘
FrameRgn(hDC,hRegion1,CreateSolidBrush(RGB(0,0,0)),2,2);

// brung our dialog to the foreground
BringWindowToTop(hwndSelCapDrvDlg);
return( DefWindowProc( hwndMain, msg, wParam, lParam ));

/**************************************************************\
* WM_COMMAND: *
\**************************************************************/
case WM_COMMAND:
CAPSTATUS CapStatus;

switch( wParam ) {

case SOURCE:
if(CapDrvCaps.fHasDlgVideoSource)
capDlgVideoSource(hwndVideo);
break;
case FORMAT://why doesnt this work
//if(CapDrvCaps.fHasDlgVideoFormat)
//{
capDlgVideoFormat(hwndMain);
// Are there new image dimensions
capGetStatus(hwndVideo, &CapStatus, sizeof(CAPSTATUS));
SetWindowPos(hwndVideo, NULL, 0, 0, CapStatus.uiImageWidth,
CapStatus.uiImageHeight, SWP_NOZORDER | SWP_NOMOVE);
//}
break;
case DISPLAY://why doesnt this work
//if (CapDrvCaps.fHasDlgVideoDisplay)
capDlgVideoDisplay(hwndVideo);
break;
case EXIT:
SendMessage(hwndMain, WM_SYSCOMMAND, SC_CLOSE, 0);
break;
case MINIMIZE:
SendMessage(hwndMain, WM_SYSCOMMAND, SC_MINIMIZE, 0);
break;
case HELP:
SendMessage(hwndMain, WM_SYSCOMMAND, SC_CONTEXTHELP, 0);
break;
case RECORDVIDEO:
if(HIWORD(wParam) == BN_CLICKED && (HWND) lParam == hwndRecord)
{
if (!isRecordFileOpen)
{
OPENFILENAME ofn; // 打开文件名结构

// 初始化打开文件名字
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwndMain;
ofn.lpstrFile = recordFile;
ofn.nMaxFile = sizeof(recordFile);
ofn.lpstrFilter = "*.avi\0*.avi";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

// 显示保存对话框
if(GetSaveFileName(&ofn) == TRUE)
{
strcpy(recordFile, ofn.lpstrFile);
strcat(recordFile, ".avi");
isRecordFileOpen = true;

// 创建视频采集线程
DWORD id;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDe脚本or = NULL;
sa.bInheritHandle = TRUE;
hVideoThread = CreateThread(&sa, (ULONG)0,
videoThreadProc, (LPVOID)(ULONG)0, (ULONG)0, &id);
if(hVideoThread == NULL)
MessageBox(NULL, "Creation of Record Thread failed!", "Thread", MB_OK | MB_ICONEXCLAMATION);

break;
}
}

if (isRecordFileOpen) // 我们已经让一个挑选的文件
{
if(isRecording) // 准备采集
{
threadEnd = true;
// 结束采集并保存
capFileSaveAs(hwndVideo, recordFile);

// 让录像按钮显示“采集”
SetWindowText(hwndRecord, "采集");

isRecording = false;
break;
}

if(!isRecording ) //没有采集,但是一个文件已经选择
{
int a = 0;

MessageBox(hwndMain, "Do you want to write over the open file?",
"File warning", MB_YESNO | MB_ICONWARNING);
if (a != IDYES)
{
isRecordFileOpen = false;
SendMessage(hwndMain, WM_COMMAND, MAKEWPARAM(RECORDVIDEO, BN_CLICKED), (LPARAM) hwndRecord);
}

if (a == IDYES)
{
capCaptureSequence(hwndVideo);
isRecording = true;
}

break;
}
}
}
break;
}
break;


/**************************************************************\
* WM_CREATE: *
\**************************************************************/

case WM_CREATE:

RECT helpRect, minRect, exitRect;
HRGN helpRgn, minRgn, exitRgn;

// 创建主窗口区域,圆形边角的一个矩形区域(区域左上角落X坐标,区域左上角落Y坐标,
//区域右下角落X坐标,区域右下角落的Y坐标,椭圆角落高度,椭圆角落宽度)

hRegion1 = CreateRoundRectRgn(100,100,440,410, 10, 10);

// 创建视频采集窗口
hwndVideo = capCreateCaptureWindow(
(LPSTR) "My Capture Window",//名称
WS_CHILD | WS_VISIBLE,//风格
120, 130, 300, 240,//左上坐标和高度宽度
(HWND) hwndMain,//父窗口句柄
(int) 1);//窗口标识符

// 创建主窗口按钮

// 创建关闭按钮
hwndExit = CreateWindow (
"button", // 按钮类
"x", // 按钮上的文本
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, // 风格
410, 110, BUTTONSIZE, BUTTONSIZE, // 位置和大小 375 110
hwndMain, // 父窗口句柄
(HMENU) EXIT,// 控制 ID: EXIT
(HINSTANCE)ghInstance,
(LPVOID)NULL);

// 创建最小化按钮
hwndMin = CreateWindow (
"button", // 按钮类
"-", // 按钮上的文本
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, // 风格
390, 110, BUTTONSIZE, BUTTONSIZE, // 按钮位置和大小
hwndMain, // 父窗口句柄
(HMENU) MINIMIZE,// 控制 ID: MINIMIZE
(HINSTANCE)ghInstance,
(LPVOID)NULL);

// 创建帮助按钮
hwndHelp = CreateWindow (
"button", // 按钮类
"?", // 按钮上的文本
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, // 风格
370, 110, BUTTONSIZE, BUTTONSIZE, // 按钮位置和大小
hwndMain, // 父窗口句柄
(HMENU) HELP,// 控制 ID: HELP
(HINSTANCE)ghInstance,
(LPVOID)NULL);

// 创建记录按钮
hwndRecord = CreateWindow (
"button", // 按钮类
"采集", // 按钮上的文本
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, // 风格
250, 378, 45, 25, // 按钮位置和大小
hwndMain, // 父窗口句柄
(HMENU) RECORDVIDEO,// 控制 ID: RECORDVIDEO
(HINSTANCE)ghInstance,
(LPVOID)NULL);

//获得按钮矩形
GetClientRect(hwndHelp, &helpRect);
GetClientRect(hwndMin, &minRect);
GetClientRect(hwndExit, &exitRect);

// 创建按钮区域
helpRgn = CreateEllipticRgnIndirect(&helpRect);
minRgn = CreateEllipticRgnIndirect(&minRect);
exitRgn = CreateEllipticRgnIndirect(&exitRect);

//将窗户设定为区域
SetWindowRgn(hwndExit,exitRgn,1);
SetWindowRgn(hwndMin,minRgn,1);
SetWindowRgn(hwndHelp,helpRgn,1);

// create the SelCapDrv dialog
hwndSelCapDrvDlg = CreateDialog((HINSTANCE)ghInstance,
MAKEINTRESOURCE( SELCAPDRVDLG ),
0, (DLGPROC)SelCapDrvProc);
// 获得列表框句柄
hwndSelCapDrvDlg_LBox = GetDlgItem(hwndSelCapDrvDlg,
SELCAPDRVDLG_LSTBOX);
EnumCapDrv();
break;

/****************************************************************\
*WM_DESTROY: PostQuitMessage() is called and get rid of vfw stuff*
\****************************************************************/

case WM_DESTROY:
capPreview(hwndVideo, FALSE); // end preview
capDriverDisconnect(hwndVideo); // disconnect from driver
PostQuitMessage( 0 );
break;

/**************************************************************\
* Let the default window proc handle all other messages *
\**************************************************************/

default:
return( DefWindowProc( hwndMain, msg, wParam, lParam ));

}

return 0;
}

LRESULT CALLBACK SelCapDrvProc( HWND hWnd, UINT msg, /*callback procedure */
WPARAM wParam, LPARAM lParam )
{

switch(msg)
{
// dialog created
case WM_INITDIALOG:
return TRUE;
// command
case WM_COMMAND:
switch ( wParam )
{
// 用户点击“选择驱动”按钮
case SELCAPDRVDLG_BUTTON:
int sel = 0;
// 获得选择的驱动
SendMessage( hwndSelCapDrvDlg_LBox, LB_GETSELITEMS, 1, sel);
// 连接驱动
SendMessage( hwndVideo, WM_CAP_DRIVER_CONNECT, sel, 0L);
// 关闭对话框
SendMessage( hwndSelCapDrvDlg, WM_CLOSE, 0, 0);
// 更新驱动性能
SendMessage( hwndVideo, WM_CAP_DRIVER_GET_CAPS,
sizeof(CAPDRIVERCAPS), (LONG) (LPVOID) &CapDrvCaps);

// 设定初始频率为66Hz
capPreviewRate( hwndVideo, 66 );
// 开始预览影像
capPreview( hwndVideo, TRUE );
}
return TRUE;
// 如果用户想关闭对话
case WM_CLOSE:
DestroyWindow(hwndSelCapDrvDlg);
return TRUE;
}

return( 0L );
}

int EnumCapDrv() // 列举安装的采集驱动程序
{
char szDeviceName[80]; // 驱动名字
char szDeviceVersion[80]; // driver version
char item[161]; // concatinated string
int i; // counter

for (i=0; i<10; i++)
{
if ( capGetDriverDe脚本ion(i, szDeviceName, sizeof(szDeviceName),
szDeviceVersion, sizeof(szDeviceVersion)) )
{
strcpy(item, szDeviceName);
strcat(item, " ");
strcat(item, szDeviceVersion);

// 把项目加入列表盒
SendMessage(hwndSelCapDrvDlg_LBox, LB_ADDSTRING, 0,
(LPARAM) item);
SendMessage(hwndSelCapDrvDlg_LBox, LB_SETITEMDATA, i, (LPARAM) i);
}
}

return 0;
}

/*int CreateWndButtons()
{//why doesnt this work
RECT rc; // window rectangle
GetClientRect(hwndMain, &rc);
// create the button
hwndExit = CreateWindow (
"button", // Builtin button class
"X", // button text
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // styles
100, 100, BUTTONSIZE, BUTTONSIZE, // position and size
hwndMain, // Parent is main window
(HMENU) EXIT,// Control ID: EXIT
(HINSTANCE)ghInstance,
(LPVOID)NULL);
return 0;
}
*/
VOID APIENTRY HandlePopupMenu(HWND hwnd, POINT pt)
{
HMENU hmenu; // menu template
HMENU hmenuTrackPopup; // shortcut menu

// Load the menu template containing the shortcut menu from the
// application's resources.

hmenu = LoadMenu((HINSTANCE)ghInstance, "PopupMenu");
if (hmenu == NULL)
return;

// Get the first shortcut menu in the menu template. This is the

// menu that TrackPopupMenu displays.

hmenuTrackPopup = GetSubMenu(hmenu, 0);

// TrackPopup uses screen coordinates, so convert the
// coordinates of the mouse click to screen coordinates.

ClientToScreen(hwnd, (LPPOINT) &pt);

// Draw and track the shortcut menu.

TrackPopupMenu(hmenuTrackPopup, TPM_LEFTALIGN | TPM_LEFTBUTTON,
pt.x, pt.y, 0, hwnd, NULL);

// 关闭菜单.

DestroyMenu(hmenu);

}

DWORD WINAPI videoThreadProc(LPVOID lParam)
{
// 使录像按钮显示“停止”
SetWindowText(hwndRecord, "停止");

// 采集视频
capCaptureSequence(hwndVideo);
isRecording = true;

//在录像按钮再次按下之前别推出线程
while (!threadEnd)
;
//MessageBox(拥有者窗户的柄,在消息框中的本文的住址,消息框名称地址,样式)
MessageBox(NULL, "Leaving Thread", "thread", NULL);
return 0;
}
  评论这张
 
阅读(1682)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018