forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathVideoCaptureDeviceForm.cs
354 lines (303 loc) · 12.6 KB
/
VideoCaptureDeviceForm.cs
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// AForge Direct Show Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © AForge.NET, 2009-2013
// contacts@aforgenet.com
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AForge.Video.DirectShow;
namespace AForge.Video.DirectShow
{
/// <summary>
/// Local video device selection form.
/// </summary>
///
/// <remarks><para>The form provides a standard way of selecting local video
/// device (USB web camera, capture board, etc. - anything supporting DirectShow
/// interface), which can be reused across applications. It allows selecting video
/// device, video size and snapshots size (if device supports snapshots and
/// <see cref="ConfigureSnapshots">user needs them</see>).</para>
///
/// <para><img src="img/video/VideoCaptureDeviceForm.png" width="478" height="205" /></para>
/// </remarks>
///
public partial class VideoCaptureDeviceForm : Form
{
// collection of available video devices
private FilterInfoCollection videoDevices;
// selected video device
private VideoCaptureDevice videoDevice;
// supported capabilities of video and snapshots
private Dictionary<string, VideoCapabilities> videoCapabilitiesDictionary = new Dictionary<string, VideoCapabilities>( );
private Dictionary<string, VideoCapabilities> snapshotCapabilitiesDictionary = new Dictionary<string, VideoCapabilities>( );
// available video inputs
private VideoInput[] availableVideoInputs = null;
// flag telling if user wants to configure snapshots as well
private bool configureSnapshots = false;
/// <summary>
/// Specifies if snapshot configuration should be done or not.
/// </summary>
///
/// <remarks><para>The property specifies if the dialog form should
/// allow configuration of snapshot sizes (if selected video source supports
/// snapshots). If the property is set to <see langword="true"/>, then
/// the form will provide additional combo box enumerating supported
/// snapshot sizes. Otherwise the combo boxes will be hidden.
/// </para>
///
/// <para>If the property is set to <see langword="true"/> and selected
/// device supports snapshots, then <see cref="VideoCaptureDevice.ProvideSnapshots"/>
/// property of the <see cref="VideoDevice">configured device</see> is set to
/// <see langword="true"/>.</para>
///
/// <para>Default value of the property is set to <see langword="false"/>.</para>
/// </remarks>
///
public bool ConfigureSnapshots
{
get { return configureSnapshots; }
set
{
configureSnapshots = value;
snapshotsLabel.Visible = value;
snapshotResolutionsCombo.Visible = value;
}
}
/// <summary>
/// Provides configured video device.
/// </summary>
///
/// <remarks><para>The property provides configured video device if user confirmed
/// the dialog using "OK" button. If user canceled the dialog, the property is
/// set to <see langword="null"/>.</para></remarks>
///
public VideoCaptureDevice VideoDevice
{
get { return videoDevice; }
}
private string videoDeviceMoniker = string.Empty;
private Size captureSize = new Size( 0, 0 );
private Size snapshotSize = new Size( 0, 0 );
private VideoInput videoInput = VideoInput.Default;
/// <summary>
/// Moniker string of the selected video device.
/// </summary>
///
/// <remarks><para>The property allows to get moniker string of the selected device
/// on form completion or set video device which should be selected by default on
/// form loading.</para></remarks>
///
public string VideoDeviceMoniker
{
get { return videoDeviceMoniker; }
set { videoDeviceMoniker = value; }
}
/// <summary>
/// Video frame size of the selected device.
/// </summary>
///
/// <remarks><para>The property allows to get video size of the selected device
/// on form completion or set the size to be selected by default on form loading.</para>
/// </remarks>
///
public Size CaptureSize
{
get { return captureSize; }
set { captureSize = value; }
}
/// <summary>
/// Snapshot frame size of the selected device.
/// </summary>
///
/// <remarks><para>The property allows to get snapshot size of the selected device
/// on form completion or set the size to be selected by default on form loading
/// (if <see cref="ConfigureSnapshots"/> property is set <see langword="true"/>).</para>
/// </remarks>
public Size SnapshotSize
{
get { return snapshotSize; }
set { snapshotSize = value; }
}
/// <summary>
/// Video input to use with video capture card.
/// </summary>
///
/// <remarks><para>The property allows to get video input of the selected device
/// on form completion or set it to be selected by default on form loading.</para></remarks>
///
public VideoInput VideoInput
{
get { return videoInput; }
set { videoInput = value; }
}
/// <summary>
/// Initializes a new instance of the <see cref="VideoCaptureDeviceForm"/> class.
/// </summary>
///
public VideoCaptureDeviceForm( )
{
InitializeComponent( );
ConfigureSnapshots = false;
// show device list
try
{
// enumerate video devices
videoDevices = new FilterInfoCollection( FilterCategory.VideoInputDevice );
if ( videoDevices.Count == 0 )
throw new ApplicationException( );
// add all devices to combo
foreach ( FilterInfo device in videoDevices )
{
devicesCombo.Items.Add( device.Name );
}
}
catch ( ApplicationException )
{
devicesCombo.Items.Add( "No local capture devices" );
devicesCombo.Enabled = false;
okButton.Enabled = false;
}
}
// On form loaded
private void VideoCaptureDeviceForm_Load( object sender, EventArgs e )
{
int selectedCameraIndex = 0;
for ( int i = 0; i < videoDevices.Count; i++ )
{
if ( videoDeviceMoniker == videoDevices[i].MonikerString )
{
selectedCameraIndex = i;
break;
}
}
devicesCombo.SelectedIndex = selectedCameraIndex;
}
// Ok button clicked
private void okButton_Click( object sender, EventArgs e )
{
videoDeviceMoniker = videoDevice.Source;
// set video size
if ( videoCapabilitiesDictionary.Count != 0 )
{
VideoCapabilities caps = videoCapabilitiesDictionary[(string) videoResolutionsCombo.SelectedItem];
videoDevice.VideoResolution = caps;
captureSize = caps.FrameSize;
}
if ( configureSnapshots )
{
// set snapshots size
if ( snapshotCapabilitiesDictionary.Count != 0 )
{
VideoCapabilities caps = snapshotCapabilitiesDictionary[(string) snapshotResolutionsCombo.SelectedItem];
videoDevice.ProvideSnapshots = true;
videoDevice.SnapshotResolution = caps;
snapshotSize = caps.FrameSize;
}
}
if ( availableVideoInputs.Length != 0 )
{
videoInput = availableVideoInputs[videoInputsCombo.SelectedIndex];
videoDevice.CrossbarVideoInput = videoInput;
}
}
// New video device is selected
private void devicesCombo_SelectedIndexChanged( object sender, EventArgs e )
{
if ( videoDevices.Count != 0 )
{
videoDevice = new VideoCaptureDevice( videoDevices[devicesCombo.SelectedIndex].MonikerString );
EnumeratedSupportedFrameSizes( videoDevice );
}
}
// Collect supported video and snapshot sizes
private void EnumeratedSupportedFrameSizes( VideoCaptureDevice videoDevice )
{
this.Cursor = Cursors.WaitCursor;
videoResolutionsCombo.Items.Clear( );
snapshotResolutionsCombo.Items.Clear( );
videoInputsCombo.Items.Clear( );
videoCapabilitiesDictionary.Clear( );
snapshotCapabilitiesDictionary.Clear( );
try
{
// collect video capabilities
VideoCapabilities[] videoCapabilities = videoDevice.VideoCapabilities;
int videoResolutionIndex = 0;
foreach ( VideoCapabilities capabilty in videoCapabilities )
{
string item = string.Format(
"{0} x {1}", capabilty.FrameSize.Width, capabilty.FrameSize.Height );
if ( !videoResolutionsCombo.Items.Contains( item ) )
{
if ( captureSize == capabilty.FrameSize )
{
videoResolutionIndex = videoResolutionsCombo.Items.Count;
}
videoResolutionsCombo.Items.Add( item );
}
if ( !videoCapabilitiesDictionary.ContainsKey( item ) )
{
videoCapabilitiesDictionary.Add( item, capabilty );
}
}
if ( videoCapabilities.Length == 0 )
{
videoResolutionsCombo.Items.Add( "Not supported" );
}
videoResolutionsCombo.SelectedIndex = videoResolutionIndex;
if ( configureSnapshots )
{
// collect snapshot capabilities
VideoCapabilities[] snapshotCapabilities = videoDevice.SnapshotCapabilities;
int snapshotResolutionIndex = 0;
foreach ( VideoCapabilities capabilty in snapshotCapabilities )
{
string item = string.Format(
"{0} x {1}", capabilty.FrameSize.Width, capabilty.FrameSize.Height );
if ( !snapshotResolutionsCombo.Items.Contains( item ) )
{
if ( snapshotSize == capabilty.FrameSize )
{
snapshotResolutionIndex = snapshotResolutionsCombo.Items.Count;
}
snapshotResolutionsCombo.Items.Add( item );
snapshotCapabilitiesDictionary.Add( item, capabilty );
}
}
if ( snapshotCapabilities.Length == 0 )
{
snapshotResolutionsCombo.Items.Add( "Not supported" );
}
snapshotResolutionsCombo.SelectedIndex = snapshotResolutionIndex;
}
// get video inputs
availableVideoInputs = videoDevice.AvailableCrossbarVideoInputs;
int videoInputIndex = 0;
foreach ( VideoInput input in availableVideoInputs )
{
string item = string.Format( "{0}: {1}", input.Index, input.Type );
if ( ( input.Index == videoInput.Index ) && ( input.Type == videoInput.Type ) )
{
videoInputIndex = videoInputsCombo.Items.Count;
}
videoInputsCombo.Items.Add( item );
}
if ( availableVideoInputs.Length == 0 )
{
videoInputsCombo.Items.Add( "Not supported" );
}
videoInputsCombo.SelectedIndex = videoInputIndex;
}
finally
{
this.Cursor = Cursors.Default;
}
}
}
}