2 using System.Collections.Generic;
8 using AForge.Video.DirectShow;
20 private VideoCaptureDevice videoSource;
21 private readonly Dictionary<string, string> videoSources =
new Dictionary<string, string>();
22 private readonly Object pulseObject =
new object();
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));
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; }
86 this.InitializeComponent();
96 private void CameraControl_Load(
object sender, EventArgs e)
101 this.InitVideoSources();
105 private void InitVideoSources()
107 FilterInfoCollection filterInfoCollection =
new FilterInfoCollection(FilterCategory.VideoInputDevice);
109 foreach (FilterInfo filterInfo
in filterInfoCollection)
111 this.videoSources.Add(filterInfo.Name, filterInfo.MonikerString);
123 if (!this.videoSources.ContainsKey(source))
125 this.videoSources.Clear();
126 this.InitVideoSources();
127 if (!this.videoSources.ContainsKey(source))
133 this.videoSource =
new VideoCaptureDevice(this.videoSources[source]);
135 if (this.videoSource.VideoCapabilities.Length > 0)
137 VideoCapabilities max = this.videoSource.VideoCapabilities.OrderBy(p => p.FrameSize.Width).Last();
138 this.videoSource.VideoResolution = max;
141 this.videoSource.NewFrame += this.videoSource_NewFrame;
142 this.videoSource.Start();
152 private void videoSource_NewFrame(
object sender, NewFrameEventArgs eventArgs)
161 bool setTrap =
false;
162 if (this.CaptureNextImage)
165 this.CaptureNextImage =
false;
168 Bitmap image = (Bitmap) eventArgs.Frame.Clone();
172 if (this.FilterLaser)
174 float laserPosition = 0.0f;
178 if (this.ShowLaserLine)
182 if (laserPosition > 0.0f)
186 Bitmap tempBitmap =
new Bitmap(grayImage.Width, grayImage.Height);
187 using (Graphics graphics = Graphics.FromImage(tempBitmap))
189 graphics.DrawImage(grayImage, 0, 0);
190 graphics.FillRectangle(this.shadowBrushDarkRed,
191 new RectangleF(laserPosition - 2, 0, 4, grayImage.Height));
193 this.pictureBoxCameraImage.Image = tempBitmap;
201 this.pictureBoxCameraImage.Image = grayImage;
206 this.pictureBoxCameraImage.Image = grayImage;
211 using (Graphics graphics = Graphics.FromImage(image))
213 int middle = image.Width/2;
218 RectangleF turntableRectangleF =
new RectangleF
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)
226 graphics.FillEllipse(this.shadowBrushBlue, turntableRectangleF);
231 if (this.LaserMotorEnabled)
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));
239 if (this.ShowLaserLine)
241 float laserPosition = TransformImage.GetLaserLinePosition(image,
244 graphics.FillRectangle(this.shadowBrushDarkRed,
245 new RectangleF(laserPosition - 2, 0, 4, image.Height));
249 this.pictureBoxCameraImage.Image = image;
254 this.nextImage = (
Image)eventArgs.Frame.Clone();
255 lock (this.pulseObject)
256 Monitor.Pulse(this.pulseObject);
271 if (this.videoSource == null || !this.videoSource.IsRunning)
274 this.videoSource.SignalToStop();
275 this.videoSource = null;
286 lock (this.pulseObject)
288 this.CaptureNextImage =
true;
289 Monitor.Wait(this.pulseObject);
291 lock (this.pulseObject)
293 this.CaptureNextImage =
true;
294 Monitor.Wait(this.pulseObject);
296 return this.nextImage;
void StartImaging(string source)
Starts the imaging.
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
static T Instance
Gets the instance.
Image GetNextPicture()
Gets the next picture.