/// <summary> /// Initializes a new instance of the MainWindow class. /// </summary> public MainWindow() { // create nuiApp instance and setting NuiApplication's name nuiApp = new NuiApplication("OpenNUI.Samples.BodyBasics"); // register delegate when nui sensor connected, disconnected nuiApp.OnSensorConnected += nuiApp_OnSensorConnected; nuiApp.OnSensorDisconnected += nuiApp_OnSensorDisconnected; try { nuiApp.Start(); } // when exception catched. it means OpenNUI dosen't installed catch { //show error messagebox MessageBox.Show("OpenNUI dosen't installed", "error", MessageBoxButton.OK, MessageBoxImage.Error); //program exit Environment.Exit(0); } // worker for openNUIFrameTimer act = new Action(delegate() { //do not work when usesensor is null if (useSensor == null) { return; } BodyData[] bodies = useSensor.GetBodyFrame(); if (bodies == null) { return; } using (DrawingContext dc = this.drawingGroup.Open()) { // Draw a transparent background to set the render size dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, this.displayWidth, this.displayHeight)); int penIndex = 0; foreach (BodyData body in bodies) { Pen drawPen = this.bodyColors[penIndex++]; if (body.Valid) //isTracked in kinect { IReadOnlyDictionary <JointType, Joint> joints = body.Joints; // convert the joint points to depth (display) space Dictionary <JointType, Point> jointPoints = new Dictionary <JointType, Point>(); foreach (JointType jointType in joints.Keys) { // sometimes the depth(Z) of an inferred joint may show as negative // clamp down to 0.1f to prevent coordinatemapper from returning (-Infinity, -Infinity) Vector3 position = joints[jointType].position; if (position.z < 0) { position.z = InferredZPositionClamp; } Vector2 depthSpacePoint = OpenNUI.CSharp.Library.NuiCoordinate.JointPostoDepthPos(position, body.Sensor.DepthInfo); jointPoints[jointType] = new Point(depthSpacePoint.x, depthSpacePoint.y); } this.DrawBody(joints, jointPoints, dc, drawPen); this.DrawHand(body.LeftHand, jointPoints[JointType.HandLeft], dc); this.DrawHand(body.RightHand, jointPoints[JointType.HandRight], dc); } } // prevent drawing outside of our render area this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, this.displayWidth, this.displayHeight)); } }); // create openNUIFrameTimer instance openNUIFrameTimer = new System.Timers.Timer(); //set the timer interval to 60fps openNUIFrameTimer.Interval = 1000 / 60; //set the timer elapsed callback openNUIFrameTimer.Elapsed += (object sender, System.Timers.ElapsedEventArgs e) => { try { //use invoke for access to the this wpf window's ui Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, act); } catch { } }; //set the timer autoreset to true for loop openNUIFrameTimer.AutoReset = true; //openNUIFrameTimer start openNUIFrameTimer.Start(); // a bone defined as a line between two joints this.bones = new List <Tuple <JointType, JointType> >(); // Torso this.bones.Add(new Tuple <JointType, JointType>(JointType.Head, JointType.Neck)); this.bones.Add(new Tuple <JointType, JointType>(JointType.Neck, JointType.SpineShoulder)); this.bones.Add(new Tuple <JointType, JointType>(JointType.SpineShoulder, JointType.SpineMid)); this.bones.Add(new Tuple <JointType, JointType>(JointType.SpineMid, JointType.SpineBase)); this.bones.Add(new Tuple <JointType, JointType>(JointType.SpineShoulder, JointType.ShoulderRight)); this.bones.Add(new Tuple <JointType, JointType>(JointType.SpineShoulder, JointType.ShoulderLeft)); this.bones.Add(new Tuple <JointType, JointType>(JointType.SpineBase, JointType.HipRight)); this.bones.Add(new Tuple <JointType, JointType>(JointType.SpineBase, JointType.HipLeft)); // Right Arm this.bones.Add(new Tuple <JointType, JointType>(JointType.ShoulderRight, JointType.ElbowRight)); this.bones.Add(new Tuple <JointType, JointType>(JointType.ElbowRight, JointType.WristRight)); this.bones.Add(new Tuple <JointType, JointType>(JointType.WristRight, JointType.HandRight)); this.bones.Add(new Tuple <JointType, JointType>(JointType.HandRight, JointType.HandTipRight)); this.bones.Add(new Tuple <JointType, JointType>(JointType.WristRight, JointType.ThumbRight)); // Left Arm this.bones.Add(new Tuple <JointType, JointType>(JointType.ShoulderLeft, JointType.ElbowLeft)); this.bones.Add(new Tuple <JointType, JointType>(JointType.ElbowLeft, JointType.WristLeft)); this.bones.Add(new Tuple <JointType, JointType>(JointType.WristLeft, JointType.HandLeft)); this.bones.Add(new Tuple <JointType, JointType>(JointType.HandLeft, JointType.HandTipLeft)); this.bones.Add(new Tuple <JointType, JointType>(JointType.WristLeft, JointType.ThumbLeft)); // Right Leg this.bones.Add(new Tuple <JointType, JointType>(JointType.HipRight, JointType.KneeRight)); this.bones.Add(new Tuple <JointType, JointType>(JointType.KneeRight, JointType.AnkleRight)); this.bones.Add(new Tuple <JointType, JointType>(JointType.AnkleRight, JointType.FootRight)); // Left Leg this.bones.Add(new Tuple <JointType, JointType>(JointType.HipLeft, JointType.KneeLeft)); this.bones.Add(new Tuple <JointType, JointType>(JointType.KneeLeft, JointType.AnkleLeft)); this.bones.Add(new Tuple <JointType, JointType>(JointType.AnkleLeft, JointType.FootLeft)); // populate body colors, one for each BodyIndex this.bodyColors = new List <Pen>(); this.bodyColors.Add(new Pen(Brushes.Red, 6)); this.bodyColors.Add(new Pen(Brushes.Orange, 6)); this.bodyColors.Add(new Pen(Brushes.Green, 6)); this.bodyColors.Add(new Pen(Brushes.Blue, 6)); this.bodyColors.Add(new Pen(Brushes.Indigo, 6)); this.bodyColors.Add(new Pen(Brushes.Violet, 6)); // Create the drawing group we'll use for drawing this.drawingGroup = new DrawingGroup(); // Create an image source that we can use in our image control this.imageSource = new DrawingImage(this.drawingGroup); // use the window object as the view model in this simple example this.DataContext = this; // initialize the components (controls) of the window this.InitializeComponent(); }
/// <summary> /// Initializes a new instance of the MainWindow class. /// </summary> public MainWindow() { // create nuiApp instance and setting NuiApplication's name nuiApp = new NuiApplication("OpenNUI.Samples.ColorBasics"); // register delegate when nui sensor connected, disconnected nuiApp.OnSensorConnected += nuiApp_OnSensorConnected; nuiApp.OnSensorDisconnected += nuiApp_OnSensorDisconnected; try { nuiApp.Start(); } // when exception catched. it means OpenNUI dosen't installed catch { //show error messagebox MessageBox.Show("OpenNUI dosen't installed", "error", MessageBoxButton.OK, MessageBoxImage.Error); //program exit Environment.Exit(0); } // worker for openNUIFrameTimer act = new Action(delegate() { //do not work when usesensor is null if (useSensor == null) { return; } //get the color frame ImageData colorFrame = useSensor.GetColorFrame(); //do not work when color frame is null (faild to GetColorFrame()) if (colorFrame == null) { return; } int numPixels = colorFrame.FrameData.Length / sizeof(uint); bitmap.Lock(); unsafe { fixed(byte *pSrcData = &colorFrame.FrameData[0]) { uint *pCurrent = (uint *)pSrcData; uint *pBitmapData = (uint *)bitmap.BackBuffer; for (int n = 0; n < numPixels; n++) { uint x = *(pCurrent++); *(pBitmapData + n) = (x & 0xFF000000) | (x & 0x00FF0000) | (x & 0x0000FF00) | (x & 0x000000FF); } } } bitmap.AddDirtyRect(new Int32Rect(0, 0, colorFrame.Description.Width, colorFrame.Description.Height)); bitmap.Unlock(); }); // create openNUIFrameTimer instance openNUIFrameTimer = new System.Timers.Timer(); //set the timer interval to 60fps openNUIFrameTimer.Interval = 1000 / 60; //set the timer elapsed callback openNUIFrameTimer.Elapsed += (object sender, System.Timers.ElapsedEventArgs e) => { try { //use invoke for access to the this wpf window's ui Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, act); } catch { } }; //set the timer autoreset to true for loop openNUIFrameTimer.AutoReset = true; //openNUIFrameTimer start openNUIFrameTimer.Start(); // initialize the components (controls) of the window InitializeComponent(); }
/// <summary> /// Initializes a new instance of the MainWindow class. /// </summary> public MainWindow() { // create nuiApp instance and setting NuiApplication's name nuiApp = new NuiApplication("OpenNUI.Samples.DepthBasics"); // register delegate when nui sensor connected, disconnected nuiApp.OnSensorConnected += nuiApp_OnSensorConnected; nuiApp.OnSensorDisconnected += nuiApp_OnSensorDisconnected; try { nuiApp.Start(); } // when exception catched. it means OpenNUI dosen't installed catch { //show error messagebox MessageBox.Show("OpenNUI dosen't installed", "error", MessageBoxButton.OK, MessageBoxImage.Error); //program exit Environment.Exit(0); } // worker for openNUIFrameTimer act = new Action(delegate() { //do not work when usesensor is null if (useSensor == null) { return; } //get the depth frame DepthData depthFrame = useSensor.GetDepthFrame(); //do not work when depth frame is null (faild to GetDepthFrame()) if (depthFrame == null) { return; } for (int i = 0; i < depthFrame.FrameData.Length; ++i) { // Get the depth for this pixel ushort depth = depthFrame.FrameData[i]; // To convert to a byte, we're mapping the depth value to the byte range. // Values outside the reliable depth range are mapped to 0 (black). ushort minDepth = 0; ushort maxDepth = ushort.MaxValue; this.depthPixels[i] = (byte)(depth >= minDepth && depth <= maxDepth ? (depth / MapDepthToByte) : 0); } bitmap.WritePixels( new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), this.depthPixels, bitmap.PixelWidth, 0); depthImage.Source = bitmap; }); // create openNUIFrameTimer instance openNUIFrameTimer = new System.Timers.Timer(); //set the timer interval to 60fps openNUIFrameTimer.Interval = 1000 / 60; //set the timer elapsed callback openNUIFrameTimer.Elapsed += (object sender, System.Timers.ElapsedEventArgs e) => { try { //use invoke for access to the this wpf window's ui Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, act); } catch { } }; //set the timer autoreset to true for loop openNUIFrameTimer.AutoReset = true; //openNUIFrameTimer start openNUIFrameTimer.Start(); // initialize the components (controls) of the window InitializeComponent(); }