void OnUpdate(object sender, Nui.SkeletonFrameReadyEventArgs e) { // KINECT Add code to get joint data and smooth it if (Application.Current.MainWindow == null || MainWindow.Instance == null) return; Nui.SkeletonData skeleton = null; PreviousCursorPosition = CursorPosition; if (e.SkeletonFrame == null) { CursorPosition = Mouse.GetPosition(Application.Current.MainWindow); } else { foreach (Nui.SkeletonData sd in e.SkeletonFrame.Skeletons) { if (sd.TrackingState == Nui.SkeletonTrackingState.Tracked) { skeleton = sd; break; } } if (skeleton == null) return; // Show User's Skeleton when they selects //if (MainWindow.Instance.showSkeletons) //{ drawJoints(skeleton, 1); //} #region Serialization // Initialize var _Skeleton = new KinectSkeleton { ankleLeft = skeleton.Joints[Nui.JointID.AnkleLeft], ankleRight = skeleton.Joints[Nui.JointID.AnkleRight], elbowLeft = skeleton.Joints[Nui.JointID.ElbowLeft], elbowRight = skeleton.Joints[Nui.JointID.ElbowRight], footLeft = skeleton.Joints[Nui.JointID.FootLeft], footRight = skeleton.Joints[Nui.JointID.FootRight], handLeft = skeleton.Joints[Nui.JointID.HandLeft], handRight = skeleton.Joints[Nui.JointID.HandRight], head = skeleton.Joints[Nui.JointID.Head], hipCenter = skeleton.Joints[Nui.JointID.HipCenter], hipLeft = skeleton.Joints[Nui.JointID.HipLeft], hipRight = skeleton.Joints[Nui.JointID.HipRight], kneeLeft = skeleton.Joints[Nui.JointID.KneeLeft], kneeRight = skeleton.Joints[Nui.JointID.KneeRight], shoulderCenter = skeleton.Joints[Nui.JointID.ShoulderCenter], shoulderLeft = skeleton.Joints[Nui.JointID.ShoulderLeft], shoulderRight = skeleton.Joints[Nui.JointID.ShoulderRight], spine = skeleton.Joints[Nui.JointID.Spine], wristLeft = skeleton.Joints[Nui.JointID.WristLeft], wristRight = skeleton.Joints[Nui.JointID.WristRight] }; var _Serializer = new XmlSerializer(typeof(KinectSkeleton)); StringBuilder _Builder = new StringBuilder(); using (StringWriter _Writer = new StringWriter(_Builder)) { _Serializer.Serialize(_Writer, _Skeleton); } m_xmlSkeleton = _Builder.ToString(); #endregion Serialization Nui.Vector nuiv = skeleton.Joints[Nui.JointID.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 1.0f, 0.5f).Position; CursorPosition = new Point(nuiv.X, nuiv.Y); } // Update which graphical element the cursor is currently over if (!Passive) UpdateElementOver(); if (MainWindow.Instance.NuiRuntime == null) { // For mouse, see if the right mouse button is down. if (_isPainting) { if (Mouse.LeftButton == MouseButtonState.Released) { // MainWindow.Instance.StopPainting(); } } else { if (Mouse.LeftButton == MouseButtonState.Pressed) { // MainWindow.Instance.StartPainting(); } } _isPainting = Mouse.LeftButton == MouseButtonState.Pressed; } else { if (MainWindow.Instance.isUserConnected) { // Send my cursor position to remote user MainWindow.Instance.SendCursorPosition((int)CursorPosition.X, (int)CursorPosition.Y, m_xmlSkeleton); } // To begin painting w/ Kinect, raise your left hand above your shoulder. // To stop painting, lower it. Nui.Joint lh = skeleton.Joints[Nui.JointID.HandLeft]; Nui.Joint ls = skeleton.Joints[Nui.JointID.ShoulderLeft]; bool isup = lh.Position.Y > ls.Position.Y; if (isup != _isPainting) { _isPainting = isup; if (_isPainting){ // MainWindow.Instance.StartPainting(); } else{ // MainWindow.Instance.StopPainting(); } } // Image number int number = MainWindow.Instance.rectangleNumber; if (_isSlected && (number != 0)) { Rectangle selectedRectangle = new Rectangle(); switch (number) { case 0: break; case 1: selectedRectangle = MainWindow.Instance.R1; break; case 2: selectedRectangle = MainWindow.Instance.R2; break; case 3: selectedRectangle = MainWindow.Instance.R3; break; case 4: selectedRectangle = MainWindow.Instance.R4; break; case 5: selectedRectangle = MainWindow.Instance.R5; break; case 6: selectedRectangle = MainWindow.Instance.R6; break; case 7: selectedRectangle = MainWindow.Instance.R7; break; case 8: selectedRectangle = MainWindow.Instance.R8; break; case 9: selectedRectangle = MainWindow.Instance.R9; break; } if (selectedRectangle != null) { // When rectangle is moving selectedRectangle.Opacity = 0.5; // selectedRectangle.IsHitTestVisible = false; Canvas.SetZIndex(selectedRectangle, 10); if (isup) { _isSlected = false; selectedRectangle.Opacity = 1.0; // selectedRectangle.IsHitTestVisible = true; Canvas.SetZIndex(selectedRectangle, 0); // Send which image is moving to the remote user MainWindow.Instance.SendMovingImage(number, _isSlected); } else { // Set moving image position on the Canvas // Canvas.SetLeft(selectedRectangle, CursorPosition.X - selectedRectangle.ActualWidth / 2); // Canvas.SetTop(selectedRectangle, CursorPosition.Y - (140 + selectedRectangle.ActualHeight / 2)); // Canvas.SetLeft(selectedRectangle, CursorPosition.X - selectedRectangle.ActualWidth / 2 - 140); // Canvas.SetTop(selectedRectangle, CursorPosition.Y - (140 + selectedRectangle.ActualHeight / 2)); Canvas.SetLeft(selectedRectangle, CursorPosition.X - selectedRectangle.ActualWidth / 2); Canvas.SetTop(selectedRectangle, CursorPosition.Y - selectedRectangle.ActualHeight / 2); // Send which image is moving to the remote user MainWindow.Instance.SendMovingImage(number, _isSlected); } } } } }
private Point getDisplayPosition(Nui.Joint joint) { float depthX, depthY; MainWindow.Instance.NuiRuntime.SkeletonEngine.SkeletonToDepthImage(joint.Position, out depthX, out depthY); depthX = depthX * 320; //convert to 320, 240 space depthY = depthY * 240; //convert to 320, 240 space int colorX, colorY; Nui.ImageViewArea iv = new Nui.ImageViewArea(); // only ImageResolution.Resolution640x480 is supported at this point MainWindow.Instance.NuiRuntime.NuiCamera.GetColorPixelCoordinatesFromDepthPixel(Nui.ImageResolution.Resolution640x480, iv, (int)depthX, (int)depthY, (short)0, out colorX, out colorY); // map back to canvas1.Width & canvas1.Height //Console.WriteLine("main window width: " + MainWindow.Instance.canvas1.Width); //Console.WriteLine("main window height: " + MainWindow.Instance.canvas1.Height); return new Point((double)(MainWindow.Instance.canvas1.ActualWidth * colorX / 640.0), (double)(MainWindow.Instance.canvas1.ActualHeight * 1.2 * colorY / 480)); }
void drawJoints(Nui.SkeletonData data, int playerIndx) { // Draw bones Brush brush = Brushes.Black; ArrayList CurrentBodySegments = new ArrayList(); ArrayList CurrentBodyJoints = new ArrayList(); switch (playerIndx) { case 1: brush = Brushes.Green; CurrentBodySegments = BodySegments; CurrentBodyJoints = BodyJoints; break; } //remove current skeleton from canvas foreach (Polyline p in CurrentBodySegments) { MainWindow.Instance.canvas1.Children.Remove(p); } //clear BodySegments Array CurrentBodySegments.Clear(); if (data != null) { //BodySegments add CurrentBodySegments.Add(getBodySegment(data.Joints, brush, Nui.JointID.HipCenter, Nui.JointID.Spine, Nui.JointID.ShoulderCenter, Nui.JointID.Head)); CurrentBodySegments.Add(getBodySegment(data.Joints, brush, Nui.JointID.ShoulderCenter, Nui.JointID.ShoulderLeft, Nui.JointID.ElbowLeft, Nui.JointID.WristLeft, Nui.JointID.HandLeft)); CurrentBodySegments.Add(getBodySegment(data.Joints, brush, Nui.JointID.ShoulderCenter, Nui.JointID.ShoulderRight, Nui.JointID.ElbowRight, Nui.JointID.WristRight, Nui.JointID.HandRight)); CurrentBodySegments.Add(getBodySegment(data.Joints, brush, Nui.JointID.HipCenter, Nui.JointID.HipLeft, Nui.JointID.KneeLeft, Nui.JointID.AnkleLeft, Nui.JointID.FootLeft)); CurrentBodySegments.Add(getBodySegment(data.Joints, brush, Nui.JointID.HipCenter, Nui.JointID.HipRight, Nui.JointID.KneeRight, Nui.JointID.AnkleRight, Nui.JointID.FootRight)); //BodySegments.Add(getBodySegment(data.Joints, brush, JointID.ShoulderRight, JointID.HipRight, JointID.HipLeft, JointID.ShoulderLeft)); foreach (Polyline p in CurrentBodySegments) { p.Opacity = 0.50; // Show Body Skeletons when 'showSkeletons' is true if (MainWindow.Instance.showSkeletons) { MainWindow.Instance.canvas1.Children.Add(p); } else { MainWindow.Instance.canvas1.Children.Remove(p); } } } foreach (UIElement l in CurrentBodyJoints) { MainWindow.Instance.canvas1.Children.Remove(l); } CurrentBodyJoints.Clear(); // Draw joints if (data != null) { foreach (Nui.Joint joint in data.Joints) { // Console.WriteLine("join name: " + joint.ID); Point jointPos = getDisplayPosition(joint); if (joint.ID == Nui.JointID.Head) { // Console.WriteLine("FOUND THE HEAD"); Ellipse el = new Ellipse(); el.Width = 50; el.Height = 50; el.Fill = Brushes.Green; el.Opacity = 0.20; Canvas.SetLeft(el, (int)jointPos.X - 25); Canvas.SetTop(el, (int)jointPos.Y - 25); CurrentBodyJoints.Add(el); TextBlock t1 = new TextBlock(); t1.Text = "Me"; t1.FontSize = 30; t1.Width = 50; t1.Height = 50; Canvas.SetLeft(t1, (int)jointPos.X - 25 + 80); Canvas.SetTop(t1, (int)jointPos.Y - 25 - 20); CurrentBodyJoints.Add(t1); } else if (joint.ID == Nui.JointID.HandRight) { Ellipse el = new Ellipse(); el.Width = 20; el.Height = 20; el.Fill = Brushes.Black; el.Opacity = 0.20; Canvas.SetLeft(el, (int)jointPos.X - 25); Canvas.SetTop(el, (int)jointPos.Y - 25); CurrentBodyJoints.Add(el); } } foreach (UIElement l in CurrentBodyJoints) { // Show Head, RightHand, Name if (MainWindow.Instance.showSkeletons) { MainWindow.Instance.canvas1.Children.Add(l); } else { MainWindow.Instance.canvas1.Children.Remove(l); } } } }
private Point3D escalarPosicion(KinectNui.Vector posicion) { double z = 2 * factorConversion.Profundidad * posicion.Z - 3 * factorConversion.Profundidad; return new Point3D(2 * posicion.X * factorConversion.Ancho, 2 * posicion.Y * factorConversion.Ancho, z); }
void OnUpdate(object sender, Nui.SkeletonFrameReadyEventArgs e) { // KINECT Add code to get joint data and smooth it if (Application.Current.MainWindow == null || MainWindow.Instance == null) return; Nui.SkeletonData skeleton = null; PreviousCursorPosition = CursorPosition; if (e.SkeletonFrame == null) { CursorPosition = Mouse.GetPosition(Application.Current.MainWindow); } else { foreach (Nui.SkeletonData sd in e.SkeletonFrame.Skeletons) if (sd.TrackingState == Nui.SkeletonTrackingState.Tracked) { skeleton = sd; break; } if (skeleton == null) return; if (skeleton == null) return; Nui.Vector nuiv = skeleton.Joints[Nui.JointID.HandRight].ScaleTo((int)ActualWidth, (int)ActualHeight, 0.50f, 0.30f).Position; CursorPosition = new Point(nuiv.X, nuiv.Y); } // Update which graphical element the cursor is currently over if (!Passive) UpdateElementOver(); if (MainWindow.Instance.NuiRuntime == null) { // For mouse, see if the right mouse button is down. if (_isPainting) { if (Mouse.LeftButton == MouseButtonState.Released) { // MainWindow.Instance.StopPainting(); } } else { if (Mouse.LeftButton == MouseButtonState.Pressed) { // MainWindow.Instance.StartPainting(); } } _isPainting = Mouse.LeftButton == MouseButtonState.Pressed; } else { // Send my cursor position to remote user MainWindow.Instance.SendCursorPosition((int)CursorPosition.X, (int)CursorPosition.Y); // To begin painting w/ Kinect, raise your left hand above your shoulder. // To stop painting, lower it. Nui.Joint lh = skeleton.Joints[Nui.JointID.HandLeft]; Nui.Joint ls = skeleton.Joints[Nui.JointID.ShoulderLeft]; bool isup = lh.Position.Y > ls.Position.Y; if (isup != _isPainting) { _isPainting = isup; if (_isPainting){ // MainWindow.Instance.StartPainting(); } else{ // MainWindow.Instance.StopPainting(); } } if (_isSlected) { Rectangle selectedRectangle = new Rectangle(); // Image number int number = MainWindow.Instance.rectangleNumber; switch (number) { case 0: break; case 1: selectedRectangle = MainWindow.Instance.R1; break; case 2: selectedRectangle = MainWindow.Instance.R2; break; case 3: selectedRectangle = MainWindow.Instance.R3; break; case 4: selectedRectangle = MainWindow.Instance.R4; break; case 5: selectedRectangle = MainWindow.Instance.R5; break; case 6: selectedRectangle = MainWindow.Instance.R6; break; } if (selectedRectangle != null) { // When rectangle is moving selectedRectangle.Opacity = 0.5; // selectedRectangle.IsHitTestVisible = false; Canvas.SetZIndex(selectedRectangle, 10); if (isup) { _isSlected = false; selectedRectangle.Opacity = 1.0; // selectedRectangle.IsHitTestVisible = true; Canvas.SetZIndex(selectedRectangle, 0); // Send which image is moving to the remote user MainWindow.Instance.SendMovingImage(number, _isSlected); } else { // Set moving image position on the Canvas Canvas.SetLeft(selectedRectangle, CursorPosition.X - selectedRectangle.ActualWidth / 2); Canvas.SetTop(selectedRectangle, CursorPosition.Y - (140 + selectedRectangle.ActualHeight / 2)); // Send which image is moving to the remote user MainWindow.Instance.SendMovingImage(number, _isSlected); } } } } }
void OnKinectVideoReady(object sender, KinectNui.ImageFrameReadyEventArgs e) { _timer.Start(); _bitmap = BitmapSource.Create(e.ImageFrame.Image.Width, e.ImageFrame.Image.Height, 96, 96, PixelFormats.Bgr32, null, e.ImageFrame.Image.Bits, e.ImageFrame.Image.Width * e.ImageFrame.Image.BytesPerPixel ); var planarImage = e.ImageFrame.Image; if (imageHelper == null) { imageHelper = new InteropBitmapHelper(planarImage.Width, planarImage.Height, planarImage.Bits); kinectVideo.Source = imageHelper.InteropBitmap; } else { imageHelper.UpdateBits(planarImage.Bits); } }