?? cameracode.cpp
字號:
//======================================================================
// CameraCode.cpp
//
// This file contains routines that interface with the WebCam driver.
//
// Copyright (C) 2006 Douglas Boling
//
//======================================================================
#include <windows.h> // For all that Windows stuff
#include <winioctl.h> // Needed for CTLCODE macro
#include <webcamsdk.h> // WebCam IOCTLs and structures
#include "CameraCode.h" // Defines for this file
#include <imaging.h>
#include "mjpeg2bmp.h" // Routines for converting MJPEG frames
#define MOTION_DETECT
// Returns number of elements
#define dim(x) (sizeof(x) / sizeof(x[0]))
/*
typedef struct {
HANDLE h;
PBYTE p;
} MMOBJSTRUCT, *PMMOBJSTRUCT;
int WriteJPEG (LPTSTR lpszName, PBYTE pData, int nSize);
int AllocMMObject (int nSize, PMMOBJSTRUCT obj);
int FreeMMObject (PMMOBJSTRUCT obj);
*/
DWORD WINAPI ReadFrameThread (PVOID pArg);
#define NUMBUFFS 5
#define PREBUFFSIZE 512
typedef struct {
BOOL fCont;
HANDLE hCam;
HANDLE hThread;
BOOL fDraw;
BOOL fDetectMotion;
DWORD dwFrameContext;
} CAMSTATE, *PCAMSTATE;
typedef struct {
PCAMSTATE pcs;
WORD wFormat;
WORD wFrame;
DWORD dwInterval;
RECT rect;
HDC hdc;
} THREADSTRUCT, *PTHREADSTRUCT;
// Returns information on a given format
int GetFormatInformation (PCAMSTATE, WORD wFormat, WORD wFrame, PFORMATPROPS pFmt);
// Label for the standard features supported by the Video spec. The
// order is important.
LPTSTR szFeatureLbls[] = {
TEXT("Unsupported"), // 0
TEXT("Scanning Mode"), // 1
TEXT("Auto-Exposure Mode"), // 2
TEXT("Auto-Exposure Priority"), // 3
TEXT("Exposure Time (Abs)"), // 4
TEXT("Exposure Time (Rel)"), // 5
TEXT("Focus (Abs)"), // 6
TEXT("Focus (Rel)"), // 7
TEXT("Iris (Abs)"), // 8
TEXT("Iris (Rel)"), // 9
TEXT("Zoom (Abs)"), // 10
TEXT("Zoom (Rel)"), // 11
TEXT("PanTilt (Abs)"), // 12
TEXT("PanTilt (Rel)"), // 13
TEXT("Roll (Abs)"), // 14
TEXT("Roll (Rel)"), // 15
TEXT("Focus, Auto"), // 16
TEXT("Privacy"), // 17
TEXT("Brightness"), // 18
TEXT("Contrast"), // 19
TEXT("Hue"), // 20
TEXT("Saturation"), // 21
TEXT("Sharpness"), // 22
TEXT("Gamma"), // 23
TEXT("White Balance Temp"), // 24
TEXT("White Balance Component"),// 25
TEXT("Backlight Compensation"), // 26
TEXT("Gain"), // 27
TEXT("Power Line Frequency"), // 28
TEXT("Hue-Auto"), // 29
TEXT("White Balance Temp-Auto"), // 30
TEXT("White Balance Component-Auto"),//31
TEXT("Digital Multiplier"), // 32
TEXT("Digital Multiplier Limit"),// 33
TEXT("Analog Video Standard"), // 34
TEXT("Analog Video Lock Status"), //35
};
//----------------------------------------------------------------------
// InitCamera - Opens the driver
//
DWORD InitCamera (LPTSTR pszCamName)
{
PCAMSTATE pcs = (PCAMSTATE)LocalAlloc (LPTR, sizeof (CAMSTATE));
if (pcs == 0)
return 0;
pcs->fCont = TRUE;
pcs->hCam = INVALID_HANDLE_VALUE;
pcs->fDraw = TRUE;
pcs->fDetectMotion = FALSE;
pcs->hThread = 0;
if (pcs->hCam == INVALID_HANDLE_VALUE)
{
// Open Driver
pcs->hCam = CreateFile (pszCamName, GENERIC_WRITE | GENERIC_READ,
0, NULL, OPEN_EXISTING, 0, NULL);
if (pcs->hCam == INVALID_HANDLE_VALUE)
{
LocalFree ((PVOID)pcs);
return 0;
}
}
return (DWORD)pcs;
}
//----------------------------------------------------------------------
// ShutdownCamera - Stops streaming and closes the driver
//
int ShutdownCamera (DWORD dwContext)
{
PCAMSTATE pcs = (PCAMSTATE)dwContext;
if (pcs->fCont)
StopStreaming (dwContext);
if (pcs->hCam != INVALID_HANDLE_VALUE)
{
CloseHandle (pcs->hCam);
}
LocalFree ((PVOID)dwContext);
return 0;
}
//----------------------------------------------------------------------
// InitConverters
//
HRESULT InitConverters (LPCWSTR pszExt)
{
return InitDisplayFrame (pszExt);
}
//----------------------------------------------------------------------
// FreeConverters
//
HRESULT FreeConverters (void)
{
return ReleaseDisplayFrame ();
}
//----------------------------------------------------------------------
// SetDrawState - Sets or clears drawing state flag
//
int SetDrawState (DWORD dwContext, BOOL fDraw)
{
PCAMSTATE pcs = (PCAMSTATE)dwContext;
pcs->fDraw = fDraw;
return 0;
}
//----------------------------------------------------------------------
// SetMotionDetectState - Sets or clears the motion detect state flag
//
int SetMotionDetectState (DWORD dwContext, BOOL fDetectMotion)
{
PCAMSTATE pcs = (PCAMSTATE)dwContext;
pcs->fDetectMotion = fDetectMotion;
return 0;
}
//----------------------------------------------------------------------
// GetFeatureList
//
int GetFeatureList (DWORD dwContext, PFEATUREPROPS pFeatures, DWORD *pdwSize)
{
int rc = 0;
BOOL f;
DWORD dwBytes;
PCAMSTATE pcs = (PCAMSTATE)dwContext;
if (pdwSize == 0)
return ERROR_INVALID_PARAMETER;
// See if they're asking for the number of supported features
if (pFeatures == 0)
{
//
// Get parameter list size
//
f = DeviceIoControl (pcs->hCam, IOCTL_CAMERA_DEVICE_QUERYPARAMETERARARY,
0, 0, 0, 0, pdwSize, NULL);
if (!f)
rc = GetLastError();
}
else
{
// Get parameter list
f = DeviceIoControl (pcs->hCam, IOCTL_CAMERA_DEVICE_QUERYPARAMETERARARY, 0, 0,
pFeatures, *pdwSize, &dwBytes, NULL);
if (!f)
rc = GetLastError();
}
return rc;
}
//----------------------------------------------------------------------
// GetFeatureText - Helper function that labels feature IDs
//
LPCTSTR GetFeatureText (DWORD dwFeatureID)
{
LPTSTR pstr = L"";
if (dwFeatureID < dim (szFeatureLbls))
pstr = szFeatureLbls[dwFeatureID];
return pstr;
}
//----------------------------------------------------------------------
// GetFeatureSetting - Queries a setting from the camera
//
int GetFeatureSetting (DWORD dwContext, DWORD dwFeature, DWORD *pVal)
{
BOOL f;
int rc = 0;
DWORD dwBytes;
PCAMSTATE pcs = (PCAMSTATE)dwContext;
if (pVal == 0)
return ERROR_INVALID_PARAMETER;
f = DeviceIoControl (pcs->hCam, IOCTL_CAMERA_DEVICE_QUERYPARAMETER,
&dwFeature, sizeof (DWORD),
pVal, sizeof (DWORD), &dwBytes, NULL);
if (!f)
rc = GetLastError();
return rc;
}
//----------------------------------------------------------------------
// SetFeatureSetting - Sets a feature on the camera
//
int SetFeatureSetting (DWORD dwContext, DWORD dwFeature, DWORD dwVal)
{
BOOL f;
int rc = 0;
DWORD dwBytes;
SETFEATURESTRUCT sfs;
PCAMSTATE pcs = (PCAMSTATE)dwContext;
sfs.cbSize = sizeof (SETFEATURESTRUCT);
sfs.dwFeatureID = dwFeature;
sfs.dwVal = dwVal;
f = DeviceIoControl (pcs->hCam, IOCTL_CAMERA_DEVICE_SETPARAMETER,
&sfs, sizeof (SETFEATURESTRUCT), 0, 0,
&dwBytes, NULL);
if (!f)
rc = GetLastError();
return rc;
}
//----------------------------------------------------------------------
// GetVideoFormats - Returns an array with the supported streaming
// video formats
//
int GetVideoFormats (DWORD dwContext, PFORMATPROPS pFormats, int *pnCount)
{
int i, rc, nCnt;
PCAMSTATE pcs = (PCAMSTATE)dwContext;
if (pnCount == 0)
return 0;
nCnt = *pnCount;
*pnCount = 0;
for (i = 0; i < nCnt; i++)
{
memset (&pFormats[i], 0, sizeof (FORMATPROPS));
rc = GetFormatInformation (pcs, 1, i+1, &pFormats[i]);
if (rc) break;
(*pnCount)++;
}
return 0;
}
//----------------------------------------------------------------------
// GetStillFormats - Returns an array with the supported still
// image formats
//
int GetStillFormats (DWORD dwContext, PFORMATPROPS pFormats, int *pnCount)
{
int rc = 0;
DWORD dwBytes;
BOOL f;
PCAMSTATE pcs = (PCAMSTATE)dwContext;
DWORD dwFormat = 1;
DWORD dw = *pnCount * sizeof (FORMATPROPS);
//
// Get information about a given video format
//
f = DeviceIoControl (pcs->hCam, IOCTL_CAMERA_DEVICE_QUERYSTILLFORMATS,
(LPVOID)&dwFormat, sizeof (DWORD),
pFormats, dw, &dwBytes, NULL);
if (!f)
rc = GetLastError();
*pnCount = dwBytes / sizeof (FORMATPROPS);
return rc;
}
//----------------------------------------------------------------------
// GetFormatInformation - Returns information about a specific streaming
// video format
//
int GetFormatInformation (PCAMSTATE pcs, WORD wFormat, WORD wFrame, PFORMATPROPS pFmt)
{
VIDFORMATSTRUCT vf;
FORMATPROPS fmtData;
DWORD dwBytes;
BOOL f;
int rc;
memset (&vf, 0, sizeof (vf));
vf.cbSize = sizeof (VIDFORMATSTRUCT);
vf.wFormatIndex = wFormat;
vf.wFrameIndex = wFrame;
//
// Get information about a given video format
//
f = DeviceIoControl (pcs->hCam, IOCTL_CAMERA_DEVICE_GETVIDEOFORMAT,
(LPVOID)&vf, sizeof (VIDFORMATSTRUCT),
&fmtData, sizeof (fmtData), &dwBytes, NULL);
rc = GetLastError();
if (!f)
{
return rc;
}
if (dwBytes != sizeof (FORMATPROPS))
{
return -1;
}
*pFmt = fmtData;
return rc;
}
//----------------------------------------------------------------------
// GetFirstStreamFrame - Starts streaming video and returns the first
// video frame.
//
int GetFirstStreamFrame (DWORD dwContext, WORD wFormat, WORD wFrame, DWORD dwInterval,
PBYTE *ppFrame, DWORD *pdwFrameSize, DWORD dwTimeout)
{
PCAMSTATE pcs = (PCAMSTATE)dwContext;
STARTVIDSTRUCT svStruct;
DWORD dwBytes;
BOOL f;
int rc = 0;
// Parameters needed to start a stream
dwBytes = 0;
svStruct.cbSize = sizeof (STARTVIDSTRUCT);
svStruct.wFormatIndex = wFormat;
svStruct.wFrameIndex = wFrame;
svStruct.dwInterval = dwInterval;
svStruct.dwNumBuffs = NUMBUFFS;
svStruct.dwPreBuffSize = PREBUFFSIZE;
svStruct.dwPostBuffSize = 0;
//
// Start the video stream
//
f = DeviceIoControl (pcs->hCam, IOCTL_CAMERA_DEVICE_STARTVIDEOSTREAM,
(LPVOID)&svStruct, sizeof (STARTVIDSTRUCT),
0, 0, &dwBytes, NULL);
if (f)
{
// Call the driver for a frame
GETFRAMESTRUCT gfsIn;
GETFRAMESTRUCTOUT gfsOut;
memset (&gfsIn, 0, sizeof (GETFRAMESTRUCT));
gfsIn.cbSize = sizeof (GETFRAMESTRUCT);
gfsIn.dwFlags = GETFRAMEFLAG_GET_LATESTFRAME;
gfsIn.dwFlags |= GETFRAMEFLAG_TIMEOUT_VALID;
gfsIn.dwTimeout = dwTimeout;
memset (&gfsOut, 0, sizeof (GETFRAMESTRUCTOUT));
gfsOut.cbSize = sizeof (GETFRAMESTRUCTOUT);
// Get the next frame of video
f = DeviceIoControl (pcs->hCam, IOCTL_CAMERA_DEVICE_GETNEXTVIDEOFRAME,
&gfsIn, sizeof (GETFRAMESTRUCT),
&gfsOut, sizeof(GETFRAMESTRUCTOUT), &dwBytes, NULL);
if (f)
{
*ppFrame = gfsOut.pFrameData;
*pdwFrameSize = gfsOut.dwFrameSize;
}
}
if (!f) rc = GetLastError();
return rc;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -