⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cameracode.cpp

📁 WEB CAM代码第二版,对CAM驱动有兴趣的朋友参考.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//======================================================================
// CameraCode.cpp 
//
// This file contains routines that interface with the WebCam driver.
//
// Copyright (C) 2005 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 "mjpeg2bmp.h"				// Routines for converting MJPEG frames

#include "camtest2.h"				// THis is only needed for debug zones

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);

BOOL fCont = TRUE;
HANDLE hCam = INVALID_HANDLE_VALUE;
HANDLE hThread;

extern BOOL fDraw;


#define NUMBUFFS       6
#define PREBUFFSIZE  512 

typedef struct {
	WORD wFormat;
	WORD wFrame;
	DWORD dwInterval;
	RECT rect;
	HDC hdc;
} THREADSTRUCT, *PTHREADSTRUCT;

// 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 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 ShutdownCamera ()
{
	if (fCont)
		StopStreaming ();

	if (hCam != INVALID_HANDLE_VALUE)
	{
		CloseHandle (hCam);
	}
	hCam = INVALID_HANDLE_VALUE;

	return 0;
}

//----------------------------------------------------------------------
// GetFeatureList
//
int 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 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 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 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 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 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 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.wFormat = wFormat;
	vf.wFrame = 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 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;
		
	if (hCam != INVALID_HANDLE_VALUE)
	{
		fCont = TRUE;
		hThread = CreateThread (NULL, 0, ReadFrameThread, (PVOID)pThd, 0, NULL);
		if (hThread)
		{
			Sleep (0);  //Let the thread start...

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -