2017-07-18 85 views
0

我正在使用kinect 360和windows sdk 1.8。我有一個用c#wpf開發的應用程序,可以用手勢移動鼠標光標,並且還可以點擊。 問題是,只要我想將光標移動到角落,我需要在kinect前面走一步才能做到這一點。 但我想以這種方式縮放光標,所以我只需要移動我的手和光標移動到所有屏幕。如何使用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; 
    } 

的代碼,我自己正在使用SDK 1.0在本教程中嘗試這個kinect for c# devsimilar to youtube video code但由於有些方法是絕對的,所以我無法跟蹤我的手 能有人幫助我如何正確地用光標縮放我的手部動作,這樣我就可以將光標移動到所有屏幕上,並且手部有輕微的移動?

+0

你能使用kinect sdk v2嗎?如果可以的話,問題將得到解決。否則我可以嘗試解決您的問題 – bingcheng45

+0

@ bingcheng45我沒有試過sdk v2。你可以建議我一個鏈接或文章,我可以找到如何做與我在代碼與sdk v2相同的東西,但如果你可以幫助我與我的當前代碼,這將是非常好的你。先謝謝你。 –

+0

我真的找不到這篇文章,但我可以讓我的代碼只有懸停和一些默認手勢在github上運行,供您查看和嘗試。如果你發現有用的確給我一個upvote XP – bingcheng45

回答

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 
     } 

乾杯!

+0

非常感謝你的幫助,但它沒有工作,因爲我告訴你我有kinect的Xbox 360和SDK V2無法正常工作。 kinect sdk v2配置管理器說我的usb 3.0與kinect不兼容可能需要一個適配器 –

+0

但kinect工作正常,如果我使用sdk 1.8 ...... –

+0

嗯,如果它工作時,你使用sdk 1.8然後我不太確定關於差異。看完這個http://zugara.com/how-does-the-kinect-2-compare-to-the-kinect-1後,我覺得使用最新的sdk – bingcheng45

相關問題