当前位置:   article > 正文

C# OpenCvSharp 读取rtsp流_c# rtsp

c# rtsp

目录

效果

项目

代码

下载 


效果

项目

代码

using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OpenCvSharp_读取rtsp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        CancellationTokenSource cts;
        VideoCapture capture;

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBoxIP.Text == "" || textBoxPort.Text == "" ||
                textBoxUserName.Text == "" || textBoxPassword.Text == "")
            {
                MessageBox.Show("Please input IP, Port, User name and Password!");
                return;
            }

            String rtspUrl = string.Format("rtsp://{0}:{1}@{2}:{3}"
                , textBoxUserName.Text
                , textBoxPassword.Text
                , textBoxIP.Text
                , textBoxPort.Text
                );

            button1.Enabled = false;
            button2.Enabled = true;

            cts = new CancellationTokenSource();
            Task task = new Task(() =>
            {
                capture.Open(rtspUrl, VideoCaptureAPIs.ANY);
                if (capture.IsOpened())
                {
                    //var dsize = new System.Windows.Size(capture.FrameWidth, capture.FrameHeight);
                    Mat frame = new Mat();
                    while (true)
                    {
                        Thread.Sleep(10);
                        //判断是否被取消;
                        if (cts.Token.IsCancellationRequested)
                        {
                            pictureBox1.Image = null;
                            return;
                        }
                        //Read image
                        capture.Read(frame);
                        if (frame.Empty())
                            continue;

                        if (pictureBox1.Image != null)
                        {
                            pictureBox1.Image.Dispose();
                        }
                        pictureBox1.Image = BitmapConverter.ToBitmap(frame);
                    }
                }

            }, cts.Token);
            task.Start();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            capture = new VideoCapture();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            button1.Enabled = true;
            cts.Cancel();
            
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (capture != null)
            {
                capture.Release();
                capture.Dispose();
            }
        }
    }
}
 

  1. using OpenCvSharp;
  2. using OpenCvSharp.Extensions;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace OpenCvSharp_读取rtsp流
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21. CancellationTokenSource cts;
  22. VideoCapture capture;
  23. private void button1_Click(object sender, EventArgs e)
  24. {
  25. if (textBoxIP.Text == "" || textBoxPort.Text == "" ||
  26. textBoxUserName.Text == "" || textBoxPassword.Text == "")
  27. {
  28. MessageBox.Show("Please input IP, Port, User name and Password!");
  29. return;
  30. }
  31. String rtspUrl = string.Format("rtsp://{0}:{1}@{2}:{3}"
  32. , textBoxUserName.Text
  33. , textBoxPassword.Text
  34. , textBoxIP.Text
  35. , textBoxPort.Text
  36. );
  37. button1.Enabled = false;
  38. button2.Enabled = true;
  39. cts = new CancellationTokenSource();
  40. Task task = new Task(() =>
  41. {
  42. capture.Open(rtspUrl, VideoCaptureAPIs.ANY);
  43. if (capture.IsOpened())
  44. {
  45. //var dsize = new System.Windows.Size(capture.FrameWidth, capture.FrameHeight);
  46. Mat frame = new Mat();
  47. while (true)
  48. {
  49. Thread.Sleep(10);
  50. //判断是否被取消;
  51. if (cts.Token.IsCancellationRequested)
  52. {
  53. pictureBox1.Image = null;
  54. return;
  55. }
  56. //Read image
  57. capture.Read(frame);
  58. if (frame.Empty())
  59. continue;
  60. if (pictureBox1.Image != null)
  61. {
  62. pictureBox1.Image.Dispose();
  63. }
  64. pictureBox1.Image = BitmapConverter.ToBitmap(frame);
  65. }
  66. }
  67. }, cts.Token);
  68. task.Start();
  69. }
  70. private void Form1_Load(object sender, EventArgs e)
  71. {
  72. capture = new VideoCapture();
  73. }
  74. private void button2_Click(object sender, EventArgs e)
  75. {
  76. button2.Enabled = false;
  77. button1.Enabled = true;
  78. cts.Cancel();
  79. }
  80. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  81. {
  82. if (capture != null)
  83. {
  84. capture.Release();
  85. capture.Dispose();
  86. }
  87. }
  88. }
  89. }

下载 

Demo下载

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号