C # Use Emgu CV to play streaming media (RTSP ),

Source: Internet
Author: User
Tags image processing library

C # Use Emgu CV to play streaming media (RTSP ),

Emgu CVIs a cross platform. net wrapper to the OpenCV image processing library. allowing OpenCV functions to be called from. NET compatible programming ages such as C #, VB, VC ++, IronPython etc. the wrapper can be compiled by Visual Studio, Xamarin Studio and Unity, it can run on Windows, Linux, Mac OS X, iOS, Android and Windows Phone.

Emgu CV: http://www.emgu.com/wiki/index.php/main_page. download the 3.0 forbidden file, and Emgu CV becomes ffmpeg.

  

Capture _capture = new Capture(fileName); //rtsp://user:password@192.168.1.66:554/  _capture.ImageGrabbed += capture_ImageGrabbed;_capture.Start();

Private void capture_ImageGrabbed (object sender, EventArgs e)
{
Try
{
Mat frame = new Mat ();

Lock (lockObj)
{
If (capture! = Null)
{
If (! Capture. Retrieve (frame ))
{
Frame. Dispose ();
Return;
}
If (frame. IsEmpty)
Return;

// You can use the ImageBox provided by Emgu CV to display the video or convert it to BitmapSource.

// Omitted

}
}
}
Catch (Exception ex)
{
}
}

 

 

public static class BitmapSourceConvert    {        /// <summary>        /// Delete a GDI object        /// </summary>        /// <param name="o">The poniter to the GDI object to be deleted</param>        /// <returns></returns>        [DllImport("gdi32")]        private static extern int DeleteObject(IntPtr o);        /// <summary>        /// Convert an IImage to a WPF BitmapSource. The result can be used in the Set Property of Image.Source        /// </summary>        /// <param name="image">The Emgu CV Image</param>        /// <returns>The equivalent BitmapSource</returns>        public static BitmapSource ToBitmapSource(IImage image)        {            using (System.Drawing.Bitmap source = image.Bitmap)            {                IntPtr ptr = source.GetHbitmap(); //obtain the Hbitmap                BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(                    ptr,                    IntPtr.Zero,                    Int32Rect.Empty,                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());                DeleteObject(ptr); //release the HBitmap                return bs;            }        }    }

 

Note: Because ffmpeg uses TCP to synchronously connect to the video source by default, the connection timeout is long,

Capture _ capture = new Capture (fileName). If the accessed media does not exist, blocking may occur. You need to set the ffmpeg connection mode or timeout time.

Setting method:
Public bool SetCaptureProperty (CapProp property, double value); // The Capture method. The specific settings are unknown, or Emgu CV does not provide this interface

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.