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

时间:2016-03-31 09:26:36

标签: c# leap-motion

我希望使用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 :(得分: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...
    }        
}