⬆️ ⬇️

Capture video from the camera and transmit it over the network

Foreword



Not so long ago, it became necessary to capture video from a web camera and transmit it over the network using .Net.

Since I ran into a similar task for the first time, I first began to look for information on this issue.

As it turned out that in pure .Net there is no support for working with webcams. After going through several different libraries, he chose Aforge.net.



Aforge.net is a framework for solving a variety of tasks from which we need the AForge.Video library.



Image capture



To capture video from a video input device, there is a class AForge.Video.DirectShow.VideoCaptureDevice . He needs to specify the moniker of the device from which the capture will occur. You must also set the NewFrame event NewFrame . This event occurs every time a new frame is received from the device, which is passed to the handler as a Bitmap object, where it can already be processed:

 private void VideoSourceNewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs) { var img = (Image) eventArgs.Frame; using (var ms=new MemoryStream()) { img.Save(ms,ImageFormat.Jpeg); //    ,    mjpeg _bufImage = ms.ToArray(); } } 




Video capture is started by calling the Start() method.

')

The list of available cameras installed in the system can be obtained using the FilterInfoCollection class, passing it, as a parameter, the necessary category of devices:

 var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); 


This class is a collection of FilterInfo elements that contain two fields:



Thus, having received the device moniker, we can transfer it to the VideoCaptureDevice class.



Formation of the video stream MJPEG



MJPEG (Motion JPEG) is the simplest video compression algorithm, so I stopped my choice on it.

The stream of MJPEG video is successively coming in JPEG format, complemented by the http header:

 HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Transfer-Encoding: chunked Content-Type: multipart/x-mixed-replace; boundary=--myboundary Expires: -1 --myboundary Content-Type: image/jpeg Content-Length:96719 .....image....... --myboundary Content-Type: image/jpeg Content-Length:96720 .....next image....... 


Example of implementation:

 public ActionResult Video() { Response.Clear(); //       Response.ContentType = "multipart/x-mixed-replace; boundary=--myboundary"; //  Response.Expires = 0; Response.Cache.SetCacheability(HttpCacheability.NoCache); var ae = new ASCIIEncoding(); //      while (Response.IsClientConnected) { try { //_bufImage - ,        jpeg var buf = _bufImage; //   var boundary = ae.GetBytes("\r\n--myboundary\r\nContent-Type: image/jpeg\r\nContent-Length:" + buf.Length + "\r\n\r\n"); Response.OutputStream.Write(boundary, 0, boundary.Length); Response.OutputStream.Write(buf, 0, buf.Length); Response.Flush(); // ,     20 / Thread.Sleep(50); } catch (Exception) { } } Response.End(); return null; } 


Aforge.net

MJPEG

Link to the source in Google Docs

Source: https://habr.com/ru/post/177793/



All Articles