WebCamLib.h
4.45 KB
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
190
191
192
193
194
195
196
//*****************************************************************************************
// File: WebCamLib.h
// Project: WebcamLib
// Author(s): John Conwell
// Gary Caldwell
//
// Declares the webcam DirectShow wrapper used by TouchlessLib
//*****************************************************************************************
#pragma once
using namespace System;
using namespace System::Runtime::InteropServices;
namespace WebCamLib
{
/// <summary>
/// Store webcam name, index
/// </summary>
public ref class CameraInfo
{
public:
property int index;
property String^ name;
};
/// <summary>
/// DirectShow wrapper around a web cam, used for image capture
/// </summary>
public ref class CameraMethods
{
public:
/// <summary>
/// Initializes information about all web cams connected to machine
/// </summary>
CameraMethods();
/// <summary>
/// Delegate used by DirectShow to pass back captured images from webcam
/// </summary>
delegate void CaptureCallbackDelegate(
int dwSize,
[MarshalAsAttribute(UnmanagedType::LPArray, ArraySubType = UnmanagedType::I1, SizeParamIndex = 0)] array<System::Byte>^ abData);
/// <summary>
/// Event callback to capture images from webcam
/// </summary>
event CaptureCallbackDelegate^ OnImageCapture;
/// <summary>
/// Retrieve information about a specific camera
/// Use the count property to determine valid indicies to pass in
/// </summary>
CameraInfo^ GetCameraInfo(int camIndex);
/// <summary>
/// Start the camera associated with the input handle
/// </summary>
void StartCamera(int camIndex, interior_ptr<int> width, interior_ptr<int> height);
/// <summary>
/// Set VideoProcAmpProperty
/// </summary>
void SetProperty(long lProperty, long lValue, bool bAuto);
/// <summary>
/// Stops the currently running camera and cleans up any global resources
/// </summary>
void Cleanup();
/// <summary>
/// Stops the currently running camera
/// </summary>
void StopCamera();
/// <summary>
/// Show the properties dialog for the specified webcam
/// </summary>
void DisplayCameraPropertiesDialog(int camIndex);
/// <summary>
/// Count of the number of cameras installed
/// </summary>
property int Count;
/// <summary>
/// Queries which camera is currently running via StartCamera(), -1 for none
/// </summary>
property int ActiveCameraIndex
{
int get()
{
return activeCameraIndex;
}
}
/// <summary>
/// IDisposable
/// </summary>
~CameraMethods();
protected:
/// <summary>
/// Finalizer
/// </summary>
!CameraMethods();
private:
/// <summary>
/// Pinned pointer to delegate for CaptureCallbackDelegate
/// Keeps the delegate instance in one spot
/// </summary>
GCHandle ppCaptureCallback;
/// <summary>
/// Initialize information about webcams installed on machine
/// </summary>
void RefreshCameraList();
/// <summary>
/// Has dispose already happened?
/// </summary>
bool disposed;
/// <summary>
/// Which camera is running? -1 for none
/// </summary>
int activeCameraIndex;
/// <summary>
/// Releases all unmanaged resources
/// </summary>
void CleanupCameraInfo();
/// <summary>
/// Setup the callback functionality for DirectShow
/// </summary>
HRESULT ConfigureSampleGrabber(IBaseFilter *pIBaseFilter);
HRESULT SetCaptureFormat(IBaseFilter* pCap, int width, int height);
};
// Forward declarations of callbacks
typedef void (__stdcall *PFN_CaptureCallback)(DWORD dwSize, BYTE* pbData);
PFN_CaptureCallback g_pfnCaptureCallback = NULL;
/// <summary>
/// Lightweight SampleGrabber callback interface
/// </summary>
class SampleGrabberCB : public ISampleGrabberCB
{
public:
SampleGrabberCB()
{
m_nRefCount = 0;
}
virtual HRESULT STDMETHODCALLTYPE SampleCB(double SampleTime, IMediaSample *pSample)
{
return E_FAIL;
}
virtual HRESULT STDMETHODCALLTYPE BufferCB(double SampleTime, BYTE *pBuffer, long BufferLen)
{
if (g_pfnCaptureCallback != NULL)
{
g_pfnCaptureCallback(BufferLen, pBuffer);
}
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject)
{
return E_FAIL; // Not a very accurate implementation
}
virtual ULONG STDMETHODCALLTYPE AddRef()
{
return ++m_nRefCount;
}
virtual ULONG STDMETHODCALLTYPE Release()
{
int n = --m_nRefCount;
if (n <= 0)
{
delete this;
}
return n;
}
private:
int m_nRefCount;
};
}