HandRecordManager.cs 3.7 KB
using System;
using System.Collections.Generic;
using System.Drawing; 
using TSA_V.Common;

namespace TSA_V.DeviceLibrary
{
    public class HandRecordManager
    {
        /// <summary>
        /// 开始统计时间
        /// </summary>
        private static  DateTime startTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 当前状态,0=无手势,>0有手势
        /// </summary>
        public static bool currHasPoint = false ;

        private static DateTime currStartTime = DateTime.Now;

        public  static int recordCount = 0;

        public static TimeSpan recordSpan = TimeSpan.Zero;

        private static Rectangle currRectangle =new Rectangle (0,0,0,0);

        private static  void LoadRect()
        {
            try
            { 
                string[] configs = Setting_NInit.Hand_RectConfig;
                if (configs.Length == 4)
                {
                    int x = Convert.ToInt32(configs[0]);
                    int y= Convert.ToInt32(configs[1]);
                    int w = Convert.ToInt32(configs[2]);
                    int h= Convert.ToInt32(configs[3]); 
                    currRectangle=new Rectangle(x,y,w,h); 
                }

            }
            catch (Exception ex)
            {
                LogUtil.error("出错:" + ex.ToString());
            }
        }

        public static  void Reset()
        {
            LogUtil.info($"重置手势统计,当前已统计:{recordCount}次,共{recordSpan.TotalSeconds}秒,当前状态:{currHasPoint}");
            //重置
            startTime = DateTime.Now;
            currHasPoint = false ;
            currStartTime = DateTime.Now;
            recordCount = 0;
            recordSpan = TimeSpan.Zero;
        }

        public static  void ProcessPoint(int pCount, List<List<int>> points)
        {
            try
            {
                if (currRectangle.Width <= 0 || currRectangle.Height <= 0)
                {
                    LoadRect();
                }

                bool isHas = false;
                if (pCount > 0 && pCount == points.Count)
                {
                    foreach (List<int> point in points)
                    {
                        if (point.Count == 3)
                        {
                            int index = point[0];
                            int x = point[1];
                            int y = point[2];

                            if (x > currRectangle.X && x < currRectangle.X + currRectangle.Width && y > currRectangle.Y && y < currRectangle.Y + currRectangle.Height)
                            {
                                isHas = true;
                                break;
                            }
                        }
                    }

                }

                if (isHas)
                {
                    if (currHasPoint)
                    {
                        return;
                    }
                    else
                    {
                        currHasPoint = true;
                        currStartTime = DateTime.Now;
                    }
                }
                else
                {
                    if (currHasPoint)
                    {
                        TimeSpan span = DateTime.Now - currStartTime;
                        recordCount++;
                        recordSpan += span;
                        currHasPoint = false;
                    }
                    else
                    {
                        currHasPoint = false;
                    }
                }

            }
            catch (Exception ex)
            {
                LogUtil.error("出错:" + ex.ToString());
            }
        }
    } 

}