Today began to work on the C # launch camera, previously done with C + + call OpenCV Library to start the camera, C # theoretically can also. But read this blog notes, immediately brain hole open, the original can and the camera to deal with the library so much AH. Before I could do it until OpenCV. Ashamed, his knowledge is too narrow.
Of course, today's plays are the Aforge.net Framework.
Let's start by looking at a simple example that I've been crunching:
Because it is evening, the light is not very good oh. Cause it doesn't look good enough.
function is actually very simple, open the camera, capture each frame image ---------aforge.net Framework Introduction 1, Introduction
Aforge.net is a specially designed for developers and researchers based on the C # Framework, which includes computer vision and artificial intelligence, image processing, neural networks, genetic algorithms, machine learning, fuzzy systems, robot control, and other fields. 2, the main structure
This framework consists of a series of class libraries. Mainly include:
aforge.imaging--some of the day-to-day image processing and filters
aforge.vision--Computer Vision Application Class Library
aforge.neuro--Neural Network Computing Library aforge.genetic-Evolutionary algorithm programming library
aforge.machinelearning--Machine Learning Class Library
aforge.robotics--provides tools class libraries for robots
aforge.video--a series of video processing class libraries
aforge.fuzzy--Fuzzy Inference System class Library
aforge.controls--image, three-dimensional, chart Display control---------------Download
Now, the version has been updated to 2.2.5.
Download Connection: http://www.aforgenet.com/framework/downloads.html
----------------Add a reference to vs
1, create a new C # Window reference program. Drag a button control and a PictureBox control in the Toolbox. The location and size of the space from the set.
2, add a reference.
The name of the DLL you have added is:
AForge.Video.DirectShow.dll
AForge.Video.dll
------------Add Namespaces
Using AForge.Video.DirectShow;
Using Aforge.video;
------------Adding class properties
---Declare variable public
filterinfocollection use_webcams = null;
public Videocapturedevice cam = null;
--------------button to click event code
---button click event
private void Startbtn_click (object sender, EventArgs e)
{
try
{
if (Startbtn.text = = "Start")
{
///--
startbtn.text = "Stop";
---Start the camera
cam. Start ();
}
else
{
///--set button hint
startbtn.text = "Start";
--Stop camera capture image
Cam. Stop ();
}
catch (Exception ex)
{
MessageBox.Show (ex. message);
}
--------The window's load function source
--Window Load event private void Form1_Load (object sender, EventArgs e) {try {
---Instantiate object use_webcams = new Filterinfocollection (filtercategory.videoinputdevice);
---camera number is greater than 0 if (Use_webcams.count > 0) {///---disable button
Startbtn.enabled = true; ---instantiated object cam = new Videocapturedevice (Use_webcams[0].
monikerstring); ---bind event cam.
Newframe + = new Newframeeventhandler (cam_newframe);
else {///--has no webcam startbtn.enabled = false;
---tip No camera messagebox.show ("No Camera peripherals"); } catch (Exception ex) {MessageBox.Show (ex).
message);
} }
Here is a custom function, the source code is as follows:
------The custom Function
private void Cam_newframe (Object obj, Newframeeventargs EventArgs)
{
pictureBox1.Image = ( Bitmap) EventArgs.Frame.Clone ();
---throw new notimplementedexception ();
}
formclosed Event Source for---------window
---window Shutdown event
private void Form1_formclosed (object sender, Formclosedeventargs e)
{
try
{
if ( Cam!= null)
{
///---Turn off the camera
if (CAM). isrunning)
{
cam. Stop ();
}} catch (Exception ex)
{
MessageBox.Show (ex. message);
}
-----Compile Run try
------Complete source code is as follows
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
---Add a namespace using AForge.Video.DirectShow;
Using Aforge.video; namespace Os_webcam {public partial class Form1:form {public Form1 () {Initializec
Omponent ();
///---Declare variable public filterinfocollection use_webcams = null;
public Videocapturedevice cam = null;
---button clicked event private void Startbtn_click (object sender, EventArgs e) {try { if (Startbtn.text = = "Start") {///--Startbtn.text = "Stop"
; ---start the camera cam.
Start ();
else {///--Set button prompt Startbtn.text = "Start"; --Stop camera captureImage Cam.
Stop (); } catch (Exception ex) {MessageBox.Show (ex).
message);
}//--window Load event private void Form1_Load (object sender, EventArgs e) {try {///---instantiated object use_webcams = new Filterinfocollection (Filtercategory.videoinputdevic
e);
---camera number is greater than 0 if (Use_webcams.count > 0) {///---disable button
Startbtn.enabled = true; ---instantiated object cam = new Videocapturedevice (Use_webcams[0].
monikerstring); ---bind event cam.
Newframe + = new Newframeeventhandler (cam_newframe);
else {///--has no webcam startbtn.enabled = false; ---tip no camera MesSagebox.show ("No Camera peripherals"); } catch (Exception ex) {MessageBox.Show (ex).
message);
}///------Custom function private void Cam_newframe (Object obj, Newframeeventargs eventArgs) {
pictureBox1.Image = (Bitmap) eventArgs.Frame.Clone ();
---throw new notimplementedexception ();
///---window closes event private void form1_formclosed (object sender, Formclosedeventargs e) {
try {if (Cam!= null) {///---turn off the camera if (Cam. isrunning) {cam.
Stop (); '} ' catch (Exception ex) {MessageBox.Show (ex).
message); }
}
}
}