如何使用kinect将光标移动到屏幕角落?

时间:2017-07-18 17:07:50

标签: c# wpf kinect

我正在使用kinect 360和windows sdk 1.8。我有一个用c#wpf开发的应用程序,可以用手势移动鼠标光标,也可以点击。 问题是每当我想将光标移动到角落时我需要在kinect前面迈出一步来做到这一点。 但我想以这种方式缩放光标,所以我只需要移动我的手,光标移动到所有屏幕。

这是我正在尝试的代码

 void sensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
    {
        bool receivedData = false;

        using (SkeletonFrame SFrame = e.OpenSkeletonFrame())
        {
            if (SFrame == null)
            {
                return;
            }
            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)
            {
                processing(currentSkeleton.Joints[JointType.HandRight]);
                click(currentSkeleton.Joints[JointType.HandLeft], currentSkeleton.Joints[JointType.Head]);
            }
        }
    }
    private void processing(Joint handright)
    {
        Microsoft.Kinect.SkeletonPoint vector = new Microsoft.Kinect.SkeletonPoint();
        vector.X = ScaleVector(1600, handright.Position.X);
        vector.Y = ScaleVector(900, -handright.Position.Y);
        vector.Z = handright.Position.Z;
        handright.Position = vector;

        zAxis = handright.Position.Z;



        leftofscreen = Convert.ToInt32(handright.Position.X);
        topofscreen = Convert.ToInt32(handright.Position.Y);

        SetCursorPos(leftofscreen, topofscreen);

    }
    private void click(Joint handleft, Joint head)
    {
        if (handleft.Position.Y > head.Position.Y)
        {
            mouse_event(LEFTDOWN, leftofscreen, topofscreen, 0, 0);
            mouse_event(LEFTUP, leftofscreen, topofscreen, 0, 0);

        }
    }

    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;
    }

我已经尝试了这个kinect for c# devsimilar to youtube video code但是因为在本教程中他使用sdk 1.0并且一些方法是绝对的所以我无法跟踪我的手 有人可以帮助我如何用光标正确地缩放我的手部动作,这样我可以用手轻微移动光标移动到所有的屏幕上吗?

1 个答案:

答案 0 :(得分:0)

以下是Kinect sdk v2中的项目解决方案 https://github.com/bingcheng45/Kinect-v2-Hovering-Example

假设您已添加使用Microsoft.Kinect引用

  

首先,您首先将此部分添加到MainWindow.xaml

<k:KinectRegion x:Name="kinectRegion">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <Grid Grid.Row="0" Margin="10 0 10 20">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>


                <k:KinectUserViewer Grid.Column="1" Height="100" HorizontalAlignment="Center" VerticalAlignment="Top" />
                <TextBlock Grid.Column="1" HorizontalAlignment="Right" Margin="0 0 -1 0" VerticalAlignment="Bottom"  FontSize="18">Controls Basics</TextBlock>
            </Grid>

            <ContentControl Grid.Row="1" x:Name="navigationRegion">
                <Grid x:Name="kinectRegionGrid" Margin="10,20,10,20">
                    <Button x:Name="btnCenter" Content="CLICK HERE" HorizontalAlignment="Left" Margin="174,48,0,0" Style="{StaticResource 
                        FrameSelectorButtonStyle}" VerticalAlignment="Top" Width="158" Height="76"  MouseLeave="btnCenter_MouseLeave" Click="btnCenter_Click" MouseEnter="btnCenter_MouseEnter" Background="#FFC9C9C9" TouchEnter="btnCenter_TouchEnter"/>
                    <Label x:Name="lbtext" Content="Label" HorizontalAlignment="Left" Margin="51,31,0,0" VerticalAlignment="Top"/>

                </Grid>
            </ContentControl>
        </Grid>
    </k:KinectRegion>

然后在Mainwindow.Xaml.cs中添加这行代码,你就可以了!

public MainWindow()
        {
            InitializeComponent();

            //hovering start
            KinectRegion.SetKinectRegion(this, kinectRegion);

            App app = ((App)Application.Current);
            app.KinectRegion = kinectRegion;

            // Use the default sensor
            this.kinectRegion.KinectSensor = KinectSensor.GetDefault();
            //hovering end
        }

干杯!