Ejemplo n.º 1
0
 private void NiTeOnNewData(HandTracker handTracker)
 {
     try
     {
         if (Settings.Default.SmartCam && this.uTracker != null && this.uTracker.IsValid && this.hTracker != null &&
             this.hTracker.IsValid)
         {
             using (UserTrackerFrameRef userframe = this.uTracker.ReadFrame())
             {
                 using (HandTrackerFrameRef handframe = this.hTracker.ReadFrame())
                 {
                     foreach (GestureData gesture in handframe.Gestures)
                     {
                         if (!gesture.IsComplete)
                         {
                             continue;
                         }
                         PointF handPos = this.hTracker.ConvertHandCoordinatesToDepth(gesture.CurrentPosition);
                         short  userId  =
                             Marshal.ReadByte(
                                 userframe.UserMap.Pixels + (int)(handPos.Y * userframe.UserMap.DataStrideBytes)
                                 + (int)(handPos.X * 2));
                         if (userId > 0)
                         {
                             this.activeUserId = userId;
                         }
                     }
                     handframe.Release();
                 }
                 if (this.activeUserId > 0)
                 {
                     UserData user = userframe.GetUserById(this.activeUserId);
                     if (user.IsValid && user.IsVisible && user.CenterOfMass.Z > 0)
                     {
                         RectangleF position    = new RectangleF(0, 0, 0, 0);
                         PointF     botlocation = this.uTracker.ConvertJointCoordinatesToDepth(user.CenterOfMass);
                         int        pSize       =
                             (int)
                             (Math.Max((int)((4700 - user.CenterOfMass.Z) * 0.08), 50)
                              * ((float)userframe.UserMap.FrameSize.Height / 480));
                         position.Y                 = (int)botlocation.Y - pSize;
                         position.Height            = pSize;
                         position.X                 = (int)botlocation.X;
                         this.activePosition.X      = position.X / userframe.UserMap.FrameSize.Width;
                         this.activePosition.Width  = position.Width / userframe.UserMap.FrameSize.Width;
                         this.activePosition.Y      = position.Y / userframe.UserMap.FrameSize.Height;
                         this.activePosition.Height = position.Height / userframe.UserMap.FrameSize.Height;
                         userframe.Release();
                         return;
                     }
                 }
                 userframe.Release();
             }
         }
     }
     catch (Exception)
     {
     }
     this.activeUserId = 0;
 }
Ejemplo n.º 2
0
        // ReSharper disable once ParameterHidesMember
        private void HandTrackerOnNewData(HandTracker handTracker)
        {
            if (!handTracker.IsValid)
            {
                return;
            }

            HandTrackerFrameRef frame = handTracker.ReadFrame();

            if (frame == null || !frame.IsValid)
            {
                return;
            }

            lock (this.image)
            {
                using (VideoFrameRef depthFrame = frame.DepthFrame)
                {
                    if (this.image.Width != depthFrame.FrameSize.Width ||
                        this.image.Height != depthFrame.FrameSize.Height)
                    {
                        this.image = new Bitmap(
                            depthFrame.FrameSize.Width,
                            depthFrame.FrameSize.Height,
                            PixelFormat.Format24bppRgb);
                    }
                }

                using (Graphics g = Graphics.FromImage(this.image))
                {
                    g.FillRectangle(Brushes.Black, new Rectangle(new Point(0, 0), this.image.Size));
                    foreach (GestureData gesture in frame.Gestures)
                    {
                        if (gesture.IsComplete)
                        {
                            handTracker.StartHandTracking(gesture.CurrentPosition);
                        }
                    }

                    if (frame.Hands.Length == 0)
                    {
                        g.DrawString("Raise your hand", SystemFonts.DefaultFont, Brushes.White, 10, 10);
                    }
                    else
                    {
                        foreach (HandData hand in frame.Hands)
                        {
                            if (hand.IsTracking)
                            {
                                Point  handPosEllipse = new Point();
                                PointF handPos        = handTracker.ConvertHandCoordinatesToDepth(hand.Position);
                                handPosEllipse.X = (int)handPos.X - 5;
                                handPosEllipse.Y = (int)handPos.Y - 5;
                                g.DrawEllipse(new Pen(Brushes.White, 5), new Rectangle(handPosEllipse, new Size(5, 5)));
                            }
                        }
                    }

                    g.Save();
                }
            }

            this.Invoke(
                new MethodInvoker(
                    delegate
            {
                this.fps      = ((1000000 / (frame.Timestamp - this.lastTime)) + (this.fps * 4)) / 5;
                this.lastTime = frame.Timestamp;
                this.Text     = @"Frame #" + frame.FrameIndex + @" - Time: " + frame.Timestamp + @" - FPS: "
                                + this.fps;
                this.pb_preview.Image = this.image.Clone(
                    new Rectangle(new Point(0, 0), this.image.Size),
                    PixelFormat.Format24bppRgb);
                frame.Release();
            }));
        }