Simply3DScan
CameraControl.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Drawing;
4 using System.Linq;
5 using System.Threading;
6 using System.Windows.Forms;
7 using AForge.Video;
8 using AForge.Video.DirectShow;
9 using Configuration;
10 using SharedObjects;
11 
12 namespace Simple3DScan.UI
13 {
17  public partial class CameraControl : UserControl
18  {
19  private bool isBusy;
20  private VideoCaptureDevice videoSource;
21  private readonly Dictionary<string, string> videoSources = new Dictionary<string, string>();
22  private readonly Object pulseObject = new object();
23 
24  readonly SolidBrush shadowBrushDarkRed = new SolidBrush(Color.FromArgb(100, Color.Firebrick));
25  readonly SolidBrush shadowBrushBlue = new SolidBrush(Color.FromArgb(25, Color.Blue));
26  readonly SolidBrush shadowBrushOrange = new SolidBrush(Color.FromArgb(75, Color.Orange));
27  readonly SolidBrush shadowBrushRed = new SolidBrush(Color.FromArgb(75, Color.Red));
28 
29  //private Bitmap testImage = null;
30 
37  public bool CaptureNextImage { get; private set; }
41  private Image nextImage;
48  public bool FilterLaser { get; set; }
55  public bool ShowLaserLine { get; set; }
62  public float LaserLeftPosition { get; set; }
69  public float LaserRightPosition { get; set; }
76  public bool LaserMotorEnabled { get; set; }
77 
81  [Log]
82  [LogException]
83  public CameraControl()
84  {
85  if(!this.DesignMode)
86  this.InitializeComponent();
87  }
88 
94  [Log]
95  [LogException]
96  private void CameraControl_Load(object sender, EventArgs e)
97  {
98 
99  //testImage = (Bitmap)Image.FromFile(".\\im.png", true);
100 
101  this.InitVideoSources();
102  }
103 
104  [LogException]
105  private void InitVideoSources()
106  {
107  foreach (FilterInfo filterInfo in new FilterInfoCollection(FilterCategory.VideoInputDevice))
108  {
109  this.videoSources.Add(filterInfo.Name, filterInfo.MonikerString);
110  }
111  }
112 
117  [Log]
118  [LogException]
119  public void StartImaging(string source)
120  {
121  if (!this.videoSources.ContainsKey(source))
122  {
123  this.videoSources.Clear();
124  this.InitVideoSources();
125  if (!this.videoSources.ContainsKey(source))
126  return;
127  }
128 
129  this.StopImaging();
130 
131  this.videoSource = new VideoCaptureDevice(this.videoSources[source]);
132 
133  if (this.videoSource.VideoCapabilities.Length > 0)
134  {
135  VideoCapabilities max = this.videoSource.VideoCapabilities.OrderBy(p => p.FrameSize.Width).Last();
136  this.videoSource.VideoResolution = max;
137  }
138 
139  this.videoSource.NewFrame += this.videoSource_NewFrame;
140  this.videoSource.Start();
141  }
142 
143 
149  [LogException]
150  private void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
151  {
152  if (this.isBusy)
153  return;
154 
155  this.isBusy = true;
156  try
157  {
158 
159  bool setTrap = false;
160  if (this.CaptureNextImage)
161  {
162  setTrap = true;
163  this.CaptureNextImage = false;
164  }
165 
166  Bitmap image = (Bitmap) eventArgs.Frame.Clone();
167 
168  // Bitmap image = (Bitmap) testImage.Clone();
169 
170  if (this.FilterLaser)
171  {
172  Bitmap grayImage = TransformImage.CreateGrayImageOfLaser(image, Singleton<Config>.Instance.Information.Filter);
173 
174  if (this.ShowLaserLine)
175  {
176  float laserPosition = TransformImage.GetLaserLinePosition(grayImage);
177 
178  if (laserPosition > 0.0f)
179  {
180  try
181  {
182  Bitmap tempBitmap = new Bitmap(grayImage.Width, grayImage.Height);
183  using (Graphics graphics = Graphics.FromImage(tempBitmap))
184  {
185  graphics.DrawImage(grayImage, 0, 0);
186  graphics.FillRectangle(this.shadowBrushDarkRed,
187  new RectangleF(laserPosition - 2, 0, 4, grayImage.Height));
188  }
189  this.pictureBoxCameraImage.Image = tempBitmap;
190  }
191  catch (Exception ex)
192  {
193  }
194  }
195  else
196  {
197  this.pictureBoxCameraImage.Image = grayImage;
198  }
199  }
200  else
201  {
202  this.pictureBoxCameraImage.Image = grayImage;
203  }
204  }
205  else
206  {
207  using (Graphics graphics = Graphics.FromImage(image))
208  {
209  int middle = image.Width/2;
210 
211  float pixelPerCM = (float)Singleton<Config>.Instance.Information.Camera.Width/Singleton<Config>.Instance.Information.Camera.WorldWidth;
212  float turntableXRadius = (float)Singleton<Config>.Instance.Information.Turntable.Radius * pixelPerCM;
213 
214  RectangleF turntableRectangleF = new RectangleF
215  {
216  X = middle - turntableXRadius,
217  Width = 2*turntableXRadius,
218  Y = image.Height*0.75f,
219  Height = turntableXRadius/1.66f/((float)image.Width/(float)image.Height)
220  };
221 
222  graphics.FillEllipse(this.shadowBrushBlue, turntableRectangleF);
223 
224  // graphics.FillRectangle(this.shadowBrushBlue, new RectangleF(175, 715, 900, 210));
225  // show where the table should be aligned to
226 
227  if (this.LaserMotorEnabled)
228  {
229  graphics.FillRectangle(this.shadowBrushOrange,
230  new RectangleF(this.LaserLeftPosition - 2, 0, 4, image.Height));
231  graphics.FillRectangle(this.shadowBrushRed,
232  new RectangleF(this.LaserRightPosition - 2, 0, 4, image.Height));
233  }
234 
235  if (this.ShowLaserLine)
236  {
237  float laserPosition = TransformImage.GetLaserLinePosition(image,
238  Singleton<Config>.Instance.Information.Filter);
239 
240  graphics.FillRectangle(this.shadowBrushDarkRed,
241  new RectangleF(laserPosition - 2, 0, 4, image.Height));
242  }
243  }
244 
245  this.pictureBoxCameraImage.Image = image;
246  }
247 
248  if (!setTrap)
249  return;
250  this.nextImage = (Image)eventArgs.Frame.Clone();
251  lock (this.pulseObject)
252  Monitor.Pulse(this.pulseObject);
253  }
254  finally
255  {
256  this.isBusy = false;
257  }
258  }
259 
263  [Log]
264  [LogException]
265  public void StopImaging()
266  {
267  if (this.videoSource == null || !this.videoSource.IsRunning)
268  return;
269 
270  this.videoSource.SignalToStop();
271  this.videoSource = null;
272  }
273 
278  [Log]
279  [LogException]
281  {
282  lock (this.pulseObject)
283  {
284  this.CaptureNextImage = true;
285  Monitor.Wait(this.pulseObject);
286  }
287  lock (this.pulseObject)
288  {
289  this.CaptureNextImage = true;
290  Monitor.Wait(this.pulseObject);
291  }
292  return this.nextImage;
293  }
294 
295  }
296 }
void StartImaging(string source)
Starts the imaging.
static float GetLaserLinePosition(Image laserImage, Filter filter)
Gets the laser line position.
static Bitmap CreateGrayImageOfLaser(Bitmap image, Filter filter)
Creates the gray image of laser.
CameraControl()
Initializes a new instance of the CameraControl class.
void StopImaging()
Stops the imaging.
Control for accessing a camera
Class for creating a singleton for a generic class
Definition: Singleton.cs:9
static T Instance
Gets the instance.
Definition: Singleton.cs:27
Does transformations of an image
Image GetNextPicture()
Gets the next picture.
System.Drawing.Image Image