[RTSP]WPF用Emgu显示RTSP视频

来源:互联网 发布:淘宝设计感饰品 知乎 编辑:程序博客网 时间:2024/06/06 06:55

[RTSP]WPF用Emgu显示RTSP视频

场景

  • 拿到一个RTSP之后如何显示,有很多办法,就是拿流解码显示,这里我们是用WPF的image控件来显示,WPF没有控件句柄的概念,所以我们要用自带的方式优美的显示出来。

操作

  • 下载emgu可以到官网下载
  • 同样也可以在github下下载
  • 3.0以上的版本集成了ffpeng,所以可以用ffpeng来解码

执行

  1. 我们新建一个WPF工程。.net 4.0以上,不然可能要手动安装依赖组件
  2. 在xaml添加一个image控件和按钮
 <Grid x:Name="myGrid">         <Image x:Name="myimage" Stretch="Fill"/>        <Button Content="Button" HorizontalAlignment="Left" Margin="28,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>        <Button Content="Button" HorizontalAlignment="Left" Visibility="Collapsed" Margin="126,17,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_2"/>        <Button Content="Button" HorizontalAlignment="Left" Margin="219,17,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>    </Grid>
  1. 我们引入emgu的dll(网上的其它教程都是基于之前的版本写的也最新的版本不同,因为.net也有一个Capture控件,而且emgu有命名空间的更改。找不到emgu.cv.dll是正常。我今天也是苦苦找了好久,阅读官网文档才发现有所更改),我们用negut来安装现在最新的版本是3.3
    这里写图片描述
  2. 在对应的按钮下执行操作
 VideoCapture _capture = new VideoCapture("rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov");   _capture.ImageGrabbed += _capture_ImageGrabbed;             _capture.Start();              private object lockObj = new object();        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;                        myimage.Dispatcher.Invoke(new Action(() =>                        {                            myimage.Source = BitmapSourceConvert.ToBitmapSource(frame);                        }));                        //显示图片 可以使用Emgu CV 提供的 ImageBox显示视频, 也可以转成 BitmapSource显示。                        frame.Dispose();                        //略          //  _capture.Dispose();停止关闭                    }                }            }            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;            }        }    }
  1. 执行测试
    这里写图片描述

提示

  • 看到别人解决了说明这是一个思路,但是好用不好用一定要试一下
  • Demo一定要结合现有的项目,不能一看可以就想到一定适合项目。
  • emgu不全是opencv,有些功能在windows下有的功能和库是没有的。

尝试,是一个人学习唯一的法门。

原创粉丝点击