📄 mavisctl.cpp
字号:
// MavisCtl.cpp - by Robin Hewitt, 2004-2005
// http://www.robinhewitt.com/mavis
// This is free software. See license at the bottom
// of this file for details.
//
//////////////////////////////////////////////////////////////
// Defines the entry point for the MavisCtl application.
// Creates the application window and runs message loop.
//
#define USE_OPENCVINTERFACE
#include "MavisCtl.h"
#include <errno.h>
//
// Global Variables:
//
// Handles to use in WinAPI calls...
HINSTANCE ghInst; // current instance
TCHAR gszTitle[MAX_LOADSTRING]; // title bar text
TCHAR gszWindowClass[MAX_LOADSTRING]; // class name for the application window
HWND ghWnd = NULL; // main application window
TCHAR gszStatusClass[MAX_LOADSTRING]; // class name for the status window
char gStatusMsgBuf[1000]; // buffer for status-bar messages
HACCEL ghAccelTable; // keyboard shortcut mapping
HFONT ghfontApp=0; // application font
TEXTMETRIC gtm={0}; // text metrics for application font
// ...to look for for an object
HWND ghWndObjList = NULL; // list of Mavis objects
int gListInd = 0; // current index for ghWndObjList
char gObjNameBuf[1000]; // buffer for names of Mavis objects
HWND ghwndLookButton = NULL; // "Look Once" button
HWND ghwndLookContButton = NULL; // "Start/Stop Looking" button
// ...to calibrate the camera
HWND ghwndCalibrateButton = NULL; // "Calibrate" button
HWND ghwndInstrText = NULL; // Instruction text for camera calibration
HWND ghwndNextButton = NULL; // "Next" button for camera calibration steps
HWND ghwndStopButton = NULL; // "Stop" button - stops calibration
// ...to locate 3D horizonal lines
HWND ghwndHLinesButton = NULL; // "H-Lines" button
HWND ghwndHLinesText = NULL; // message text for HLines
HWND ghwndTravelDist = NULL; // input box for travel distance between frames
HWND ghwndMMText = NULL; // static control that says mm
// status bar instance
StatusBar * pStatus = NULL;
// Bitmap display objects
RECT rcBmp;
BITMAPINFOHEADER bih;
BYTE * pBmpBuffer = NULL;
int bufSize = 0;
// Mavis
Mavis * pMv = NULL;
HANDLE hMavisThread = NULL;
// Mavis dll
HINSTANCE mavisdll = NULL;
LOOKONCEPROC procLookOnce = NULL;
STARTLOOKPROC procStartLooking = NULL;
STOPLOOKPROC procStopLooking = NULL;
LOCATEHLINESPROC procLocateHLines = NULL;
NEXTLINEPROC procNextLine = NULL;
NEXTFRAMEPROC procNextFrame = NULL;
// structures for communicating with Mavis
CtlData_t ctlData;
ObjLoc_t objLoc;
HLineMetadata_t HLinesMetadata;
// separate thread for looking until there's a sighting
HANDLE hLookContThread = NULL;
// camera calibration
CalibrationHelper * pCH = NULL;
// app state
int gAppState;
//
// Foward declarations of functions included in this code module:
//
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
void OnClose();
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK StatusProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK StatusText(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int FrameCB(BYTE * pBuff, int nBytes);
void DisplayBitmap();
bool LoadMavisDll();
void UnloadMavisDll();
void SetAppState(int);
DWORD WINAPI RunMavis(LPVOID lpThreadData);
DWORD WINAPI LookUntilSighted(LPVOID lpThreadData);
//
// FUNCTION: WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
//
// PURPOSE: Program entry point.
//
// COMMENTS:
// Most of the initialization work is done in InitInstance()
// which is called from here. In this function, the window title
// and class name strings are loaded. These are used to verify
// that no other instance is running. Mavis will fail if another
// instance is running. This check gives us a chance to display
// a meaningful error message in that case.
//
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, gszTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MAVISCTL, gszWindowClass, MAX_LOADSTRING);
LoadString(hInstance, IDS_STATUS, gszStatusClass, MAX_LOADSTRING);
// only allow one instance to run
if( FindWindow(gszWindowClass, gszTitle) )
{
MessageBox(NULL, TEXT("Mavis Control Program is Already Running"), gszTitle, 0);
return FALSE;
}
// Initialize the application
if (!InitInstance (hInstance, nCmdShow)) return FALSE;
// Main message loop:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, ghAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
// Reached on WM_QUIT message
OnClose();
return msg.wParam;
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Initialize application data and save handles to window
// and instance.
//
// COMMENTS:
// In this function, we save the instance and window handles in global
// variables, initialize Mavis, and create and display the main program
// window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HDC hdc;
// set up the application window and store global variables
ghInst = hInstance;
MyRegisterClass(ghInst);
ghWnd = CreateWindow(gszWindowClass, gszTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, 705, 400, NULL, NULL, hInstance, NULL);
if (!ghWnd)
{
MessageBox(NULL, TEXT("Failed to create client window"), gszTitle, 0);
return FALSE;
}
ghAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MAVISCTL);
// get some GDI data
ghfontApp = (HFONT)GetStockObject(ANSI_VAR_FONT);
hdc = GetDC(NULL);
SelectObject(hdc, ghWnd);
GetTextMetrics(hdc, >m);
ReleaseDC(NULL, hdc);
// create the object list
ghWndObjList = CreateWindow(
TEXT("listbox"),
NULL,
WS_CHILD | WS_VISIBLE | LBS_STANDARD,
340, 20, 200, 2*gtm.tmHeight,
ghWnd,
(HMENU)LIST_OBJS,
ghInst,
NULL
);
SendMessage(ghWndObjList, LB_ADDSTRING, 0, (LPARAM)HOMEBASE_OBJ_NAME);
SendMessage(ghWndObjList, LB_ADDSTRING, 1, (LPARAM)MOTION_DETECTOR_NAME);
//SendMessage(ghWndObjList, LB_ADDSTRING, 2, (LPARAM)MPI_FACE_OBJ_NAME);
//SendMessage(ghWndObjList, LB_ADDSTRING, 3, (LPARAM)"Bruce");
SendMessage(ghWndObjList, LB_ADDSTRING, 3, (LPARAM)HAAR_FACE_DETECTOR_NAME);
SendMessage(ghWndObjList, LB_SETCURSEL, (WPARAM)gListInd, (LPARAM)0);
SendMessage(ghWndObjList, LB_GETTEXT, (WPARAM)gListInd, (LPARAM)gObjNameBuf);
// Layout the top row with buttons:
// create the "Look Once" button
ghwndLookButton = CreateWindow(
TEXT("button"), // pointer to registered class name
TEXT("Look Once"), // pointer to window name
WS_CHILD|BS_PUSHBUTTON|BS_TEXT|WS_VISIBLE, // window style
340, // horizontal position of window
20 + 2*gtm.tmHeight, // vertical position of window
12*gtm.tmAveCharWidth, // window width
2*gtm.tmHeight, // window height
ghWnd, // handle to parent window
(HMENU)BTN_LOOK_ONCE, // handle to child-window identifier
ghInst, // handle to application instance
NULL // pointer to window-creation data
);
// create the "Start/Stop Looking" button
ghwndLookContButton = CreateWindow(
TEXT("button"), // pointer to registered class name
TEXT("Look Cont."), // pointer to window name
WS_CHILD|BS_PUSHBUTTON|BS_TEXT|WS_VISIBLE, // window style
340 + 4 + 12*gtm.tmAveCharWidth, // horizontal position of window
20 + 2*gtm.tmHeight, // vertical position of window
12*gtm.tmAveCharWidth, // window width
2*gtm.tmHeight, // window height
ghWnd, // handle to parent window
(HMENU)BTN_LOOK_CONT, // handle to child-window identifier
ghInst, // handle to application instance
NULL // pointer to window-creation data
);
// create the "Calibrate" button
ghwndCalibrateButton = CreateWindow(
TEXT("button"), // pointer to registered class name
TEXT("Calibrate"), // pointer to window name
WS_CHILD|BS_PUSHBUTTON|BS_TEXT|WS_VISIBLE, // window style
340 + 8 + 24*gtm.tmAveCharWidth, // horizontal position of window
20 + 2*gtm.tmHeight, // vertical position of window
12*gtm.tmAveCharWidth, // window width
2*gtm.tmHeight, // window height
ghWnd, // handle to parent window
(HMENU)BTN_CALIBRATE, // handle to child-window identifier
ghInst, // handle to application instance
NULL // pointer to window-creation data
);
// create the "H-Lines" button
ghwndHLinesButton = CreateWindow(
TEXT("button"), // pointer to registered class name
TEXT("H-Lines"), // pointer to window name
WS_CHILD|BS_PUSHBUTTON|BS_TEXT|WS_VISIBLE, // window style
340 + 12 + 36*gtm.tmAveCharWidth, // horizontal position of window
20 + 2*gtm.tmHeight, // vertical position of window
12*gtm.tmAveCharWidth, // window width
2*gtm.tmHeight, // window height
ghWnd, // handle to parent window
(HMENU)BTN_HLINES, // handle to child-window identifier
ghInst, // handle to application instance
NULL // pointer to window-creation data
);
// Layout the area beneath the buttons:
// create the "instruction" text area - for calibration
ghwndInstrText = CreateWindow(
TEXT("static"), // pointer to registered class name
TEXT(""), // pointer to window name
WS_CHILD|SS_LEFT|SS_SUNKEN|WS_VISIBLE, // window style
345, // horizontal position of window
20 + 5*gtm.tmHeight, // vertical position of window
200, // window width
10*gtm.tmHeight, // window height
ghWnd, // handle to parent window
(HMENU)TXT_INSTR, // handle to child-window identifier
ghInst, // handle to application instance
NULL // pointer to window-creation data
);
SetBkColor( GetDC(ghwndInstrText), GetBkColor(GetDC(ghWnd)) );
// create the message area for locating HLines
ghwndHLinesText = CreateWindow(
TEXT("static"), // pointer to registered class name
TEXT(""), // pointer to window name
WS_CHILD|SS_LEFT|SS_SUNKEN|WS_VISIBLE, // window style
345, // horizontal position of window
20 + 2*gtm.tmHeight, // vertical position of window
240, // window width
10*gtm.tmHeight, // window height
ghWnd, // handle to parent window
(HMENU)TXT_INSTR, // handle to child-window identifier
ghInst, // handle to application instance
NULL // pointer to window-creation data
);
SetBkColor( GetDC(ghwndHLinesText), GetBkColor(GetDC(ghWnd)) );
// create the input box for travel distance when locating HLines
ghwndTravelDist = CreateWindow(
TEXT("edit"), // pointer to registered class name
TEXT(""), // pointer to window name
WS_CHILD|WS_VISIBLE|WS_BORDER|ES_LEFT, // window style
345, // horizontal position of window
25 + 13*gtm.tmHeight, // vertical position of window
80, // window width
1.3 * gtm.tmHeight, // window height
ghWnd, // handle to parent window
(HMENU)EDIT_HLINES, // handle to child-window identifier
ghInst, // handle to application instance
NULL // pointer to window-creation data
);
// add a "mm" label after the intput box
ghwndMMText = CreateWindow(
TEXT("static"), // pointer to registered class name
TEXT("mm"), // pointer to window name
WS_CHILD|SS_CENTER|WS_VISIBLE, // window style
345 + 83, // horizontal position of window
25 + 13*gtm.tmHeight, // vertical position of window
2*gtm.tmMaxCharWidth, // window width
1.3 * gtm.tmHeight, // window height
ghWnd, // handle to parent window
(HMENU)TXT_INSTR, // handle to child-window identifier
ghInst, // handle to application instance
NULL // pointer to window-creation data
);
SetBkColor( GetDC(ghwndMMText), 0x00ffffff );
// create the "Next" button
ghwndNextButton = CreateWindow(
TEXT("button"), // pointer to registered class name
TEXT("Next"), // pointer to window name
WS_CHILD|BS_PUSHBUTTON|BS_TEXT|WS_VISIBLE, // window style
345, // horizontal position of window
25 + 15*gtm.tmHeight, // vertical position of window
6*gtm.tmAveCharWidth, // window width
2*gtm.tmHeight, // window height
ghWnd, // handle to parent window
(HMENU)BTN_NEXT, // handle to child-window identifier
ghInst, // handle to application instance
NULL // pointer to window-creation data
);
// create the "Stop" button
ghwndStopButton = CreateWindow(
TEXT("button"), // pointer to registered class name
TEXT("Stop"), // pointer to window name
WS_CHILD|BS_PUSHBUTTON|BS_TEXT|WS_VISIBLE, // window style
355 + 6*gtm.tmAveCharWidth, // horizontal position of window
25 + 15*gtm.tmHeight, // vertical position of window
6*gtm.tmAveCharWidth, // window width
2*gtm.tmHeight, // window height
ghWnd, // handle to parent window
(HMENU)BTN_STOP, // handle to child-window identifier
ghInst, // handle to application instance
NULL // pointer to window-creation data
);
// Status Bar
try
{
pStatus = new StatusBar(ghInst, ghWnd, (WNDPROC)StatusProc, (WNDPROC)StatusText);
}
catch(std::runtime_error& e)
{
MessageBox(NULL, TEXT( e.what() ), gszTitle, 0);
return FALSE;
}
// prepare bitmap-display objects
SetRect(&rcBmp, 10, 20, 330, 260);
memset( &bih, 0, sizeof( bih ) );
bih.biSize = sizeof( bih );
bih.biWidth = 320;
bih.biHeight = 240;
bih.biPlanes = 1;
bih.biBitCount = 24;
// initialize Mavis
try
{
pMv = new Mavis(FrameCB);
}
catch(MavisErr& e)
{
string msg = "Error: ";
msg += e.getMsg();
MessageBox(NULL, TEXT(msg.c_str()), gszTitle, 0);
return FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -