Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using System.Runtime.InteropServices;
- using System.Linq;
- using Microsoft.Kinect;
- using RGiesecke.DllExport;
- namespace KinectExtension
- {
- public class KinectExtension
- {
- public static ThreadStart tsKinect;
- public static Thread threadKinect;
- public static KinectSensor sensor;
- public static Skeleton[] skeleton;
- public static bool tracking = true;
- public static float HandRight_x;
- public static float HandRight_y;
- public static float HandRight_z;
- [DllExport("StartKinect", CallingConvention = CallingConvention.Cdecl)]
- public static void StartKinect()
- {
- tsKinect = new ThreadStart(StartSensor);
- threadKinect = new Thread(tsKinect);
- threadKinect.Start();
- }
- [DllExport("StopKinect", CallingConvention = CallingConvention.Cdecl)]
- public static void StopKinect()
- {
- tracking = false;
- threadKinect.Abort();
- GC.Collect();
- }
- [DllExport("GetHandRightX", CallingConvention = CallingConvention.Cdecl)]
- public static float GetHandRightX()
- {
- return HandRight_x;
- }
- [DllExport("GetHandRightY", CallingConvention = CallingConvention.Cdecl)]
- public static float GetHandRightY()
- {
- return HandRight_y;
- }
- [DllExport("GetHandRightZ", CallingConvention = CallingConvention.Cdecl)]
- public static float GetHandRightZ()
- {
- return HandRight_z;
- }
- public static void StartSensor()
- {
- sensor = KinectSensor.KinectSensors.FirstOrDefault(s => s.Status == KinectStatus.Connected);
- sensor.SkeletonStream.Enable();
- sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
- sensor.DepthStream.Enable(DepthImageFormat.Resolution320x240Fps30);
- skeleton = new Skeleton[sensor.SkeletonStream.FrameSkeletonArrayLength];
- sensor.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(skeletonFrameReady);
- sensor.Start();
- while(tracking == true)
- {
- if (sensor.IsRunning == true)
- {
- foreach (Skeleton s in skeleton)
- {
- if (s != null)
- {
- foreach (Joint j in s.Joints)
- {
- if (j != null && j.TrackingState == JointTrackingState.Tracked )
- {
- if (j.JointType==JointType.HandRight)
- {
- HandRight_x = j.Position.X;
- HandRight_y = j.Position.Y;
- HandRight_z = j.Position.Z;
- }
- }
- }
- }
- }
- }
- }
- }
- public static void skeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
- {
- using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) // Open the Skeleton frame
- {
- if (skeletonFrame != null && skeleton != null) // check that a frame is available
- {
- skeletonFrame.CopySkeletonDataTo(skeleton); // get the skeletal information in this frame
- }
- }
- return;
- }
- }
- }
Add Comment
Please, Sign In to add comment