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  FilterInfoCollection filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
108 
109  foreach (FilterInfo filterInfo in filterInfoCollection)
110  {
111  this.videoSources.Add(filterInfo.Name, filterInfo.MonikerString);
112  }
113  }
114 
119  [Log]
120  [LogException]
121  public void StartImaging(string source)
122  {
123  if (!this.videoSources.ContainsKey(source))
124  {
125  this.videoSources.Clear();
126  this.InitVideoSources();
127  if (!this.videoSources.ContainsKey(source))
128  return;
129  }
130 
131  this.StopImaging();
132 
133  this.videoSource = new VideoCaptureDevice(this.videoSources[source]);
134 
135  if (this.videoSource.VideoCapabilities.Length > 0)
136  {
137  VideoCapabilities max = this.videoSource.VideoCapabilities.OrderBy(p => p.FrameSize.Width).Last();
138  this.videoSource.VideoResolution = max;
139  }
140 
141  this.videoSource.NewFrame += this.videoSource_NewFrame;
142  this.videoSource.Start();
143  }
144 
145 
151  [LogException]
152  private void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
153  {
154  if (this.isBusy)
155  return;
156 
157  this.isBusy = true;
158  try
159  {
160 
161  bool setTrap = false;
162  if (this.CaptureNextImage)
163  {
164  setTrap = true;
165  this.CaptureNextImage = false;
166  }
167 
168  Bitmap image = (Bitmap) eventArgs.Frame.Clone();
169 
170  // Bitmap image = (Bitmap) testImage.Clone();
171 
172  if (this.FilterLaser)
173  {
174  float laserPosition = 0.0f;
175 
176  Bitmap grayImage = TransformImage.CreateGrayImageOfLaser(image, Singleton<Config>.Instance.Information.Filter);
177 
178  if (this.ShowLaserLine)
179  {
180  laserPosition = TransformImage.GetLaserLinePosition(grayImage);
181 
182  if (laserPosition > 0.0f)
183  {
184  try
185  {
186  Bitmap tempBitmap = new Bitmap(grayImage.Width, grayImage.Height);
187  using (Graphics graphics = Graphics.FromImage(tempBitmap))
188  {
189  graphics.DrawImage(grayImage, 0, 0);
190  graphics.FillRectangle(this.shadowBrushDarkRed,
191  new RectangleF(laserPosition - 2, 0, 4, grayImage.Height));
192  }
193  this.pictureBoxCameraImage.Image = tempBitmap;
194  }
195  catch (Exception ex)
196  {
197  }
198  }
199  else
200  {
201  this.pictureBoxCameraImage.Image = grayImage;
202  }
203  }
204  else
205  {
206  this.pictureBoxCameraImage.Image = grayImage;
207  }
208  }
209  else
210  {
211  using (Graphics graphics = Graphics.FromImage(image))
212  {
213  int middle = image.Width/2;
214 
215  float pixelPerCM = (float)Singleton<Config>.Instance.Information.Camera.Width/Singleton<Config>.Instance.Information.Camera.WorldWidth;
216  float turntableXRadius = (float)Singleton<Config>.Instance.Information.Turntable.Radius * pixelPerCM;
217 
218  RectangleF turntableRectangleF = new RectangleF
219  {
220  X = middle - turntableXRadius,
221  Width = 2*turntableXRadius,
222  Y = image.Height*0.75f,
223  Height = turntableXRadius/1.66f/((float)image.Width/(float)image.Height)
224  };
225 
226  graphics.FillEllipse(this.shadowBrushBlue, turntableRectangleF);
227 
228  // graphics.FillRectangle(this.shadowBrushBlue, new RectangleF(175, 715, 900, 210));
229  // show where the table should be aligned to
230 
231  if (this.LaserMotorEnabled)
232  {
233  graphics.FillRectangle(this.shadowBrushOrange,
234  new RectangleF(this.LaserLeftPosition - 2, 0, 4, image.Height));
235  graphics.FillRectangle(this.shadowBrushRed,
236  new RectangleF(this.LaserRightPosition - 2, 0, 4, image.Height));
237  }
238 
239  if (this.ShowLaserLine)
240  {
241  float laserPosition = TransformImage.GetLaserLinePosition(image,
242  Singleton<Config>.Instance.Information.Filter);
243 
244  graphics.FillRectangle(this.shadowBrushDarkRed,
245  new RectangleF(laserPosition - 2, 0, 4, image.Height));
246  }
247  }
248 
249  this.pictureBoxCameraImage.Image = image;
250  }
251 
252  if (!setTrap)
253  return;
254  this.nextImage = (Image)eventArgs.Frame.Clone();
255  lock (this.pulseObject)
256  Monitor.Pulse(this.pulseObject);
257  }
258  finally
259  {
260  this.isBusy = false;
261  }
262  }
263 
267  [Log]
268  [LogException]
269  public void StopImaging()
270  {
271  if (this.videoSource == null || !this.videoSource.IsRunning)
272  return;
273 
274  this.videoSource.SignalToStop();
275  this.videoSource = null;
276  }
277 
282  [Log]
283  [LogException]
285  {
286  lock (this.pulseObject)
287  {
288  this.CaptureNextImage = true;
289  Monitor.Wait(this.pulseObject);
290  }
291  lock (this.pulseObject)
292  {
293  this.CaptureNextImage = true;
294  Monitor.Wait(this.pulseObject);
295  }
296  return this.nextImage;
297  }
298 
299  }
300 }
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