首页 文章

如何使用C#在Leap Motion中获取手指位置

提问于
浏览
0

我想用c#获得Leap动作中的手指位置 . 我是跳跃运动控制器的新手 . 所以我找不到任何用于检测手指位置的示例代码,但我试图想出一些代码 . 我认为它有很多错误 . 那么,如何使用C#获取Leap Motion中的手指位置?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Leap;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form, ILeapEventDelegate
{


    private Controller controller;
    private LeapEventListener listener;
    public Form1()
    {
        InitializeComponent();

        this.controller = new Controller();
        this.listener = new LeapEventListener(this);
        controller.AddListener(listener);
    }


    delegate void LeapEventDelegate(string EventName);
    public void LeapEventNotification(string EventName)
    {
        if (!this.InvokeRequired)
        {
            switch (EventName)
            {
                case "onInit":
                    MessageBox.Show("onInit");
                    break;
                case "onConnect":
                    MessageBox.Show("onConnect");
                    break;
                case "onFrame":
                    MessageBox.Show("onFrame");
                    break;
            }
        }
        else
        {
  BeginInvoke(new LeapEventDelegate(LeapEventNotification), new object[] {
  EventName });   
         }
    }
    private void Form1_Load(object sender, EventArgs e)
    {

    }
    }


   public interface ILeapEventDelegate
   {
    void LeapEventNotification(string EventName);
    }

    public class LeapEventListener : Listener
   {
    ILeapEventDelegate eventDelegate;

    public LeapEventListener(ILeapEventDelegate delegateObject)
    {
        this.eventDelegate = delegateObject;
    }
    public override void OnInit(Controller controller)
    {
        this.eventDelegate.LeapEventNotification("onInit");
    }
    public override void OnConnect(Controller controller)
    {
        this.eventDelegate.LeapEventNotification("onConnect");
    }
    public override void OnFrame(Controller controller)
    {
        this.eventDelegate.LeapEventNotification("onFrame");
    }
    public override void OnExit(Controller controller)
    {
        this.eventDelegate.LeapEventNotification("onExit");
    }
    public override void OnDisconnect(Controller controller)
    {
        this.eventDelegate.LeapEventNotification("onDisconnect");
    }
     } 
    }

1 回答

  • 0

    您的代码看起来像是从documentation example派生的 . 但是,你遗漏了实际的事件处理程序,所以什么都不会发生 .

    您将在链接的示例中看到有一个newFrameHandler函数 . 您可以更改它以访问框架对象中的手指位置,这是在某个时刻跟踪的手的快照 .

    void newFrameHandler(Frame frame)
        {
            foreach(Finger in frame.Fingers){
                Vector fingerPosition = finger.TipPosition;
                //Do something with this information...
            }
        }
    

    您可以类似地获取手,然后访问每只手的手指,这通常是更自然的方法:

    foreach(Hand hand in frame.Hands){
        foreach(Finger in hand.Fingers){
            Vector fingerPosition = finger.TipPosition;
            //Do something with this information...
        }        
    }
    

相关问题