using System;
using System.Drawing;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System.Windows.Forms;
namespace UnNomalized_node
{
    public class OpenCamera
    {
        //更新图片线程用的委托回调变量
        internal delegate void UpdateImageDelegate();
        internal UpdateImageDelegate UpdateImageThreadCallBack_OC;

        /// <summary>
        /// 用于获取<see cref="OpenCameraWithOpencvSharp.MainFrm.pictureBox1"/>
        /// </summary>
        internal static PictureBox PictureBox { set; get; }
        /// <summary>
        /// 用于获取或存储摄像头设备变量
        /// </summary>
        internal static VideoCapture VideoCapture_OS;
        public OpenCamera()
        {

        }
        /// <summary>
        /// 构造方法，用于传递<see cref="OpenCameraWithOpencvSharp.MainFrm.pictureBox1"/>参数
        /// </summary>
        /// <param name="pictureBox"></param>
        public OpenCamera(PictureBox pictureBox)
        {
            PictureBox = pictureBox;

        }
        public void UpdatePictrueImage()
        {
            try
            {

                UpdateImageThreadCallBack_OC = new UpdateImageDelegate(UpdateImage);


                //跟新图片线程
                Thread thread = new Thread(new ThreadStart(UpdateImageThreadCallBack_OC))
                {
                    IsBackground = true
                };
                thread.Start();
                Thread.Sleep(40);
            }
            catch (Exception)
            {

                throw new Exception();
            }

        }
        /// <summary>
        /// 更新图片方法，用于<see cref="UpdateImageThreadCallBack_OC",传递方法/>
        /// </summary>
        private void UpdateImage()
        {
            
            while (true)
            {
                VideoCapture_OS = InitVideoCapture();
                Mat mat = new Mat();
            
                if (VideoCapture_OS.Read(mat))
                {
                    if (mat.Size().Width != 0 && mat.Size().Height != 0)
                    {
                        int h = PictureBox.Height;
                        int w = PictureBox.Width;
                        Mat mat_resize = mat.Resize(new OpenCvSharp.Size(w, h));
                        //Image image = (Image)mat_resize.ToBitmap();
                        
                        PictureBox.Invoke(new Action(() =>
                        {
                            if (PictureBox.Image != null)
                                PictureBox.Image.Dispose();
                            PictureBox.Image = BitmapConverter.ToBitmap(mat_resize);
                        }));

                        mat.Release();
                        mat_resize.Release();
                        
                    }
                }
                
                Thread.Sleep(33);

            }
        }

        /// <summary>
        /// 初始化摄像头并设置摄像头宽高参数
        /// </summary>
        /// <returns>摄像头调用对象</returns>
        private static VideoCapture InitVideoCapture()
        {
            if (VideoCapture_OS == null)
            {
                VideoCapture_OS = new OpenCvSharp.VideoCapture("http://192.168.2.4/doc/page/config.asp");

                //VideoCapture_OS.Set(CaptureProperty.FrameWidth, 1280);
                //VideoCapture_OS.Set(CaptureProperty.FrameHeight, 960);  
            }
            else
            {
                if(VideoCapture_OS.IsOpened()==false)
                {
                    VideoCapture_OS.Open("http://192.168.2.4/doc/page/config.asp");
                    //VideoCapture_OS.Set(CaptureProperty.FrameWidth, 1280);
                    //VideoCapture_OS.Set(CaptureProperty.FrameHeight, 960);
                    Thread.Sleep(500);
                } 
            }
            return VideoCapture_OS;
        }
    }
}

