1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
#include <windows.h>
#include <vfw.h>
#include <stdio.h>
#pragma comment(lib,"vfw32.lib")
#pragma comment(lib,"gdi32.lib")
//Remember to Link to vfw32 Library, gdi32 Library
LRESULT CALLBACK WindowProc (HWND, UINT, WPARAM, LPARAM);
PBITMAPINFO CreateBitmapInfoStruct(HWND hwnd, HBITMAP hBmp);
void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC);
char szAppName [] = TEXT("Motion Tracker");
HWND camhwnd;
HDC hdcMem = CreateCompatibleDC(NULL);
PAINTSTRUCT ps;
HBITMAP hbm;
//Globals for the frame comparison
HBITMAP curFrame = NULL, lasFrame = NULL, bakFrame = NULL, bufFrame = NULL;
//WinMain -- Main Window
int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow )
{
HWND hwnd;
MSG msg;
WNDCLASS wc;
wc.style = CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(GetModuleHandle(NULL), IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
RegisterClass (&wc);
// Create the window
hwnd = CreateWindow (szAppName,
szAppName,
WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
775,525,0,0,
hInstance,0);
ShowWindow (hwnd,SW_SHOW);
UpdateWindow (hwnd);
while (GetMessage(&msg,0,0,0))
{
if (!IsDialogMessage(hwnd, &msg))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
return msg.wParam;
}
//Main Window Procedure WindowProc
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
//some buttons
HWND hButtStartCam;
HWND hButtStopCam;
HWND hButtGrabFrame;
switch (message) /* handle the messages */
{
case WM_CREATE:
{
hButtStartCam = CreateWindowEx(0,"BUTTON","Start Camera",WS_CHILD | WS_VISIBLE,
10,120,100,20,hwnd,(HMENU)1,hInstance, 0);
hButtStopCam = CreateWindowEx(0,"BUTTON","Stop Camera",WS_CHILD | WS_VISIBLE,
10,160,100,20,hwnd,(HMENU)2,hInstance, 0);
hButtGrabFrame = CreateWindowEx(0,"BUTTON","Grab Frame",WS_CHILD | WS_VISIBLE,
10,200,100,20,hwnd,(HMENU)3,hInstance, 0);
camhwnd = capCreateCaptureWindow ("camera window", WS_CHILD , 10, 10, 100, 100, hwnd, 0);
SendMessage(camhwnd,WM_CAP_DRIVER_CONNECT,0,0);
SendMessage(camhwnd,WM_CAP_DLG_VIDEOSOURCE,0,0);
break;
}
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case 1:
{
ShowWindow(camhwnd,SW_SHOW);
SendMessage(camhwnd,WM_CAP_DRIVER_CONNECT,0,0);
SendMessage(camhwnd, WM_CAP_SET_SCALE, true , 0);
SendMessage(camhwnd, WM_CAP_SET_PREVIEWRATE, 66, 0);
SendMessage(camhwnd, WM_CAP_SET_PREVIEW, true , 0);
break;
}
case 2:
{
ShowWindow(camhwnd,SW_HIDE);
SendMessage(camhwnd, WM_CAP_DRIVER_DISCONNECT, 0, 0);
break;
}
case 3:
{
//Grab a Frame
SendMessage(camhwnd, WM_CAP_GRAB_FRAME, 0, 0);
//Copy the frame we have just grabbed to the clipboard
SendMessage(camhwnd, WM_CAP_EDIT_COPY,0,0);
//Copy the clipboard image data to a HBITMAP object called hbm
if (OpenClipboard(camhwnd))
{
if (curFrame != NULL) {
lasFrame = curFrame;
}
curFrame = (HBITMAP) GetClipboardData(CF_BITMAP);
CloseClipboard();
}
InvalidateRect(hwnd,NULL,true); // tell the window it needs to redraw (sends a WM_PAINT message)
SendMessage(camhwnd,WM_CAP_DRIVER_CONNECT,0,0);
SendMessage(camhwnd, WM_CAP_SET_SCALE, true , 0);
SendMessage(camhwnd, WM_CAP_SET_PREVIEWRATE, 66, 0);
SendMessage(camhwnd, WM_CAP_SET_PREVIEW, true , 0);
break;
}
}
break;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC screen = BeginPaint(hwnd,&ps);
if (lasFrame != NULL) {
bufFrame = (HBITMAP)SelectObject(hdcMem, lasFrame);
SelectObject(hdcMem, lasFrame);
StretchBlt(screen, // destination DC
120, // x upper left
10, // y upper left
256, // destination width
192, // destination height
hdcMem, // you just created this above
0,
0, // x and y upper left
640, // source bitmap width
480, // source bitmap height
SRCCOPY); // raster operation
}
bufFrame = (HBITMAP)SelectObject(hdcMem, curFrame);
SelectObject(hdcMem, curFrame);
StretchBlt(screen, // destination DC
386, // x upper left
10, // y upper left
256, // destination width
192, // destination height
hdcMem, // you just created this above
0,
0, // x and y upper left
640, // source bitmap width
480, // source bitmap height
SRCCOPY); // raster operation
EndPaint( hwnd, &ps );
break;
}
case WM_DESTROY:
{
SelectObject(hdcMem, bufFrame);
DeleteDC(hdcMem);
DeleteObject(curFrame);
DeleteObject(lasFrame);
SendMessage(camhwnd, WM_CAP_DRIVER_DISCONNECT, 0, 0);
PostQuitMessage(0); /* send a WM_QUIT to the message queue */
break;
}
default: /* for messages that we don't deal with */
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
|