Kinect开发--用Kinect控制鼠标移动(第一阶段)

2016-02-14 10:31:47来源:作者:82Flex人点击

开发环境:vs2013&&kinect sdk

开发语言:C#(WPF)

简介:用Kinect控制鼠标的一个小小的样例,重写部分较少,根据SkeletonBasics样例工程文件通过代码重写而来,主要功能为以右手移动控制鼠标移动,以左手上举次数判断鼠标位置是否锁定.(尚未加入鼠标坐标获取功能)

存在的问题:GetCursorPosition函数不能正常获取鼠标位置,有哪位大神知道怎么获取嘞0v0

代码如下 //------------------------------------------------------------------------------// <copyright file="MainWindow.xaml.cs" company="Microsoft">// Copyright (c) Microsoft Corporation. All rights reserved.// </copyright>//------------------------------------------------------------------------------namespace Microsoft.Samples.Kinect.SkeletonBasics{using System.IO;using System.Windows;using System.Windows.Media;using Microsoft.Kinect;using Microsoft.Kinect.Interop;using System.Runtime.InteropServices;using System.Threading;using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Linq;using System.Text; using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{private readonly int MOUSEEVENTF_LEFTDOWN = 0x2;private readonly int MOUSEEVENTF_LEFTUP = 0x4;private Point LeftPosition;private Point RightPosition;private Point LeftInitPosition;private Point RightInitPosition;private bool Flag = false;private bool LeftFlag = false;private int LeftAdd = 1;private bool FirstTime = true;private int Time = 5;private double InitPositionZ = 1.5;private int LeftHandUp = 400;private int LeftHandDown = 400;private Point NowCursorPosition;private Point CursorPosition = new Point(960, 540);[DllImport("user32.dll", EntryPoint = "mouse_event")]private static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);[DllImport("user32.dll", EntryPoint = "SetCursorPos")]private static extern int SetCursorPos(int x, int y);[DllImport("user32.dll", EntryPoint = "GetCursorPos")]///存在问题的函数private static extern bool GetCursorPos(out Point pt);/// <summary>/// Width of output drawing/// </summary>private const float RenderWidth = 640.0f;/// <summary>/// Height of our output drawing/// </summary>private const float RenderHeight = 480.0f;/// <summary>/// Thickness of drawn joint lines/// </summary>private const double JointThickness = 3;/// <summary>/// Thickness of body center ellipse/// </summary>private const double BodyCenterThickness = 10;/// <summary>/// Thickness of clip edge rectangles/// </summary>private const double ClipBoundsThickness = 10;/// <summary>/// Brush used to draw skeleton center point/// </summary>private readonly Brush centerPointBrush = Brushes.Blue;/// <summary>/// Brush used for drawing joints that are currently tracked/// </summary>private readonly Brush trackedJointBrush = new SolidColorBrush(Color.FromArgb(255, 68, 192, 68));/// <summary>/// Brush used for drawing joints that are currently inferred/// </summary>private readonly Brush inferredJointBrush = Brushes.Yellow;/// <summary>/// Pen used for drawing bones that are currently tracked/// </summary>private readonly Pen trackedBonePen = new Pen(Brushes.Green, 6);/// <summary>/// Pen used for drawing bones that are currently inferred/// </summary>private readonly Pen inferredBonePen = new Pen(Brushes.Gray, 1);/// <summary>/// Active Kinect sensor/// </summary>private KinectSensor sensor;/// <summary>/// Drawing group for skeleton rendering output/// </summary>private DrawingGroup drawingGroup;/// <summary>/// Drawing image that we will display/// </summary>private DrawingImage imageSource;/// <summary>/// Initializes a new instance of the MainWindow class./// </summary>public MainWindow(){InitializeComponent();}/// <summary>/// Draws indicators to show which edges are clipping skeleton data/// </summary>/// <param name="skeleton">skeleton to draw clipping information for</param>/// <param name="drawingContext">drawing context to draw to</param>private static void RenderClippedEdges(Skeleton skeleton, DrawingContext drawingContext){if (skeleton.ClippedEdges.HasFlag(FrameEdges.Bottom)){drawingContext.DrawRectangle(Brushes.Red,null,new Rect(0, RenderHeight - ClipBoundsThickness, RenderWidth, ClipBoundsThickness));}if (skeleton.ClippedEdges.HasFlag(FrameEdges.Top)){drawingContext.DrawRectangle(Brushes.Red,null,new Rect(0, 0, RenderWidth, ClipBoundsThickness));}if (skeleton.ClippedEdges.HasFlag(FrameEdges.Left)){drawingContext.DrawRectangle(Brushes.Red,null,new Rect(0, 0, ClipBoundsThickness, RenderHeight));}if (skeleton.ClippedEdges.HasFlag(FrameEdges.Right)){drawingContext.DrawRectangle(Brushes.Red,null,new Rect(RenderWidth - ClipBoundsThickness, 0, ClipBoundsThickness, RenderHeight));}}/// <summary>/// Execute startup tasks/// </summary>/// <param name="sender">object sending the event</param>/// <param name="e">event arguments</param>private void WindowLoaded(object sender, RoutedEventArgs e){// Create the drawing group we'll use for drawingthis.drawingGroup = new DrawingGroup();// Create an image source that we can use in our image controlthis.imageSource = new DrawingImage(this.drawingGroup);// Display the drawing using our image controlImage.Source = this.imageSource;// Look through all sensors and start the first connected one.// This requires that a Kinect is connected at the time of app startup.// To make your app robust against plug/unplug, // it is recommended to use KinectSensorChooser provided in Microsoft.Kinect.Toolkit (See components in Toolkit Browser).foreach (var potentialSensor in KinectSensor.KinectSensors){if (potentialSensor.Status == KinectStatus.Connected){this.sensor = potentialSensor;break;}}if (null != this.sensor){// Turn on the skeleton stream to receive skeleton framesthis.sensor.SkeletonStream.Enable();// Add an event handler to be called whenever there is new color frame datathis.sensor.SkeletonFrameReady += this.SensorSkeletonFrameReady;// Start the sensor!try{this.sensor.Start();}catch (IOException){this.sensor = null;}}/*if (null == this.sensor){this.statusBarText.Text = Properties.Resources.NoKinectReady;} */sensor.ElevationAngle = 0;}/// <summary>/// Execute shutdown tasks/// </summary>/// <param name="sender">object sending the event</param>/// <param name="e">event arguments</param>private void WindowClosing(object sender, System.ComponentModel.CancelEventArgs e){if (null != this.sensor){this.sensor.Stop();}}/// <summary>/// Event handler for Kinect sensor's SkeletonFrameReady event/// </summary>/// <param name="sender">object sending the event</param>/// <param name="e">event arguments</param>private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e){Skeleton[] skeletons = new Skeleton[0];using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()){if (skeletonFrame != null){skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];skeletonFrame.CopySkeletonDataTo(skeletons);}}using (DrawingContext dc = this.drawingGroup.Open()){// Draw a transparent background to set the render sizedc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, RenderWidth, RenderHeight));if (skeletons.Length != 0){foreach (Skeleton skel in skeletons){RenderClippedEdges(skel, dc);if (skel.TrackingState == SkeletonTrackingState.Tracked){this.DrawBonesAndJoints(skel, dc);}else if (skel.TrackingState == SkeletonTrackingState.PositionOnly){dc.DrawEllipse(this.centerPointBrush,null,this.SkeletonPointToScreen(skel.Position),BodyCenterThickness,BodyCenterThickness);}}}// prevent drawing outside of our render areathis.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, RenderWidth, RenderHeight));}}/// <summary>/// Draws a skeleton's bones and joints/// </summary>/// <param name="skeleton">skeleton to draw</param>/// <param name="drawingContext">drawing context to draw to</param>/// 以下为主要重写部分private void DrawBonesAndJoints(Skeleton skeleton, DrawingContext drawingContext){/*// Render Torsothis.DrawBone(skeleton, drawingContext, JointType.Head, JointType.ShoulderCenter);this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.ShoulderLeft);this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.ShoulderRight);this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.Spine);this.DrawBone(skeleton, drawingContext, JointType.Spine, JointType.HipCenter);this.DrawBone(skeleton, drawingContext, JointType.HipCenter, JointType.HipLeft);this.DrawBone(skeleton, drawingContext, JointType.HipCenter, JointType.HipRight);*/// Left Armthis.DrawBone(skeleton, drawingContext, JointType.ShoulderLeft, JointType.ElbowLeft);this.DrawBone(skeleton, drawingContext, JointType.ElbowLeft, JointType.WristLeft);this.DrawBone(skeleton, drawingContext, JointType.WristLeft, JointType.HandLeft);// Right Armthis.DrawBone(skeleton, drawingContext, JointType.ShoulderRight, JointType.ElbowRight);this.DrawBone(skeleton, drawingContext, JointType.ElbowRight, JointType.WristRight);this.DrawBone(skeleton, drawingContext, JointType.WristRight, JointType.HandRight);/*// Left Legthis.DrawBone(skeleton, drawingContext, JointType.HipLeft, JointType.KneeLeft);this.DrawBone(skeleton, drawingContext, JointType.KneeLeft, JointType.AnkleLeft);this.DrawBone(skeleton, drawingContext, JointType.AnkleLeft, JointType.FootLeft);// Right Legthis.DrawBone(skeleton, drawingContext, JointType.HipRight, JointType.KneeRight);this.DrawBone(skeleton, drawingContext, JointType.KneeRight, JointType.AnkleRight);this.DrawBone(skeleton, drawingContext, JointType.AnkleRight, JointType.FootRight);*/// Render Jointsforeach (Joint joint in skeleton.Joints){Brush drawBrush = null;if (joint.JointType.Equals(JointType.HandRight)||joint.JointType.Equals(JointType.HandLeft)||joint.JointType.Equals(JointType.WristRight)||joint.JointType.Equals(JointType.WristLeft)||joint.JointType.Equals(JointType.ElbowRight)||joint.JointType.Equals(JointType.ElbowLeft)||joint.JointType.Equals(JointType.ShoulderRight)||joint.JointType.Equals(JointType.ShoulderLeft)){if (joint.TrackingState == JointTrackingState.Tracked){drawBrush = this.trackedJointBrush;}else if (joint.TrackingState == JointTrackingState.Inferred){drawBrush = this.inferredJointBrush;}if (drawBrush != null){drawingContext.DrawEllipse(drawBrush, null, this.SkeletonPointToScreen(joint.Position), JointThickness, JointThickness);}if (joint.JointType.Equals(JointType.HandLeft)){LeftPosition = SkeletonPointToScreen(joint.Position);if (joint.TrackingState.Equals(JointTrackingState.Inferred) ||joint.TrackingState.Equals(JointTrackingState.NotTracked))Flag = false;if (joint.TrackingState.Equals(JointTrackingState.Tracked))Flag = true;if ((int)LeftPosition.Y < LeftHandUp){LeftHandUp = (int)LeftPosition.Y;}if ((int)LeftPosition.Y > LeftHandDown){LeftHandDown = (int)LeftPosition.Y;}if ((int)LeftPosition.Y > (int)(RenderHeight / 2)){LeftHandUp = LeftHandDown;}/*if ((int)LeftPosition.Y < (int)(RenderHeight / 2)){LeftHandUp = (int)LeftPosition.Y;}*/TBLeftX.Text = LeftPosition.X.ToString();TBLeftY.Text = LeftPosition.Y.ToString();TBLeftZ.Text = joint.Position.Z.ToString();}if (joint.JointType.Equals(JointType.HandRight)){RightPosition = SkeletonPointToScreen(joint.Position);TBRightX.Text = RightPosition.X.ToString();TBRightY.Text = RightPosition.Y.ToString();TBRightZ.Text = joint.Position.Z.ToString();TBLeftUp.Text = LeftHandUp.ToString();TBLeftDown.Text = LeftHandDown.ToString();if (LeftHandDown - LeftHandUp > 300){LeftFlag = true;}if ((LeftHandDown - LeftHandUp) < 300 && LeftFlag){LeftFlag = false;LeftAdd++;}if ((LeftAdd % 2 == 0)){this.TB2.Text = "Active";this.CursorPosition.X += (int)(0.1 * ((int)RightPosition.X - (int)RightInitPosition.X));this.CursorPosition.Y += (int)(0.1 * ((int)RightPosition.Y - (int)RightInitPosition.Y));if (joint.Position.Z < InitPositionZ){this.TB1.Text = "Near";//mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, (int)this.CursorPosition.X, (int)this.CursorPosition.Y, 0, 0);}else{this.TB1.Text = "Far";}SetCursorPos((int)CursorPosition.X, (int)CursorPosition.Y);}else {this.TB2.Text = "Lock";//GetCursorPos(out NowCursorPosition);RightInitPosition.X = 320;//NowCursorPosition.X;RightInitPosition.Y = 240;//NowCursorPosition.Y;TBCursorX.Text = (NowCursorPosition.X).ToString();TBCursorY.Text = (NowCursorPosition.Y).ToString();}}}}}/// <summary>/// Maps a SkeletonPoint to lie within our render space and converts to Point/// </summary>/// <param name="skelpoint">point to map</param>/// <returns>mapped point</returns>private Point SkeletonPointToScreen(SkeletonPoint skelpoint){// Convert point to depth space. // We are not using depth directly, but we do want the points in our 640x480 output resolution.DepthImagePoint depthPoint = this.sensor.CoordinateMapper.MapSkeletonPointToDepthPoint(skelpoint, DepthImageFormat.Resolution640x480Fps30);return new Point(depthPoint.X, depthPoint.Y);}/// <summary>/// Draws a bone line between two joints/// </summary>/// <param name="skeleton">skeleton to draw bones from</param>/// <param name="drawingContext">drawing context to draw to</param>/// <param name="jointType0">joint to start drawing from</param>/// <param name="jointType1">joint to end drawing at</param>private void DrawBone(Skeleton skeleton, DrawingContext drawingContext, JointType jointType0, JointType jointType1){Joint joint0 = skeleton.Joints[jointType0];Joint joint1 = skeleton.Joints[jointType1];// If we can't find either of these joints, exitif (joint0.TrackingState == JointTrackingState.NotTracked ||joint1.TrackingState == JointTrackingState.NotTracked){return;}// Don't draw if both points are inferredif (joint0.TrackingState == JointTrackingState.Inferred &&joint1.TrackingState == JointTrackingState.Inferred){return;}// We assume all drawn bones are inferred unless BOTH joints are trackedPen drawPen = this.inferredBonePen;if (joint0.TrackingState == JointTrackingState.Tracked && joint1.TrackingState == JointTrackingState.Tracked){drawPen = this.trackedBonePen;}drawingContext.DrawLine(drawPen, this.SkeletonPointToScreen(joint0.Position), this.SkeletonPointToScreen(joint1.Position));}/// <summary>/// Handles the checking or unchecking of the seated mode combo box/// </summary>/// <param name="sender">object sending the event</param>/// <param name="e">event arguments</param>private void CheckBoxSeatedModeChanged(object sender, RoutedEventArgs e){if (null != this.sensor){if (this.checkBoxSeatedMode.IsChecked.GetValueOrDefault()){this.sensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated;}else{this.sensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Default;}}}}}XAML文件----<Window x:Class="Microsoft.Samples.Kinect.SkeletonBasics.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="Skeleton Basics" Height="735" Width="770" Loaded="WindowLoaded" Closing="WindowClosing"><Window.Resources><SolidColorBrush x:Key="MediumGreyBrush" Color="#ff6e6e6e"/><SolidColorBrush x:Key="KinectPurpleBrush" Color="#ff52318f"/><SolidColorBrush x:Key="KinectBlueBrush" Color="#ff00BCF2"/><Style TargetType="{x:Type Image}"><Setter Property="SnapsToDevicePixels" Value="True"/></Style><Style TargetType="{x:Type CheckBox}" x:Key="SquareCheckBox" ><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type CheckBox}"><Grid><StackPanel Orientation="Horizontal" Background="Transparent"><Grid x:Name="SquareCheckBoxChecked"><Image x:Name="CheckedNormal" Source="Images/CheckedNormal.png" Stretch="None" HorizontalAlignment="Center"/><Image x:Name="CheckedHover" Source="Images/CheckedHover.png" Stretch="None" HorizontalAlignment="Center" Visibility="Collapsed"/></Grid><Grid x:Name="SquareCheckBoxUnchecked" Visibility="Collapsed"><Image x:Name="UncheckedNormal" Source="Images/UncheckedNormal.png" Stretch="None" HorizontalAlignment="Center"/><Image x:Name="UncheckedHover" Source="Images/UncheckedHover.png" Stretch="None" HorizontalAlignment="Center" Visibility="Collapsed"/></Grid><TextBlock x:Name="SquareCheckBoxText" Text="{TemplateBinding Content}" TextAlignment="Left" VerticalAlignment="Center" Foreground="{StaticResource KinectPurpleBrush}" FontSize="15" Margin="9,0,0,0"/></StackPanel></Grid><ControlTemplate.Triggers><Trigger Property="IsChecked" Value="false"><Setter Property="Visibility" Value="Collapsed" TargetName="SquareCheckBoxChecked"/><Setter Property="Visibility" Value="Visible" TargetName="SquareCheckBoxUnchecked"/></Trigger><Trigger Property="IsMouseOver" Value="true"><Setter Property="Visibility" Value="Collapsed" TargetName="CheckedNormal"/><Setter Property="Visibility" Value="Collapsed" TargetName="UncheckedNormal"/><Setter Property="Visibility" Value="Visible" TargetName="CheckedHover"/><Setter Property="Visibility" Value="Visible" TargetName="UncheckedHover"/><Setter Property="Foreground" Value="{StaticResource KinectBlueBrush}" TargetName="SquareCheckBoxText"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></Window.Resources><Grid Name="layoutGrid" Margin="10 0 10 0"><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="*"/><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/></Grid.RowDefinitions><DockPanel Grid.Row="0" Margin="0 0 0 20"><Image DockPanel.Dock="Left" Source="Images/Logo.png" Stretch="Fill" Height="32" Width="81" Margin="0 10 0 5"/><TextBlock DockPanel.Dock="Right" Margin="0 0 -1 0" VerticalAlignment="Bottom" Foreground="{StaticResource MediumGreyBrush}" FontFamily="Segoe UI" FontSize="18">Skeleton Basics</TextBlock><Image Grid.Column="1" Source="Images/Status.png" Stretch="None" HorizontalAlignment="Center" Margin="0 0 0 5"/><TextBlock x:Name="TB1" Height="47" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="211" FontSize="36"/><TextBlock x:Name="TB2" Height="47" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" FontSize="36"/></DockPanel><Viewbox Grid.Row="1" Stretch="Uniform" HorizontalAlignment="Center"><Image Name="Image" Width="640" Height="480"/></Viewbox><CheckBox Grid.Row="2" Style="{StaticResource SquareCheckBox}" Content="Seated Mode" Height="Auto" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0 10 10 10" Name="checkBoxSeatedMode" Checked="CheckBoxSeatedModeChanged" Unchecked="CheckBoxSeatedModeChanged"/><StatusBar Grid.Row="3" HorizontalAlignment="Stretch" Name="statusBar" VerticalAlignment="Bottom" Background="White" Foreground="{StaticResource MediumGreyBrush}"><StatusBarItem Padding="0 0 0 10"/></StatusBar><TextBlock x:Name="TBLeftX" HorizontalAlignment="Left" Margin="177,5,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="17" Width="85"/><TextBlock x:Name="TBLeftY" HorizontalAlignment="Left" Margin="267,5,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="17" Width="85"/><TextBlock x:Name="TBRightX" HorizontalAlignment="Left" Margin="476,5,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="17" Width="85"/><TextBlock x:Name="TBRightY" HorizontalAlignment="Left" Margin="566,5,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="17" Width="85"/><TextBlock x:Name="TBLeftZ" HorizontalAlignment="Left" Margin="341,5,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="17" Width="85"/><TextBlock x:Name="TBRightZ" HorizontalAlignment="Left" Margin="647,5,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="17" Width="85"/><TextBlock x:Name="TBLeftUp" HorizontalAlignment="Left" Margin="177,24,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/><TextBlock x:Name="TBLeftDown" HorizontalAlignment="Left" Margin="267,24,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/><TextBlock x:Name="TBCursorX" HorizontalAlignment="Left" Margin="415,24,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/><TextBlock x:Name="TBCursorY" HorizontalAlignment="Left" Margin="566,24,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/></Grid></Window>

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台