📄 cameracode.cpp
字号:
#include "StdAfx.h"
#include "CameraCode.h"
typedef struct {
CCameraCode *pCamercode;
WORD wFormat;
WORD wFrame;
DWORD dwInterval;
RECT rect;
HDC hdc;
} THREADSTRUCT, *PTHREADSTRUCT;
CCameraCode::CCameraCode(void)
{
fCont = TRUE;
hCam = INVALID_HANDLE_VALUE;
pMjpe2bmp=new CMjpeg2bmp;
fDraw=FALSE;
}
CCameraCode::~CCameraCode(void)
{
}
// 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
//
int CCameraCode::InitCamera ()
{
int rc = 0;
if (hCam == INVALID_HANDLE_VALUE)
{
// Open Driver
hCam = CreateFile (TEXT("CAM1:"), GENERIC_WRITE | GENERIC_READ,
0, NULL, OPEN_EXISTING, 0, NULL);
if (hCam == INVALID_HANDLE_VALUE)
{
rc = GetLastError();
//printf ("can't open file rc = %d\r\n", rc);
}
}
return rc;
}
//----------------------------------------------------------------------
// ShutdownCamera - Stops streaming and closes the driver
//
int CCameraCode::ShutdownCamera ()
{
if (fCont)
StopStreaming ();
if (hCam != INVALID_HANDLE_VALUE)
{
CloseHandle (hCam);
}
hCam = INVALID_HANDLE_VALUE;
return 0;
}
//----------------------------------------------------------------------
// GetFeatureList
//
int CCameraCode::GetFeatureList (PFEATUREPROPS pFeatures, DWORD *pdwSize)
{
int rc = 0;
BOOL f;
DWORD dwBytes;
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 (hCam, IOCTL_CAMERA_DEVICE_QUERYPARAMETERARARY,
0, 0, 0, 0, pdwSize, NULL);
if (!f)
rc = GetLastError();
//printf ("DeviceIoControl returned %d %d\r\n", f, rc);
}
else
{
// Get parameter list
f = DeviceIoControl (hCam, IOCTL_CAMERA_DEVICE_QUERYPARAMETERARARY, 0, 0,
pFeatures, *pdwSize, &dwBytes, NULL);
if (!f)
rc = GetLastError();
//printf ("DeviceIoControl IOCTL_CAMERA_DEVICE_QUERYPARAMETERARARY returned %d %d\r\n", f, rc);
}
return rc;
}
//----------------------------------------------------------------------
// GetFeatureText - Helper function that labels feature IDs
//
LPCTSTR CCameraCode::GetFeatureText (DWORD dwFeatureID)
{
LPTSTR pstr = L"";
if (dwFeatureID < dim (szFeatureLbls))
pstr = szFeatureLbls[dwFeatureID];
return pstr;
}
//----------------------------------------------------------------------
// GetFeatureSetting - Queries a setting from the camera
//
int CCameraCode::GetFeatureSetting (DWORD dwFeature, DWORD *pVal)
{
BOOL f;
int rc = 0;
DWORD dwBytes;
if (pVal == 0)
return ERROR_INVALID_PARAMETER;
f = DeviceIoControl (hCam, IOCTL_CAMERA_DEVICE_QUERYPARAMETER,
&dwFeature, sizeof (DWORD),
pVal, sizeof (DWORD), &dwBytes, NULL);
if (!f)
rc = GetLastError();
//printf ("DeviceIoControl IOCTL_CAMERA_DEVICE_QUERYPARAMETER returned %d %d\r\n", f, rc);
return rc;
}
//----------------------------------------------------------------------
// SetFeatureSetting - Sets a feature on the camera
//
int CCameraCode::SetFeatureSetting (DWORD dwFeature, DWORD dwVal)
{
BOOL f;
int rc = 0;
DWORD dwBytes;
SETFEATURESTRUCT sfs;
sfs.cbSize = sizeof (SETFEATURESTRUCT);
sfs.dwFeatureID = dwFeature;
sfs.dwVal = dwVal;
f = DeviceIoControl (hCam, IOCTL_CAMERA_DEVICE_SETPARAMETER,
&sfs, sizeof (SETFEATURESTRUCT), 0, 0,
&dwBytes, NULL);
if (!f)
rc = GetLastError();
//printf ("DeviceIoControl IOCTL_CAMERA_DEVICE_SETPARAMETER returned %d %d\r\n", f, rc);
return rc;
}
//----------------------------------------------------------------------
// GetVideoFormats - Returns an array with the supported streaming
// video formats
//
int CCameraCode::GetVideoFormats (PFORMATPROPS pFormats, int *pnCount)
{
int i, rc, nCnt;
if (pnCount == 0)
return 0;
nCnt = *pnCount;
*pnCount = 0;
for (i = 0; i < nCnt; i++)
{
memset (&pFormats[i], 0, sizeof (FORMATPROPS));
rc = GetFormatInformation (1, i+1, &pFormats[i]);
if (rc) break;
(*pnCount)++;
}
return 0;
}
//----------------------------------------------------------------------
// GetStillFormats - Returns an array with the supported still
// image formats
//
int CCameraCode::GetStillFormats (PFORMATPROPS pFormats, int *pnCount)
{
int rc = 0;
DWORD dwBytes;
BOOL f;
DWORD dwFormat = 1;
DWORD dw = *pnCount * sizeof (FORMATPROPS);
//
// Get information about a given video format
//
f = DeviceIoControl (hCam, IOCTL_CAMERA_DEVICE_QUERYSTILLFORMATS,
(LPVOID)&dwFormat, sizeof (DWORD),
pFormats, dw, &dwBytes, NULL);
rc = GetLastError();
//printf ("DeviceIoControl IOCTL_CAMERA_DEVICE_QUERYSTILLFORMATS returned %d %d\r\n", f, rc);
if (!f)
{
//printf ("Failure to get format data rc = %d\r\n", rc);
return rc;
}
*pnCount = dwBytes / sizeof (FORMATPROPS);
return rc;
}
//----------------------------------------------------------------------
// GetFormatInformation - Returns information about a specific streaming
// video format
//
int CCameraCode::GetFormatInformation (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 (hCam, IOCTL_CAMERA_DEVICE_GETVIDEOFORMAT,
(LPVOID)&vf, sizeof (VIDFORMATSTRUCT),
&fmtData, sizeof (fmtData), &dwBytes, NULL);
rc = GetLastError();
//printf ("DeviceIoControl IOCTL_CAMERA_DEVICE_GETVIDEOFORMAT returned %d %d\r\n", f, rc);
if (!f)
{
//printf ("Failure to get format data rc = %d\r\n", rc);
return rc;
}
if (dwBytes != sizeof (FORMATPROPS))
{
//printf ("Unexpected size for return data\r\n");
return -1;
}
*pFmt = fmtData;
return rc;
}
//----------------------------------------------------------------------
// StartStreaming - Starts streaming video to a device context at
// the specified rectangle.
//
// If the .right and .bot fields of the rect structure are zero, the
// image will be sized based on its original format. If the rect
// structure is fully filled, the image will be sized to fit the rectangle
//
int CCameraCode::StartStreaming (HDC hdc, RECT *prect, WORD wFormat, WORD wFrame, DWORD dwInterval)
{
PTHREADSTRUCT pThd = (PTHREADSTRUCT)LocalAlloc (LPTR, sizeof (THREADSTRUCT));
if (!pThd) return ERROR_NOT_ENOUGH_MEMORY;
// Load parameter passing struct
pThd->wFormat = wFormat;
pThd->wFrame = wFrame;
pThd->rect = *prect;
pThd->dwInterval = dwInterval;
pThd->hdc = hdc;
pThd->pCamercode =this;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -