IPCamera.cs 4.7 KB
using DL.CV;
using Emgu.CV;
using Emgu.CV.Structure;
using System;
using System.Drawing;
using System.Threading.Tasks;
namespace Dolen.CV
{
    public class IPCamera
    {
        private VideoCapture currentDevice;
        private VideoWriter videoWriter;
        private bool recording;
        bool isLive;
        private int videoWidth;
        private int videoHeight;
        public IPCameraConfig Config { get; set; }
        ///// <summary>
        ///// 记录保存的文件夹路径
        ///// </summary>
        //public string RecordDirectory { get; set; } = "";
        public int VideoWidth { get { return videoWidth; } }
        public int VideoHeight { get { return videoHeight; } }
        /// <summary>
        /// 相机是否连接
        /// </summary>
        public bool CamIsOpened
        {
            get
            {
                if (currentDevice != null)
                {
                    return currentDevice.IsOpened;
                }
                else
                    return false;
            }
        }
        public delegate void ImageGrabbedEventHandler(IPCameraEventArgs eventArgs);
        public event ImageGrabbedEventHandler ImageGrabbed;
        public IPCamera(IPCameraConfig cameraConfig)
        {
            Config = cameraConfig;
        }
        private void InitializeVariables(string rtsp)
        {
            currentDevice = new VideoCapture(rtsp);
            recording = false;
            videoWidth = currentDevice.Width;
            videoHeight = currentDevice.Height;
            currentDevice.ImageGrabbed += CurrentDevice_ImageGrabbed;
        }
        int cnt = 0;
        private void CurrentDevice_ImageGrabbed(object sender, EventArgs e)
        {
            try
            {
                if (cnt < 5)
                {
                    cnt++;
                    return;
                }
                cnt = 0;
                using (Mat m = new Mat())
                {
                    if (currentDevice.Retrieve(m, 0))
                    {
                        ImageGrabbed?.Invoke(new IPCameraEventArgs(Config, m.ToImage<Bgr, Byte>().ToBitmap()));
                        if (recording && videoWriter != null)
                        {
                            videoWriter.Write(m);
                        }
                    }
                }
            }
            catch
            {
            }
        }
        /// <summary>
        /// 开始实时
        /// </summary>
        /// <returns></returns>
        public bool StartLive()
        {
            if (isLive)
                return false;
            Task.Factory.StartNew(() =>
            {
                try
                {
                    isLive = true;
                    InitializeVariables(Config.ToString());
                    currentDevice.Start();
                    return true;
                }
                catch
                {
                    return false;
                }
            });
            return false;
        }
        /// <summary>
        /// 停止实时
        /// </summary>
        public void StopLive()
        {
            if (!isLive)
                return;
            Task.Factory.StartNew(() =>
            {
                currentDevice.Stop();
                isLive = false;
            });
        }
        /// <summary>
        /// 开始记录
        /// </summary>
        /// <param name="filename">文件名,无后缀</param>
        public void StartRecording(string filename)
        {
            if (recording)
                return;
            recording = true;
            videoWriter = new VideoWriter($"{filename}.mp4", VideoWriter.Fourcc('M', 'P', '4', 'V'), 30, new System.Drawing.Size(videoWidth, videoHeight), true);
        }
        /// <summary>
        /// 停止记录
        /// </summary>
        public bool StopRecording()
        {
            try
            {
                recording = false;
                if (videoWriter != null)
                {
                    videoWriter.Dispose();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
    public class IPCameraEventArgs : EventArgs
    {
        public string Name { get; set; }
        public Bitmap Image { get; set; }
        public string IP { get; set; }
        public int ChannelNum { get; set; }
        public DateTime UpdateTime { get; set; }

        public IPCameraEventArgs(IPCameraConfig config, Bitmap bitmap)
        {
            Name = config.Name;
            IP = config.IP;
            Image = bitmap;
            ChannelNum = config.ChannelNum;
            UpdateTime = DateTime.Now;
        }
    }
}