首页 > 学院 > 开发设计 > 正文

C# 通过Emgu CV播放流媒体(RTSP)

2019-11-17 02:20:45
字体:
来源:转载
供稿:网友

C# 通过Emgu CV播放流媒体(RTSP)

Emgu CVis a cross platform .Net wrapper to theOpenCVimage PRocessing library. AllowingOpenCVfunctions to be called from .NET compatible languages 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,需要下载3.0版本以后的,Emgu CV 3.0集成了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;

        //显示图片 可以使用Emgu CV 提供的ImageBox显示视频, 也可以转成BitmapSource显示。

        //略

      }     }   }   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;            }        }    }

 注意事项:由于ffmpeg 默认使用TCP阻塞方式建立连接,连接超时时间很长。如果连接不成功,会导致阻塞。需要设置ffmpeg连接方式或者设置超时时间。

设置方式:public bool SetCaptureProperty(CapProp property, double value); //Capture方法。具体怎么设置不知道、或者Emgu CV没有提供这个接口


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表