Guest User

Untitled

a guest
Feb 17th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Runtime.InteropServices;
  4. using System.Linq;
  5. using Microsoft.Kinect;
  6. using RGiesecke.DllExport;
  7.  
  8.  
  9. namespace KinectExtension
  10. {
  11.  
  12.  
  13. public class KinectExtension
  14. {
  15. public static ThreadStart tsKinect;
  16. public static Thread threadKinect;
  17. public static KinectSensor sensor;
  18. public static Skeleton[] skeleton;
  19. public static bool tracking = true;
  20. public static float HandRight_x;
  21. public static float HandRight_y;
  22. public static float HandRight_z;
  23.  
  24. [DllExport("StartKinect", CallingConvention = CallingConvention.Cdecl)]
  25. public static void StartKinect()
  26. {
  27. tsKinect = new ThreadStart(StartSensor);
  28. threadKinect = new Thread(tsKinect);
  29. threadKinect.Start();
  30. }
  31.  
  32. [DllExport("StopKinect", CallingConvention = CallingConvention.Cdecl)]
  33. public static void StopKinect()
  34. {
  35. tracking = false;
  36. threadKinect.Abort();
  37. GC.Collect();
  38. }
  39.  
  40. [DllExport("GetHandRightX", CallingConvention = CallingConvention.Cdecl)]
  41. public static float GetHandRightX()
  42. {
  43. return HandRight_x;
  44. }
  45.  
  46. [DllExport("GetHandRightY", CallingConvention = CallingConvention.Cdecl)]
  47. public static float GetHandRightY()
  48. {
  49. return HandRight_y;
  50. }
  51.  
  52. [DllExport("GetHandRightZ", CallingConvention = CallingConvention.Cdecl)]
  53. public static float GetHandRightZ()
  54. {
  55. return HandRight_z;
  56. }
  57.  
  58. public static void StartSensor()
  59. {
  60. sensor = KinectSensor.KinectSensors.FirstOrDefault(s => s.Status == KinectStatus.Connected);
  61. sensor.SkeletonStream.Enable();
  62. sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
  63. sensor.DepthStream.Enable(DepthImageFormat.Resolution320x240Fps30);
  64. skeleton = new Skeleton[sensor.SkeletonStream.FrameSkeletonArrayLength];
  65. sensor.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(skeletonFrameReady);
  66. sensor.Start();
  67.  
  68. while(tracking == true)
  69. {
  70. if (sensor.IsRunning == true)
  71. {
  72. foreach (Skeleton s in skeleton)
  73. {
  74. if (s != null)
  75. {
  76. foreach (Joint j in s.Joints)
  77. {
  78. if (j != null && j.TrackingState == JointTrackingState.Tracked )
  79. {
  80. if (j.JointType==JointType.HandRight)
  81. {
  82. HandRight_x = j.Position.X;
  83. HandRight_y = j.Position.Y;
  84. HandRight_z = j.Position.Z;
  85. }
  86. }
  87. }
  88. }
  89. }
  90.  
  91. }
  92. }
  93.  
  94.  
  95.  
  96. }
  97.  
  98. public static void skeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
  99. {
  100. using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) // Open the Skeleton frame
  101. {
  102. if (skeletonFrame != null && skeleton != null) // check that a frame is available
  103. {
  104. skeletonFrame.CopySkeletonDataTo(skeleton); // get the skeletal information in this frame
  105. }
  106. }
  107. return;
  108. }
  109.  
  110.  
  111. }
  112. }
Add Comment
Please, Sign In to add comment