c#显示mjpg-stream流

 

上图为Form1,主窗体。

引用AForge.Controls.dll后,工具箱出现控件,videosourceplayer就是我们要放到窗体中的视频流播放器控件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge;
using AForge.Controls;
using AForge.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;


namespace shujuku
{
    public partial class window2 : UserControl
    {
        private const int statLength = 15;
        private int statIndex = 0;
        private int statReady = 0;
        private int[] statCount = new int[statLength];
        public window2()
        {
            InitializeComponent();
        }

        private void videoSourcePlayer1_Click(object sender, EventArgs e)
        {

        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (videoSourcePlayer.VideoSource != null)
            {
                videoSourcePlayer.SignalToStop();
                videoSourcePlayer.WaitForStop();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MJPEGStream mjpegSource = new MJPEGStream("http://192.168.43.22:8080/?action=stream");
            OpenVideoSource(mjpegSource);
            Class1.IsOpenOK = false;

        }
        private void OpenVideoSource(IVideoSource source)
        {
            this.Cursor = Cursors.WaitCursor;
            videoSourcePlayer.SignalToStop();
            videoSourcePlayer.WaitForStop();
            videoSourcePlayer.VideoSource = source;
            videoSourcePlayer.Start();
            statIndex = statReady = 0;
            timer1.Start();
            this.Cursor = Cursors.Default;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            IVideoSource videoSource = videoSourcePlayer.VideoSource;
            if (videoSource != null)
            {
                statCount[statIndex] = videoSource.FramesReceived;
                if (++statIndex >= statLength)
                    statIndex = 0;
                if (statReady < statLength)
                    statReady++;
                float fps = 0;
                for (int i = 0; i < statReady; i++)
                {
                    fps += statCount[i];
                }
                fps /= statReady;
                statCount[statIndex] = 0;
                //  fpsLabel.Text = fps.ToString("F2") + " fps";
            }
        }
        private void videoSourcePlayer_NewFrame(object sender, ref Bitmap image)
        {
            DateTime now = DateTime.Now;
            Graphics g = Graphics.FromImage(image);
            SolidBrush brush = new SolidBrush(Color.Red);
            g.DrawString(now.ToString(), this.Font, brush, new PointF(5, 5));
            brush.Dispose();
            g.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            JPEGStream jpegSource = new JPEGStream("http://192.168.43.22:8080/?action=snap");
            OpenVideoSource(jpegSource);
        }
    }
}
 

使用python-opencv调用mjpg-stream视频可以通过以下几个步骤实现: 1. 首先,我们需要安装python-opencv库。可以使用pip命令进行安装,如下所示: ``` pip install opencv-python ``` 2. 导入所需的库。我们需要导入opencv模块,以及用于从URL中读取视频的模块urllib.request。代码如下: ```python import cv2 import urllib.request ``` 3. 定义视频的URL。根据实际情况,将URL替换为相应的mjpg-stream视频的URL。代码如下: ```python stream_url = "http://example.com/stream/video.mjpg" ``` 4. 使用urllib.request.urlopen()方法打开视频URL,并读取其中的数据。代码如下: ```python stream = urllib.request.urlopen(stream_url) ``` 5. 使用cv2.VideoCapture()方法创建一个视频捕获对象,然后使用read()方法从视频中读取每一帧图像数据。代码如下: ```python capture = cv2.VideoCapture(stream) ret, frame = capture.read() ``` 6. 在一个循环中,不断读取视频中的每一帧图像数据,然后进行后续的处理。例如,可以显示图像、保存图像等。代码如下: ```python while True: ret, frame = capture.read() cv2.imshow("Video Stream", frame) # 处理图像数据的其他操作 if cv2.waitKey(1) == ord('q'): break capture.release() cv2.destroyAllWindows() ``` 在以上步骤中,我们首先安装了python-opencv库,然后导入所需的库,之后定义了视频的URL。接着,通过urllib.request库打开视频URL,并创建了一个视频捕获对象。最后,利用一个循环读取每一帧图像数据,并进行相应的处理,例如显示图像或保存图像。最后,释放资源并关闭窗口。 以上就是使用python-opencv调用mjpg-stream视频的基本过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值