FrmCameraPlay.cs 5.5 KB
using AccAOI.camera;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.Common;
using TSA_V.DeviceLibrary;

namespace TSA_V
{
    public partial class FrmCameraPlay : Form
    {
        public bool ISShow = false;
        public int ScreenIndex = ConfigAppSettings.GetIntValue(Setting_Init.VideoFrom_Index, 2);
        public bool NeedShow = ConfigAppSettings.GetBoolValue(Setting_Init.VideoForm_Show);
        public static FrmCameraPlay instance = new FrmCameraPlay();
        public FrmCameraPlay()
        {
            //NeedShow = false;
            InitializeComponent();
        }

        private void FrmCameraPlay_Load(object sender, EventArgs e)
        {
        }
        public bool ShowForm()
        {
            ISShow = true;
            if (!NeedShow)
            {
                return false;
            }
            Screen[] sc = Screen.AllScreens;
            Screen screen = null;
            if (sc.Count() > ScreenIndex)
            {
                screen = sc[ScreenIndex];

                int ScreenWidth = screen.Bounds.Width;
                int ScreenHeight = screen.Bounds.Height;
                this.Width = ScreenWidth;
                this.Height = ScreenWidth;

                this.StartPosition = FormStartPosition.Manual;
                this.WindowState = FormWindowState.Maximized;
                this.Location = new Point(0, 0);
                this.ShowDialog();
                LogUtil.info("实时视频界面显示:屏幕索引=" + ScreenIndex + " !");
                return true;
            }
            else if (sc.Count() > 0)
            {
                screen = sc[0];
                this.StartPosition = FormStartPosition.Manual;
                this.Size = new Size(400, 300);
                this.Location = new Point(screen.Bounds.Left + screen.Bounds.Width - this.Width, screen.Bounds.Top + 5);
                this.ShowDialog();
                LogUtil.info("实时视频界面显示:屏幕索引=" + ScreenIndex + " !");
                return true;
            }
            else
            {
                LogUtil.info("实时视频界面显示:屏幕索引=" + ScreenIndex + " 未找到");
                return false;
            }

        }

        private void FrmCameraPlay_Shown(object sender, EventArgs e)
        {
            timer1.Start();
            this.TopMost = true;
            CameraManager.camera_event += CameraManager_camera_event;
            CameraManager.LoadCamera();
            CameraManager.StartGetImage(CameraName());


            //CameraManager.LoadCamera();
            //HIKCamera.Instance.camera_event += CameraManager_camera_event;
            //CameraManager.StartCallBackGrabImage(CameraName());

        }
         

        private void CameraManager_camera_event(object sender, Bitmap e)
        {
            try
            {
                if (e != null)
                {
                    if (pictureBox1.Image != null)
                    {
                        pictureBox1.Image.Dispose();
                    }
                    pictureBox1.Image = e;
                } 
            }
            catch (Exception ex)
            {
                LogUtil.error("CameraManager_camera_event 出错:" + ex.ToString());
            }
        }
        private string CameraName()
        {
            string camera = ConfigAppSettings.GetValue(Setting_Init.CameraName);
            if (camera == "")
            {
                camera = AccAOI.camera.CameraManager.hikNameList.ToArray().FirstOrDefault();
            }
            return camera;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if(this.Visible  )
            {
                try
                {
                    //string camera = ConfigAppSettings.GetValue(Setting_Init.CameraName);
                    //if (camera == "")
                    //{
                    //    camera = AccAOI.camera.CameraManager.hikNameList.ToArray().FirstOrDefault();
                    //}
                    //Image currImage = CameraManager.GetCamerImage(camera);

                    //if (currImage == null)
                    //{
                    //    string file = @"G:\test.bmp";
                    //    if (File.Exists(file))
                    //    {
                    //        //从本地加载图片 
                    //        Bitmap aa = new Bitmap(file);
                    //        currImage = ImageManager.DeepClone(aa);
                    //    }
                    //}

                    //if (currImage != null)
                    //{
                    //    if (pictureBox1.Image != null)
                    //    {
                    //        pictureBox1.Image.Dispose();
                    //    }
                    //    pictureBox1.Image = currImage;
                    //}
                }
                catch (Exception ex)
                {
                    LogUtil.error("实时视频播放出错:" + ex.ToString());
                }
            }
        }

        private void FrmCameraPlay_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }
         

        private void FrmCameraPlay_FormClosed(object sender, FormClosedEventArgs e)
        {
            CameraManager.StopGetImage();
        }
    } 
}