RED_IDX = 2; const int GREEN_IDX = 1; const int BLUE_IDX = 0; byte[] depthFrame32 = new byte[320 *240 * 4]; Dictionary<JointID,Brush>jointColors = new Dictionary<JointID,Brush>() { {JointID.HipCenter, newSolidColorBrush(Color.FromRgb(169, 176, 155))}, {JointID.Spine, newSolidColorBrush(Color.FromRgb(169, 176, 155))}, {JointID.ShoulderCenter, newSolidColorBrush(Color.FromRgb(168, 230, 29))}, {JointID.Head, newSolidColorBrush(Color.FromRgb(200, 0, 0))}, {JointID.ShoulderLeft, newSolidColorBrush(Color.FromRgb(79, 84, 33))}, {JointID.ElbowLeft, newSolidColorBrush(Color.FromRgb(84, 33, 42))}, {JointID.WristLeft, newSolidColorBrush(Color.FromRgb(255, 126, 0))}, {JointID.HandLeft, newSolidColorBrush(Color.FromRgb(215, 86,0))}, {JointID.ShoulderRight, newSolidColorBrush(Color.FromRgb(33, 79, 84))}, {JointID.ElbowRight, newSolidColorBrush(Color.FromRgb(33, 33, 84))}, {JointID.WristRight, newSolidColorBrush(Color.FromRgb(77, 109,243))}, {JointID.HandRight, newSolidColorBrush(Color.FromRgb(255, 126, 0))}, {JointID.HipLeft, newSolidColorBrush(Color.FromRgb(77, 109,243))}, {JointID.KneeLeft, newSolidColorBrush(Color.FromRgb(69, 33, 84))}, {JointID.AnkleLeft, newSolidColorBrush(Color.FromRgb(229, 170, 122))}, {JointID.FootLeft, newSolidColorBrush(Color.FromRgb(255, 126, 0))}, {JointID.HipRight, newSolidColorBrush(Color.FromRgb(181, 165, 213))}, {JointID.KneeRight, newSolidColorBrush(Color.FromRgb(71, 222, 76))}, {JointID.AnkleRight, newSolidColorBrush(Color.FromRgb(245, 228, 156))}, {JointID.FootRight, newSolidColorBrush(Color.FromRgb(77, 109,243))} }; private void Window_Loaded(objectsender, EventArgs e) { nui = new Runtime(); try { nui.Initialize(RuntimeOptions.UseDepthAndPlayerIndex |RuntimeOptions.UseSkeletalTracking | RuntimeOptions.UseColor); } catch (InvalidOperationException) { System.Windows.MessageBox.Show("Runtime initialization failed.Please make sure Kinect device is plugged in."); return; } try { nui.VideoStream.Open(ImageStreamType.Video,2, ImageResolution.Resolution640x480, ImageType.Color); nui.DepthStream.Open(ImageStreamType.Depth, 2,ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex); } catch (InvalidOperationException) { System.Windows.MessageBox.Show("Failed to open stream. Please makesure to specify a supported image type and resolution."); return; } lastTime = DateTime.Now; nui.DepthFrameReady += newEventHandler<ImageFrameReadyEventArgs>(nui_DepthFrameReady); nui.SkeletonFrameReady += newEventHandler<SkeletonFrameReadyEventArgs>(nui_SkeletonFrameReady); nui.VideoFrameReady += newEventHandler<ImageFrameReadyEventArgs>(nui_ColorFrameReady); } byte[] convertDepthFrame(byte[]depthFrame16) { for (int i16 = 0, i32 = 0; i16 <depthFrame16.Length && i32 < depthFrame32.Length; i16 += 2, i32 +=4) { int player = depthFrame16[i16] & 0x07; int realDepth =(depthFrame16[i16+1] << 5) | (depthFrame16[i16] >> 3); byte intensity = (byte)(255 -(255 * realDepth / 0x0fff)); depthFrame32[i32 + RED_IDX] =0; depthFrame32[i32 + GREEN_IDX] = 0; depthFrame32[i32 + BLUE_IDX] =0; switch (player) { case 0: depthFrame32[i32 +RED_IDX] = (byte)(intensity / 2); depthFrame32[i32 +GREEN_IDX] = (byte)(intensity / 2); depthFrame32[i32 +BLUE_IDX] = (byte)(intensity / 2); break; case 1: depthFrame32[i32 +RED_IDX] = intensity; break; case 2: depthFrame32[i32 +GREEN_IDX] = intensity; break; case 3: depthFrame32[i32 + RED_IDX]= (byte)(intensity / 4); depthFrame32[i32 +GREEN_IDX] = (byte)(intensity); depthFrame32[i32 +BLUE_IDX] = (byte)(intensity); break; case 4: depthFrame32[i32 + RED_IDX] =(byte)(intensity); depthFrame32[i32 +GREEN_IDX] = (byte)(intensity); depthFrame32[i32 +BLUE_IDX] = (byte)(intensity / 4); break; case 5: depthFrame32[i32 +RED_IDX] = (byte)(intensity); depthFrame32[i32 +GREEN_IDX] = (byte)(intensity / 4); depthFrame32[i32 +BLUE_IDX] = (byte)(intensity); break; case 6: depthFrame32[i32 +RED_IDX] = (byte)(intensity / 2); depthFrame32[i32 +GREEN_IDX] = (byte)(intensity / 2); depthFrame32[i32 +BLUE_IDX] = (byte)(intensity); break; case 7: depthFrame32[i32 +RED_IDX] = (byte)(255 - intensity); depthFrame32[i32 +GREEN_IDX] = (byte)(255 - intensity); depthFrame32[i32 + BLUE_IDX] = (byte)(255- intensity); break; } } return depthFrame32; } void nui_DepthFrameReady(object sender,ImageFrameReadyEventArgs e) { PlanarImage Image =e.ImageFrame.Image; byte[] convertedDepthFrame =convertDepthFrame(Image.Bits); ++totalFrames; DateTime cur = DateTime.Now; if (cur.Subtract(lastTime) >TimeSpan.FromSeconds(1)) { int frameDiff = totalFrames -lastFrames; lastFrames = totalFrames; lastTime = cur; //frameRate.Text =frameDiff.ToString() + " fps"; } } public void XYZ(Joint jtmd, Joint jtme) { this.TXD.Text =jtmd.Position.X.ToString(); this.TYD.Text =jtmd.Position.Y.ToString(); this.TZD.Text =jtmd.Position.Z.ToString(); this.TYE.Text = jtme.Position.Y.ToString(); var ParLib =Convert.ToDouble(this.ParLib.Text ); if (jtme.Position.Y <= ParLib) { this.TextStatus.Content ="ABERTO"; this.TextStatus.Foreground =Brushes.Red; Valor = 0; Paralela.Escrever(Convert.ToInt32("0378", 16),Convert.ToByte(Valor)); } else { Valor = 64; this.TextStatus.Content ="FECHADO"; this.TextStatus.Foreground =Brushes.Green; Paralela.Escrever(Convert.ToInt32("0378",16), Convert.ToByte(Valor)); } var x = jtmd.Position.X; var y = jtmd.Position.Y; var z = jtmd.Position.Z; if(string.IsNullOrEmpty(Par.Text))Par.Text= "0,2"; var Par_ =Convert.ToDouble(this.Par.Text); if ((x - UltimoX) > Par_) { this.XMais.Visibility =Visibility.Visible; UltimoX = x; Valor += 1; } else { if ((x - UltimoX) < -Par_) { this.XMenos.Visibility =Visibility.Visible; UltimoX = x; Valor += 2; } else { this.XMais.Visibility =Visibility.Hidden; this.XMenos.Visibility =Visibility.Hidden;