C # Call the AForge class library to operate the camera
It is a great honor to share the same information.
Recently, a project requires camera operations. Baidu has a lot of information on the Internet, many of which are C # calling the window API to send SendMessage to implement camera operations, however, when C # Calls the window API, due to driver issues, the video selection dialog box is always displayed, making people speechless. Some of the experts have intercepted the window message, and then click the OK button, this is not a compliment, but some Daniel has rewritten IT based on the API prototype. As for IT, I am an IT hacker, and then I continue to Baidu and found an AForge powerful C # class library, finally, I finally got it done. Next I posted some poor code to facilitate the study and communication between colleagues or friends who need it,
First use AForge Class Library: http://www.aforgenet.com/
Then reference AForge, AForge. Controls (this is a control and can be added to the Toolbox), AForge. Imaging, AForge. Video, AForge. Video. DirectShow;
Then directly upload the code
private FilterInfoCollection videoDevices; private VideoCaptureDevice videoSource; public int selectedDeviceIndex = 0;
The following figure shows how to obtain a device.
Public FilterInfoCollection GetDevices () {try {// enumerate all video input devices videoDevices = new FilterInfoCollection (FilterCategory. VideoInputDevice); if (videoDevices. Count! = 0) {LogClass. writeFile ("the video device has been found. "); return videoDevices;} else return null;} catch (Exception ex) {LogClass. writeFile ("error: no video device found! Specific cause: "+ ex. Message); return null ;}}
Select a device and connect to the camera
///
/// Connect to the video camera ////////////
Public VideoCaptureDevice VideoConnect (int deviceIndex = 0, int resolutionIndex = 0) {if (videoDevices. count <= 0) return null; selectedDeviceIndex = deviceIndex; videoSource = new VideoCaptureDevice (videoDevices [deviceIndex]. monikerString );Return videoSource ;}
// Capture, take a photo, public void GrabBitmap (string path) {if (videoSource = null) return; g_Path = path; videoSource. newFrame + = new NewFrameEventHandler (videoSource_NewFrame );}
void videoSource_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs) { Bitmap bmp = (Bitmap)eventArgs.Frame.Clone(); string fullPath = path + "temp\\"; if (!Directory.Exists(fullPath)) Directory.CreateDirectory(fullPath); string img = fullPath + DateTime.Now.ToString("yyyyMMdd hhmmss") + ".bmp"; bmp.Save(img);
// If you do not write this message here, you will not stop taking photos later. videoSource. NewFrame-= new NewFrameEventHandler (videoSource_NewFrame );}
This completes camera operations.
However, a problem is found. If you want to take a photo, you need to save the photo first, and there is a problem here. Therefore, you need to add the control in the front of the interface to the medical AForge. controls, add it to the toolbox, and drag the VideoSourcePlayer control into the form to get a single image processing:
Bitmap bmp = videoSourcePlayer1.GetCurrentFrame ();
In this way, the AForge class library is very powerful. This is just the tip of the iceberg. Please give me some comments and suggestions on the shortcomings of this article. Thank you...