Esempio n. 1
0
        protected override void OnPaint(PaintEventArgs args)
        {
            base.OnPaint(args);

            // Update Nuitrack data. Data will be synchronized with skeleton time stamps.
            try
            {
                Nuitrack.Update(_skeletonTracker);
            }
            catch (LicenseNotAcquiredException exception)
            {
                Console.WriteLine("LicenseNotAcquired exception. Exception: ", exception);
                throw exception;
            }
            catch (Exception exception)
            {
                Console.WriteLine("Nuitrack update failed. Exception: ", exception);
            }

            // Draw a bitmap
            args.Graphics.DrawImage(_bitmap.Bitmap, new Point(0, 0));

            // Draw skeleton joints
            if (_skeletonData != null)
            {
                const int jointSize = 10;
                foreach (var skeleton in _skeletonData.Skeletons)
                {
                    SolidBrush brush = new SolidBrush(Color.FromArgb(255 - 40 * skeleton.ID, 0, 0));
                    foreach (var joint in skeleton.Joints)
                    {
                        args.Graphics.FillEllipse(brush, joint.Proj.X * _bitmap.Width - jointSize / 2,
                                                  joint.Proj.Y * _bitmap.Height - jointSize / 2, jointSize, jointSize);
                    }
                }
            }

            // Draw hand pointers
            if (_handTrackerData != null)
            {
                foreach (var userHands in _handTrackerData.UsersHands)
                {
                    if (userHands.LeftHand != null)
                    {
                        HandContent hand  = userHands.LeftHand.Value;
                        int         size  = hand.Click ? 20 : 30;
                        Brush       brush = new SolidBrush(Color.Aquamarine);
                        args.Graphics.FillEllipse(brush, hand.X * _bitmap.Width - size / 2, hand.Y * _bitmap.Height - size / 2, size, size);
                    }

                    if (userHands.RightHand != null)
                    {
                        HandContent hand  = userHands.RightHand.Value;
                        int         size  = hand.Click ? 20 : 30;
                        Brush       brush = new SolidBrush(Color.DarkBlue);
                        args.Graphics.FillEllipse(brush, hand.X * _bitmap.Width - size / 2, hand.Y * _bitmap.Height - size / 2, size, size);
                    }
                }
            }

            // Update Form
            this.Invalidate();
        }
        protected override void OnPaint(PaintEventArgs args)
        {
            int exit_cnt = 0;

            base.OnPaint(args);

            // Update Nuitrack data. Data will be synchronized with skeleton time stamps.
            try
            {
                Nuitrack.Update(_skeletonTracker);
            }
            catch (LicenseNotAcquiredException exception)
            {
                Console.WriteLine("LicenseNotAcquired exception. Exception: ", exception);
                throw exception;
            }
            catch (Exception exception)
            {
                Console.WriteLine("Nuitrack update failed. Exception: ", exception);
            }

            // Draw a bitmap
            args.Graphics.DrawImage(_bitmap.Bitmap, new Point(0, 0));

            // Draw skeleton joints
            if (_skeletonData != null)
            {
                Database_set();
                using (conn = new MySqlConnection(DB_INFO))
                {
                    if (conn.State != System.Data.ConnectionState.Open)
                    {
                        conn.Open();
                    }


                    /*
                     * If not exist database of userId, DB creation
                     * If exist, skip
                     */
                    try
                    {
                        string create_sql = "CREATE TABLE IF NOT EXISTS " + this.TBL_NAME + "(" +
                                            "TYPE VARCHAR(10) NOT NULL, " +
                                            "ANGLE INT NOT NULL, " +
                                            "TIME DATETIME NOT NULL" +
                                            ")";
                        MySqlCommand create_cmd = new MySqlCommand(create_sql, conn);
                        create_cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("CREATE TABLE USER_ID error!");
                        MessageBox.Show(ex.Message);
                        MessageBox.Show(ex.StackTrace);
                    }
                    finally
                    {
                        //Console.WriteLine("CREATE TABLE IF NOT EXISTS Success.");
                        conn.Close();
                    }
                }

                if (conn.State == System.Data.ConnectionState.Open)
                {
                    conn.Close();
                }

                const int jointSize = 10;
                foreach (var skeleton in _skeletonData.Skeletons)
                {
                    SolidBrush brush = new SolidBrush(Color.FromArgb(255 - 40 * skeleton.ID, 0, 0));

                    int _cnt = 0;
                    foreach (var joint in skeleton.Joints)
                    {
                        args.Graphics.FillEllipse(brush, joint.Proj.X * _bitmap.Width - jointSize / 2,
                                                  joint.Proj.Y * _bitmap.Height - jointSize / 2, jointSize, jointSize);


                        //Type & enum
                        //Console.WriteLine(joint.Type + " : " + joint.Proj.X + " " + joint.Proj.Y + " " + joint.Proj.Z);

                        _myAngle._myJoint[_cnt++].setValue
                        (
                            joint.Real.X,
                            joint.Real.Y,
                            joint.Real.Z
                        );
                    }


                    // print the data at about 1(sec) intervals
                    if (myCnt >= 1000)
                    {
                        int temp = (int)(GetJointAngle.AngleBetweenJoints
                                         (
                                             _myAngle._myJoint[angleInfo[this.SET_NUM, 0]],
                                             _myAngle._myJoint[angleInfo[this.SET_NUM, 1]],
                                             _myAngle._myJoint[angleInfo[this.SET_NUM, 2]]
                                         )
                                         );

                        using (conn = new MySqlConnection(DB_INFO))
                        {
                            if (conn.State != System.Data.ConnectionState.Open)
                            {
                                conn.Open();
                            }

                            DateTime now  = DateTime.Now;
                            string   date = now.ToString("yyyy-MM-dd H:mm:ss");

                            try
                            {
                                string sql = "INSERT INTO " + TBL_NAME + " VALUES(" +
                                             "'" + "SET " + SET_NUM + "'" + "," +
                                             "'" + temp + "'" + "," +
                                             "'" + date + "'" +
                                             ")";
                                MySqlCommand cmd = new MySqlCommand(sql, conn);
                                //cmd.Connection.Open();
                                cmd.ExecuteNonQuery();

                                if (exit_cnt++ >= 3)
                                {
                                    Application.Exit();
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("MySQL connection error!");
                                MessageBox.Show(ex.Message);
                                MessageBox.Show(ex.StackTrace);
                            }
                            finally
                            {
                                //Console.WriteLine("INSERT INTO TBL VALUES() Success.");
                                conn.Close();
                            }
                        }

                        Console.WriteLine(temp + " DB Insert !");
                        myCnt = 0;
                    }
                    else
                    {
                        myCnt++;
                    }
                }
            }

            // Draw hand pointers
            if (_handTrackerData != null)
            {
                foreach (var userHands in _handTrackerData.UsersHands)
                {
                    if (userHands.LeftHand != null)
                    {
                        HandContent hand  = userHands.LeftHand.Value;
                        int         size  = hand.Click ? 20 : 30;
                        Brush       brush = new SolidBrush(Color.Aquamarine);
                        args.Graphics.FillEllipse(brush, hand.X * _bitmap.Width - size / 2, hand.Y * _bitmap.Height - size / 2, size, size);
                    }

                    if (userHands.RightHand != null)
                    {
                        HandContent hand  = userHands.RightHand.Value;
                        int         size  = hand.Click ? 20 : 30;
                        Brush       brush = new SolidBrush(Color.DarkBlue);
                        args.Graphics.FillEllipse(brush, hand.X * _bitmap.Width - size / 2, hand.Y * _bitmap.Height - size / 2, size, size);
                    }
                }
            }

            // Update Form
            this.Invalidate();
        }