📄 wab_sensor.c
字号:
#include <stdio.h>
#include <windows.h>//需要的API函数实际在winbase.h中定义
#include <cvirte.h> /* Needed if linking in external compiler; harmless otherwise */
#include <formatio.h>
#include <userint.h>
#include "wab_sensor.h"
//设置所需要摄像头的常量
#define WM_CAP_START WM_USER //开始
#define WM_CAP_STOP WM_CAP_START + 68 //停止
#define WM_CAP_DRIVER_CONNECT WM_CAP_START + 10 //连接
#define WM_CAP_DRIVER_DISCONNECT WM_CAP_START + 11 //断开
#define WM_CAP_SAVEDIB WM_CAP_START + 25
#define WM_CAP_GRAB_FRAME WM_CAP_START + 60
#define WM_CAP_SEQUENCE WM_CAP_START + 62
#define WM_CAP_FILE_SET_CAPTURE_FILEA WM_CAP_START + 20 //保存至文件
#define WM_CAP_SEQUENCE_NOFILE WM_CAP_START + 63
#define WM_CAP_SET_OVERLAY WM_CAP_START + 51
#define WM_CAP_SET_PREVIEW WM_CAP_START + 50
#define WM_CAP_SET_CALLBACK_VIDEOSTREAM WM_CAP_START + 6
#define WM_CAP_SET_CALLBACK_ERROR WM_CAP_START + 2
#define WM_CAP_SET_CALLBACK_STATUSA WM_CAP_START + 3
#define WM_CAP_SET_CALLBACK_FRAME WM_CAP_START + 5
#define WM_CAP_SET_SCALE WM_CAP_START + 53
#define WM_CAP_SET_PREVIEWRATE WM_CAP_START + 52
#define WM_CAP_COPYDIB WM_CAP_START + 30 //复制图片
//定义显示图象的位置以及大小
int width;
int height;
int top;
int left;
//定义面板句柄
static int handle;
//定义摄像头句柄
static int result;
static int panelHandle;
/*-------------------------------------------------------------------------------------------------------*/
typedef int DLLEXPORT (*DLLCdeclFunction)(char *, int, int, int, int, int, int, int);//定义函数指针
/*-------------------------------------------------------------------------------------------------------*/
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "wab_sensor.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
//取得默认制式下的图象位置及大小
GetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_LEFT, &left);
GetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_TOP, &top);
GetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_HEIGHT, &height);
GetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_WIDTH, &width);
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}
//退出按钮
int CVICALLBACK QuitCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event) {
case EVENT_COMMIT:
QuitUserInterface (0);
break;
}
return 0;
}
//关闭按钮
int CVICALLBACK PanelCallback (int panel, int event, void *callbackData,
int eventData1, int eventData2)
{
int key_vk;
switch (event)
{
case EVENT_GOT_FOCUS:
break;
case EVENT_LOST_FOCUS:
break;
case EVENT_CLOSE:
QuitUserInterface (0);
break;
case EVENT_KEYPRESS:
//if(portopen == 1) {
key_vk = eventData1 & VAL_VKEY_MASK;
if(key_vk == 0x0300) { //Esc
SetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_WIDTH, width);
SetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_HEIGHT, height);
SetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_TOP, top);
SetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_LEFT, left);
SendMessage((HWND)result, WM_CAP_DRIVER_DISCONNECT, 0, 0);
startCB(panel, PANEL_CANVAS_TV, EVENT_COMMIT, NULL, 0, 0);
}
//}
break;
}
return 0;
}
//开始按钮
int CVICALLBACK startCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int width_1;
int height_1;
int top_1;
int left_1;
/*-------------------------------------------------------------------------------------------------------*/
HMODULE hinstLib;
DLLCdeclFunction DLLFunction;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
/*-------------------------------------------------------------------------------------------------------*/
switch (event) {
case EVENT_COMMIT:
//获得面板句柄
GetPanelAttribute (panelHandle, ATTR_SYSTEM_WINDOW_HANDLE, &handle);
//获得屏幕中图像要显示的位置
GetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_LEFT, &left_1);
GetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_TOP, &top_1);
GetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_HEIGHT, &height_1);
GetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_WIDTH, &width_1);
//打开摄像头
hinstLib = LoadLibrary("avicap32.dll"); //装载动态链接库avicap32.dll
if (hinstLib != NULL) { //成功装载动态链接库mydll.dll
DLLFunction = (DLLCdeclFunction)GetProcAddress(hinstLib, (LPCSTR)"capCreateCaptureWindowA");//取函数指针地址
if (fRunTimeLinkSuccess = (DLLFunction != NULL)) { //dll中有函数MyDLLCdeclFunction()
result = (int)DLLFunction ("", WS_CHILD | WS_VISIBLE, left_1, top_1, width_1, height_1, handle, 0);//调用dll函数!!!
if(!result)
MessagePopup ("wab_sensor", "open the sensor failed!");
//else
//MessagePopup ("CVI MessagePopup ", "result not null");
} else {
MessagePopup("wab_sensor", "the function you call is not in the dll!");
}
//fFreeResult = FreeLibrary(hinstLib);//卸载动态链接库mydll.dll
//若在此调用了FreeLibrary函数,则会出现下面的错误:(暂时搞不清楚为什么)
// FATAL RUN-TIME ERROR: "wab_sensor.c", line 132, col 13, thread id 0x00000D84: The program has caused a 'General Protection' fault at 001B:73AF7FC5.
}
//if (! fRunTimeLinkSuccess)
// MessagePopup ("wab_sensor", "free dll failed");
//设置摄像头函数
SendMessage((HWND)result, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
SendMessage((HWND)result, WM_CAP_SET_CALLBACK_ERROR, 0, 0);
SendMessage((HWND)result, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);
SendMessage((HWND)result, WM_CAP_DRIVER_CONNECT, 0, 0);
SendMessage((HWND)result, WM_CAP_SET_SCALE, 1, 0);
SendMessage((HWND)result, WM_CAP_SET_PREVIEWRATE, 66, 0);
SendMessage((HWND)result, WM_CAP_SET_OVERLAY, 1, 0);
SendMessage((HWND)result, WM_CAP_SET_PREVIEW, 1, 0);
break;
}
return 0;
}
//停止按钮
int CVICALLBACK stopCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event) {
case EVENT_COMMIT:
if (result != 0) {
//断开连接
SendMessage((HWND)result, WM_CAP_DRIVER_DISCONNECT, 0, 0);
}
break;
}
return 0;
}
//保存BMP按钮
int CVICALLBACK recordbmp (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
// int available;
//定义BMP位图ID号
int bitmapID;
//定义文件存储路径
char path[MAX_PATHNAME_LEN];
//定义静态变量,设置循环次数
static int i;
switch (event) {
case EVENT_COMMIT:
/* //此代码为将图片存入剪切板
if (result != 0) {
SendMessage((HWND)result, WM_CAP_GRAB_FRAME, 0, 0);
SendMessage((HWND)result, WM_CAP_COPYDIB, 0, 0);
ClipboardGetBitmap (&bitmapID, &available);
if (available ==1) {
CanvasClear (panelHandle, PANEL_CANVAS, VAL_ENTIRE_OBJECT);
CanvasDrawBitmap (panelHandle, PANEL_CANVAS, bitmapID, VAL_ENTIRE_OBJECT, VAL_ENTIRE_OBJECT);
}
DiscardBitmap (bitmapID);
}
*/
//保存位图
if (FileSelectPopup ("", "*.bmp", "*.bmp; *.jpg", "保存", VAL_SAVE_BUTTON, 0, 0, 1, 1, path) > 0) {
SendMessage((HWND)result,WM_CAP_SAVEDIB,0,(LPARAM)path);
//获得BMP位图ID
GetBitmapFromFile (path, &bitmapID);
switch (i) {
//设置三个位图浏览器
case 0:
CanvasDrawBitmap (panelHandle, PANEL_CANVAS_SNAP0, bitmapID, VAL_ENTIRE_OBJECT, VAL_ENTIRE_OBJECT);
break;
case 1:
CanvasDrawBitmap (panelHandle, PANEL_CANVAS_SNAP1, bitmapID, VAL_ENTIRE_OBJECT, VAL_ENTIRE_OBJECT);
break;
case 2:
CanvasDrawBitmap (panelHandle, PANEL_CANVAS_SNAP2, bitmapID, VAL_ENTIRE_OBJECT, VAL_ENTIRE_OBJECT);
break;
}
//删除BMP位图ID
DiscardBitmap (bitmapID);
//循环累加,共三次,三次之后回零
i++;
if (i == 3) {
i = 0;
}
}
break;
}
return 0;
}
//开始录像按钮
int CVICALLBACK recordstream (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
char path[MAX_PATHNAME_LEN];
switch (event) {
case EVENT_COMMIT:
//保存录像
if (FileSelectPopup ("", "*.avi", "*.avi", "保存", VAL_SAVE_BUTTON, 0, 0, 1, 1, path) > 0) {
SendMessage((HWND)result,WM_CAP_FILE_SET_CAPTURE_FILEA,0,(LPARAM)path);
SendMessage((HWND)result, WM_CAP_SEQUENCE, 0, 0);
}
break;
}
return 0;
}
//停止录像按钮
int CVICALLBACK stopstream (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event) {
case EVENT_COMMIT:
SendMessage((HWND)result, WM_CAP_STOP, 0, 0);
break;
}
return 0;
}
//显示制式设置
int CVICALLBACK method (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int kind;
int width_n;
int height_n;
int top_n;
int left_n;
switch (event) {
case EVENT_COMMIT:
//获得制式种类
GetCtrlVal (panelHandle, PANEL_RING, &kind);
switch (kind) {
case 0:
//160*120制式
SetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_WIDTH, 160);
SetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_HEIGHT, 120);
break;
case 1:
//176*144制式
SetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_WIDTH, 176);
SetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_HEIGHT, 144);
break;
case 2:
//320*240制式
SetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_WIDTH, 320);
SetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_HEIGHT, 240);
break;
case 3:
//352*288制式
SetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_WIDTH, 352);
SetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_HEIGHT, 288);
break;
case 4:
//1默认制式
SetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_WIDTH, width);
SetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_HEIGHT, height);
break;
}
//获得屏幕中图像要显示的位置
GetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_LEFT, &left_n);
GetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_TOP, &top_n);
GetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_HEIGHT, &height_n);
GetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_WIDTH, &width_n);
//重新定位
SetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_TOP, top + (height-height_n)/2);
SetCtrlAttribute (panelHandle, PANEL_CANVAS_TV, ATTR_LEFT, left + (width-width_n)/2);
//断开连接
SendMessage((HWND)result, WM_CAP_DRIVER_DISCONNECT, 0, 0);
//调用开始按钮事件
startCB(panel, PANEL_CANVAS_TV, EVENT_COMMIT, NULL, 0, 0);
break;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -