diff --git a/kinectTest.sln b/kinectTest.sln new file mode 100644 index 0000000..0b0f646 --- /dev/null +++ b/kinectTest.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "kinectTest", "kinectTest\kinectTest.csproj", "{07DA2BB9-75D4-43AA-8491-896A6F98C84F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {07DA2BB9-75D4-43AA-8491-896A6F98C84F}.Debug|x86.ActiveCfg = Debug|x86 + {07DA2BB9-75D4-43AA-8491-896A6F98C84F}.Debug|x86.Build.0 = Debug|x86 + {07DA2BB9-75D4-43AA-8491-896A6F98C84F}.Release|x86.ActiveCfg = Release|x86 + {07DA2BB9-75D4-43AA-8491-896A6F98C84F}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/kinectTest/App.xaml b/kinectTest/App.xaml new file mode 100644 index 0000000..035385b --- /dev/null +++ b/kinectTest/App.xaml @@ -0,0 +1,8 @@ + + + + + diff --git a/kinectTest/App.xaml.cs b/kinectTest/App.xaml.cs new file mode 100644 index 0000000..f483f2b --- /dev/null +++ b/kinectTest/App.xaml.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Windows; + +namespace kinectTest +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/kinectTest/MainWindow.xaml b/kinectTest/MainWindow.xaml new file mode 100644 index 0000000..69cf843 --- /dev/null +++ b/kinectTest/MainWindow.xaml @@ -0,0 +1,13 @@ + + + + + + + + + + diff --git a/kinectTest/MainWindow.xaml.cs b/kinectTest/MainWindow.xaml.cs new file mode 100644 index 0000000..039dfed --- /dev/null +++ b/kinectTest/MainWindow.xaml.cs @@ -0,0 +1,176 @@ +using System; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Shapes; +using Microsoft.Kinect; +using System.Linq; + +namespace kinectTest +{ + public partial class MainWindow : Window + { + //Instantiate the Kinect runtime. Required to initialize the device. + //IMPORTANT NOTE: You can pass the device ID here, in case more than one Kinect device is connected. + KinectSensor sensor = KinectSensor.KinectSensors[0]; + byte[] pixelData; + Skeleton[] skeletons; + + public MainWindow() + { + InitializeComponent(); + + //Runtime initialization is handled when the window is opened. When the window + //is closed, the runtime MUST be unitialized. + this.Loaded += new RoutedEventHandler(MainWindow_Loaded); + this.Unloaded += new RoutedEventHandler(MainWindow_Unloaded); + + sensor.ColorStream.Enable(); + sensor.SkeletonStream.Enable(); + } + + void runtime_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e) + { + bool receivedData = false; + + using (SkeletonFrame SFrame = e.OpenSkeletonFrame()) + { + if (SFrame == null) + { + // The image processing took too long. More than 2 frames behind. + } + else + { + skeletons = new Skeleton[SFrame.SkeletonArrayLength]; + SFrame.CopySkeletonDataTo(skeletons); + receivedData = true; + } + } + + if (receivedData) + { + + Skeleton currentSkeleton = (from s in skeletons + where s.TrackingState == SkeletonTrackingState.Tracked + select s).FirstOrDefault(); + + if (currentSkeleton != null) + { + SetEllipsePosition(head, currentSkeleton.Joints[JointType.Head]); + SetEllipsePosition(leftHand, currentSkeleton.Joints[JointType.HandLeft]); + SetEllipsePosition(rightHand, currentSkeleton.Joints[JointType.HandRight]); + } + } + } + + public void ClampPositionToCircle(SkeletonPoint center, float radius, ref SkeletonPoint position) + { + // Calculate the offset vector from the center of the circle to our position + SkeletonPoint offset = new SkeletonPoint(); + offset.X = position.X - center.X; + offset.Y = position.Y - center.Y; + offset.Z = position.Z; + // Calculate the linear distance of this offset vector + double distance = Math.Abs(Math.Sqrt((offset.X * offset.X) + (offset.Y * offset.Y))); + if (radius < distance) + { + // If the distance is more than our radius we need to clamp + // Calculate the direction to our position + SkeletonPoint direction = new SkeletonPoint(); + direction.X = (float)(offset.X / distance); + direction.Y = (float)(offset.Y / distance); + direction.Z = offset.Z; + //Vector2 direction = offset / distance; + // Calculate our new position using the direction to our old position and our radius + position.X += center.X * radius; + position.Y += center.Y * radius; + + //position = center + direction * radius; + } + } + + + //This method is used to position the ellipses on the canvas + //according to correct movements of the tracked joints. + + //IMPORTANT NOTE: Code for vector scaling was imported from the Coding4Fun Kinect Toolkit + //available here: http://c4fkinect.codeplex.com/ + //I only used this part to avoid adding an extra reference. + private void SetEllipsePosition(Ellipse ellipse, Joint joint) + { + Microsoft.Kinect.SkeletonPoint vector = new Microsoft.Kinect.SkeletonPoint(); + vector.X = ScaleVector(640, joint.Position.X); + vector.Y = ScaleVector(480, -joint.Position.Y); + vector.Z = joint.Position.Z; + + Joint updatedJoint = new Joint(); + updatedJoint = joint; + updatedJoint.TrackingState = JointTrackingState.Tracked; + updatedJoint.Position = vector; + + SkeletonPoint center = new SkeletonPoint(); + center.X = 0; + center.Y = 0; + SkeletonPoint pos = updatedJoint.Position; + ClampPositionToCircle(center, 10.0f, ref pos); + updatedJoint.Position = pos; + Canvas.SetLeft(ellipse, updatedJoint.Position.X); + Canvas.SetTop(ellipse, updatedJoint.Position.Y); + } + + + private float ScaleVector(int length, float position) + { + float value = (((((float)length) / 1f) / 2f) * position) + (length / 2); + if (value > length) + { + return (float)length; + } + if (value < 0f) + { + return 0f; + } + return value; + } + + void MainWindow_Unloaded(object sender, RoutedEventArgs e) + { + sensor.Stop(); + } + + void MainWindow_Loaded(object sender, RoutedEventArgs e) + { + sensor.SkeletonFrameReady += runtime_SkeletonFrameReady; + sensor.ColorFrameReady += runtime_VideoFrameReady; + sensor.Start(); + } + + void runtime_VideoFrameReady(object sender, ColorImageFrameReadyEventArgs e) + { + bool receivedData = false; + + using (ColorImageFrame CFrame = e.OpenColorImageFrame()) + { + if (CFrame == null) + { + // The image processing took too long. More than 2 frames behind. + } + else + { + pixelData = new byte[CFrame.PixelDataLength]; + CFrame.CopyPixelDataTo(pixelData); + receivedData = true; + } + } + + if (receivedData) + { + BitmapSource source = BitmapSource.Create(640, 480, 96, 96, + PixelFormats.Bgr32, null, pixelData, 640 * 4); + + videoImage.Source = source; + } + } + } +} diff --git a/kinectTest/Properties/AssemblyInfo.cs b/kinectTest/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..80d8661 --- /dev/null +++ b/kinectTest/Properties/AssemblyInfo.cs @@ -0,0 +1,55 @@ +using System.Reflection; +using System.Resources; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Windows; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("kinectTest")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("kinectTest")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2011")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +//In order to begin building localizable applications, set +//CultureYouAreCodingWith in your .csproj file +//inside a . For example, if you are using US english +//in your source files, set the to en-US. Then uncomment +//the NeutralResourceLanguage attribute below. Update the "en-US" in +//the line below to match the UICulture setting in the project file. + +//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)] + + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] + + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/kinectTest/Properties/Resources.Designer.cs b/kinectTest/Properties/Resources.Designer.cs new file mode 100644 index 0000000..2e2704a --- /dev/null +++ b/kinectTest/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.235 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace kinectTest.Properties +{ + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("kinectTest.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/kinectTest/Properties/Resources.resx b/kinectTest/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/kinectTest/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/kinectTest/Properties/Settings.Designer.cs b/kinectTest/Properties/Settings.Designer.cs new file mode 100644 index 0000000..8cfe0b5 --- /dev/null +++ b/kinectTest/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.235 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace kinectTest.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/kinectTest/Properties/Settings.settings b/kinectTest/Properties/Settings.settings new file mode 100644 index 0000000..033d7a5 --- /dev/null +++ b/kinectTest/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/kinectTest/kinectTest.csproj b/kinectTest/kinectTest.csproj new file mode 100644 index 0000000..4bbf40d --- /dev/null +++ b/kinectTest/kinectTest.csproj @@ -0,0 +1,107 @@ + + + + Debug + x86 + 8.0.30703 + 2.0 + {07DA2BB9-75D4-43AA-8491-896A6F98C84F} + WinExe + Properties + kinectTest + kinectTest + v4.0 + Client + 512 + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 4 + + + x86 + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + x86 + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + False + ..\..\..\..\..\Program Files\Microsoft SDKs\Kinect\v1.0\Assemblies\Microsoft.Kinect.dll + + + + + + + + + + 4.0 + + + + + + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + App.xaml + Code + + + MainWindow.xaml + Code + + + + + Code + + + True + True + Resources.resx + + + True + Settings.settings + True + + + ResXFileCodeGenerator + Resources.Designer.cs + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + + \ No newline at end of file