程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 通過Emgu CV播放流媒體(RTSP),

C# 通過Emgu CV播放流媒體(RTSP),

編輯:C#入門知識

C# 通過Emgu CV播放流媒體(RTSP),


      Emgu CV is a cross platform .Net wrapper to the OpenCV image processing library. Allowing OpenCV functions 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:[email protected]: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同步連接請求視頻源,連接超時時間很長,

Capture _capture = new Capture(fileName),如果訪問的媒體不存在,會導致阻塞。需要設置ffmpeg連接方式或者超時時間。

設置方式:
public bool SetCaptureProperty(CapProp property, double value); //Capture方法。具體怎麼設置不知道、或者Emgu CV沒有提供這個接口

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved