利用Win32API驱动本机摄像头
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
程序名称: WebCam测试
目的:利用Win32API驱动本机摄像头。
注意事项: vfm32.lib路径默认不在LabWindow的环境变量中,需手工添加。
修改纪录:
作者: FASM
生成日期:2009.5.22
*/
#include
#include
#include
#include
#include "Main.h"
static int panelHandle;
static int CapHandle; //获得的视频窗口句柄
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "Main.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}
int CVICALLBACK PICCALLBACK (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_LEFT_CLICK:
if(CapHandle) //如果视频窗口创建成功
{
SendMessage ((HWND)CapHandle, WM_CAP_DRIVER_DISCONNECT, 0, 0); //如果已打开,则先关闭
SendMessage ((HWND)CapHandle, WM_CAP_DRIVER_CONNECT, 0, 0); //连接设备
SendMessage ((HWND)CapHandle, WM_CAP_SET_PREVIEWRATE, 1000, 0); //设置御览祯率,目前设为1000(设备较旧)SendMessage ((HWND)CapHandle, WM_CAP_SET_SCALE, TRUE, 0);
SendMessage ((HWND)CapHandle, WM_CAP_SET_PREVIEW, TRUE, 0); //打开御览
}
break;
}
return 0;
}
int CVICALLBACK QUITCALLBACK (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
if(CapHandle)
SendMessage ((HWND)CapHandle, WM_CAP_DRIVER_DISCONNECT, 0, 0); //关闭设备连接,窗口句柄不需要释放(只是借用来的) QuitUserInterface (0);
break;
case EVENT_LEFT_CLICK:
break;
}
return 0;
}
/*
函数说明:本来获得设备信息的最佳位置是在窗口类的创建过程,但LabWindows默认不处理Windows消息。所以改在这里来预处理设备信息。
注意:这个函数是会被LabWindows多次调用的,所以应对设备用到的资源进行保护。本例利用全局的视频御览句柄做保护标志。(详见代码)
*/
int CVICALLBACK PANELCALLBACK (int panel, int event, void *callbackData,
int eventData1, int eventData2)
{
char szDeviceName[MAX_PATH] = {'\0',}; //设备名称
char szDeviceDesc[MAX_PATH] = {'\0',}; //设备描述
int Handle = 0; //面板的句柄
int CapLeft = 0; //这里和以下为创建的御览区域
int CapTop = 0;
int CapHeight = 0;
int CapWidth = 0;
switch (event)
{
case EVENT_GOT_FOCUS:
if(!CapHandle) //EVENT_GOT_FOCUS事件会被多次调用到,这里保证只会被创建一次
{
if(capGetDriverDescription(0,szDeviceName,MAX_PATH,szDeviceDesc,MAX_PATH)) //获得设备名称(如果有设备的话,返回真){
SetCtrlAttribute (panelHandle,PANEL_VIDEO, ATTR_LABEL_TEXT, szDeviceName); //显示设备名称
GetPanelAttribute (panelHandle,ATTR_SYSTEM_WINDOW_HANDLE,&Handle); //获得面板的句柄值
GetCtrlAttribute (panelHandle, PANEL_VIDEO, ATTR_LEFT, &CapLeft);
GetCtrlAttribute (panelHandle, PANEL_VIDEO, ATTR_TOP, &CapTop);
GetCtrlAttribute (panelHandle, PANEL_VIDEO, ATTR_HEIGHT, &CapHeight);
GetCtrlAttribute (panelHandle, PANEL_VIDEO, ATTR_WIDTH, &CapWidth);
CapHandle = (int) capCreateCaptureWindow ("TestCap",WS_CHILD | WS_VISIBLE ,CapLeft,
CapTop,CapWidth,CapHeight,(HWND)Handle,0); //创建视频御览窗口}
}
break;
case EVENT_LOST_FOCUS:
break;
case EVENT_CLOSE:
break;
case EVENT_PANEL_SIZING:
break;
default:
break;
}
return 0;
}