Simply3DScan
ConfigurationControl.Camera.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Globalization;
4 using System.Linq;
5 using AForge.Video.DirectShow;
6 using SharedObjects;
7 
8 namespace Simple3DScan.UI
9 {
10  partial class ConfigurationControl
11  {
15  private readonly Dictionary<string, string> videoSources = new Dictionary<string, string>();
16 
17  private readonly FilterInfoCollection filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
18 
22  [Log]
23  private void InitCameraValues()
24  {
25  this.InitiateCameraDropdown();
26 
27  this.textBoxMoniker.Text = this.configuration.Camera.Moniker;
28 
29  this.textBoxCameraX.Text = this.configuration.Camera.WorldCoordinates.X.ToString(CultureInfo.InvariantCulture);
30  this.textBoxCameraX.TextChanged += this.TextBoxCameraXOnTextChanged;
31 
32  this.textBoxCameraY.Text = this.configuration.Camera.WorldCoordinates.Y.ToString(CultureInfo.InvariantCulture);
33  this.textBoxCameraY.TextChanged += this.TextBoxCameraYOnTextChanged;
34 
35  this.textBoxCameraZ.Text = this.configuration.Camera.WorldCoordinates.Z.ToString(CultureInfo.InvariantCulture);
36  this.textBoxCameraZ.TextChanged += this.TextBoxCameraZOnTextChanged;
37 
38  this.textBoxCameraWidthCM.Text = this.configuration.Camera.WorldWidth.ToString(CultureInfo.InvariantCulture);
39  this.textBoxCameraWidthCM.TextChanged += this.TextBoxCameraWidthOnTextChanged;
40 
41  this.textBoxCameraHeightPX.Text = this.configuration.Camera.Height.ToString();
42  this.textBoxCameraWidthPX.Text = this.configuration.Camera.Width.ToString();
43  }
44 
48  [LogException]
49  private void InitiateCameraDropdown()
50  {
51  int i = 0;
52  foreach (FilterInfo filterInfo in this.filterInfoCollection)
53  {
54  this.comboBoxCamera.Items.Add(filterInfo.Name);
55  this.videoSources.Add(filterInfo.Name, filterInfo.MonikerString);
56  if (!String.IsNullOrEmpty(this.configuration.Camera.Name) &&
57  filterInfo.Name == this.configuration.Camera.Name)
58  {
59  this.comboBoxCamera.SelectedIndex = i;
60 
61  this.SetMaxFrameSizes(filterInfo.MonikerString);
62 
63  break;
64  }
65  i++;
66  }
67 
68  this.comboBoxCamera.SelectedIndexChanged += this.ComboBoxCameras_SelectedIndexChanged;
69  }
70 
76  [Log]
77  [LogException]
78  protected void ComboBoxCameras_SelectedIndexChanged(object sender, EventArgs e)
79  {
80  if (!this.videoSources.ContainsKey(this.comboBoxCamera.Text))
81  return;
82 
83  this.configuration.Camera.Name = this.comboBoxCamera.Text;
84  this.configuration.Camera.Moniker = this.videoSources[this.comboBoxCamera.Text];
85 
86  this.textBoxMoniker.Text = this.configuration.Camera.Moniker;
87 
88  this.SetMaxFrameSizes(this.configuration.Camera.Moniker);
89  }
90 
95  [Log]
96  [LogException]
97  private void SetMaxFrameSizes(string monikerString)
98  {
99  VideoCaptureDevice source = new VideoCaptureDevice(monikerString);
100 
101  if (source.VideoCapabilities.Length > 0)
102  {
103  VideoCapabilities max = source.VideoCapabilities.OrderBy(p => p.FrameSize.Width).Last();
104  source.VideoResolution = max;
105  }
106  if (null != source.VideoResolution)
107  {
108  this.configuration.Camera.Height = source.VideoResolution.FrameSize.Height;
109  this.configuration.Camera.Width = source.VideoResolution.FrameSize.Width;
110  this.textBoxCameraHeightPX.Text = source.VideoResolution.FrameSize.Height.ToString();
111  }
112  }
113 
119  [Log]
120  [LogException]
121  private void TextBoxCameraXOnTextChanged(object sender, EventArgs eventArgs)
122  {
123  this.configuration.Camera.WorldCoordinates.X = float.Parse(this.textBoxCameraX.Text);
124  }
125 
131  [Log]
132  [LogException]
133  private void TextBoxCameraYOnTextChanged(object sender, EventArgs eventArgs)
134  {
135  this.configuration.Camera.WorldCoordinates.Y = float.Parse(this.textBoxCameraY.Text);
136  }
137 
143  [Log]
144  [LogException]
145  private void TextBoxCameraZOnTextChanged(object sender, EventArgs eventArgs)
146  {
147  this.configuration.Camera.WorldCoordinates.Z = float.Parse(this.textBoxCameraZ.Text);
148  }
149 
156  [Log]
157  [LogException]
158  private void TextBoxCameraWidthOnTextChanged(object sender, EventArgs e)
159  {
160  this.configuration.Camera.WorldWidth = float.Parse(this.textBoxCameraWidthCM.Text);
161  }
162 
163  [Log]
164  [LogException]
165  private void buttonLoadCameraDefaults_Click(object sender, EventArgs e)
166  {
167  this.DetachCameraEvents();
168  this.comboBoxCamera.Items.Clear();
169  string tempCamera = this.configuration.Camera.Name;
170  string tempMoniker = this.configuration.Camera.Moniker;
171  this.configuration.Camera = Configuration.Config.CreateDefaultCamera();
172  this.configuration.Camera.Name = tempCamera;
173  this.configuration.Camera.Moniker = tempMoniker;
174  this.InitCameraValues();
175  }
176 
177  private void DetachCameraEvents()
178  {
179  this.textBoxCameraX.TextChanged -= this.TextBoxCameraXOnTextChanged;
180  this.textBoxCameraY.TextChanged -= this.TextBoxCameraYOnTextChanged;
181  this.textBoxCameraZ.TextChanged -= this.TextBoxCameraZOnTextChanged;
182  this.textBoxCameraWidthCM.TextChanged -= this.TextBoxCameraWidthOnTextChanged;
183  this.comboBoxCamera.SelectedIndexChanged -= this.ComboBoxCameras_SelectedIndexChanged;
184  }
185  }
186 }
string Name
Gets or sets the name.
Definition: Camera.cs:15
void ComboBoxCameras_SelectedIndexChanged(object sender, EventArgs e)
Handles the SelectedIndexChanged event of the ComboBoxCameras control.
static Camera CreateDefaultCamera()
Creates the default camera.
Definition: Config.cs:97
Class for accessing the configuration.
Definition: Config.cs:12