Emgu.CV 播放视频-本地文件/RTSP流


using Emgu.CV;

using System;

using System.Drawing;

using System.Threading;

using System.Windows.Forms;

 

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        Emgu.CV.Capture cap;

        public Form1()

        {

            InitializeComponent();

            //cap = new Emgu.CV.Capture("F:\\test.avi");

            cap = new Emgu.CV.Capture("rtsp://192.168.1.6.............");

            cap.ImageGrabbed += capture_ImageGrabbed;

            cap.Start();

 

        }

 

        private delegate void SetPicVideo(Bitmap val);//跨线程修改图片框

        private object lockObj = new object();

        private Thread SetPicVideoThread;

        Bitmap bmpVideo = null;

        private void capture_ImageGrabbed(object sender, EventArgs e)

        {

            try

            {

                Mat frame = new Mat();

                //lock (lockObj)

                {

                    if (cap != null)

                    {

                       

                        if (!cap.Retrieve(frame))

                        {

                            frame.Dispose();

                            return;

                        }

 

                        if (frame.IsEmpty)

                            return;

 

                        bmpVideo =  frame.Bitmap;

                        SetPicVideoThread = new Thread(new ThreadStart(setPicVideo));

                        SetPicVideoThread.IsBackground = true;

                        SetPicVideoThread.Start();

                    }

                }

 

                //frame.Dispose();

            }

            catch (Exception ex)

            {

 

            }

        }

 

        void SetPic(Bitmap val)

        {

            if (val != null)

            {

                this.pictureBox1.Image = val;

            }

        }

 

        private void setPicVideo()

        {

            if (pictureBox1.InvokeRequired)

            {

                SetPicVideo d = new SetPicVideo(SetPic);

                object[] arg = new object[] { bmpVideo };//要传入的参数值

                this.Invoke(d, arg);

            }

            else

            {

                SetPic(bmpVideo);

            }

        }

 

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)

        {

            cap.Stop();

        }

    }

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM