C# OpenCV 接收RSTP视频流

public class VideoReceivedEventArgs : EventArgs
{
    public Mat Frame { get; set; }
    public VideoReceivedEventArgs(Mat frame)
    {
        this.Frame = frame;
    }
}
internal interface IVideoReceiver : IDisposable
{
    string Camera { get; }
    bool IsConnected { get; }
    double Fps { get; }
    Size Size { get; }

    event EventHandler<VideoReceivedEventArgs> VideoReceived;

    bool Connect();

    bool Disconnect();

    Mat Receive();
}
internal class VideoReceiver : IVideoReceiver
{
    public bool IsConnected => this.capture?.IsOpened() ?? false;
    public double Fps { get; private set; }
    public Size Size { get; private set; }

    public string Camera { get; private set; }

    private string url;
    public VideoReceiver(string camera, string url)
    {
        this.Camera = camera;
        this.url = url;
    }


    private VideoCapture capture;
    public bool Connect()
    {
        this.Disconnect();
        this.capture = new VideoCapture(url)
        {
            BufferSize = 256
        };
        this.capture.Set(VideoCaptureProperties.PosMsec, 0);
        this.capture.Set(VideoCaptureProperties.PosFrames, 0);
        this.capture.Set(VideoCaptureProperties.Fps, 15);


        this.capture.Open(url);
        this.Size = new Size(this.capture.FrameWidth, this.capture.FrameHeight);
        this.Fps = this.capture.Fps;

        return this.IsConnected;
    }

    public event EventHandler<VideoReceivedEventArgs> VideoReceived;

    protected virtual void OnVideoReceived(VideoReceivedEventArgs e)
    {
        this.VideoReceived?.Invoke(this, e);
    }

    public bool Disconnect()
    {
        this.capture?.Release();
        this.capture?.Dispose();
        this.capture = null;
        return true;
    }

    public Mat Receive()
    {
        if (this.capture is null)
        {
            return null;
        }
        var frame = new Mat();
        this.capture.Read(frame);

        this.OnVideoReceived(new VideoReceivedEventArgs(frame));
        return frame;
    }

    public void Dispose()
    {
        this.Dispose(disposing: true);
        GC.SuppressFinalize(this);
    }

    private bool disposed = false;
    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                capture.Dispose();
            }

            this.capture = null;
            disposed = true;
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在C#中使用OpenCV库获取RTSP视频流并将其显示在PictureBox上,你需要使用OpenCV的VideoCapture类来打开RTSP流,并使用OpenCV的Mat类来处理视频帧,并将其转换为图像以在PictureBox上显示。以下是一个示例代码: 首先,确保你已经安装了OpenCV并将其配置到你的C#项目中。你可以使用NuGet包管理器来安装OpenCvSharp库。 然后,在Windows Forms应用程序中,你需要添加一个PictureBox控件和一个Button控件。当点击按钮时,将触发获取RTSP视频流并显示在PictureBox上的操作。 ```csharp using System; using System.Drawing; using System.Windows.Forms; using OpenCvSharp; namespace RTSPPlayer { public partial class Form1 : Form { private VideoCapture videoCapture; private bool isPlaying; public Form1() { InitializeComponent(); } private void btnStart_Click(object sender, EventArgs e) { string rtspUrl = "rtsp://example.com/stream"; // RTSP流的URL videoCapture = new VideoCapture(rtspUrl); isPlaying = true; // 创建一个定时器,在固定的时间间隔内刷新视频帧 Timer timer = new Timer(); timer.Interval = 33; // 每秒约30帧 timer.Tick += Timer_Tick; timer.Start(); } private void Timer_Tick(object sender, EventArgs e) { if (isPlaying) { // 从视频捕捉设备中获取下一帧 Mat frame = new Mat(); videoCapture.Read(frame); // 将帧转换为图像 Bitmap image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(frame); // 在PictureBox上显示图像 pictureBox.Image = image; // 释放帧资源 frame.Release(); } } private void btnStop_Click(object sender, EventArgs e) { isPlaying = false; // 释放视频捕捉设备资源 if (videoCapture != null) { videoCapture.Release(); videoCapture.Dispose(); videoCapture = null; } // 清空PictureBox的图像 pictureBox.Image = null; } } } ``` 在上述代码中,我们使用OpenCvSharp库创建一个VideoCapture对象来打开RTSP流。然后,我们使用一个定时器来从视频捕捉设备中获取下一帧,并将其转换为图像以在PictureBox上显示。 请注意,上述代码只是一个简单的示例,你可能需要根据实际需求进行修改和扩展。另外,为了避免阻塞UI线程,你可能需要使用异步操作来获取视频帧数据。 希望这可以帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿蒙Amon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值