【问题标题】:How to get finger positions in Leap Motion using C#如何使用 C# 在 Leap Motion 中获取手指位置
【发布时间】:2016-03-31 09:26:36
【问题描述】:

我想使用 c# 在 Leap 运动中获取手指位置。我是 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");
    }
     } 
    }

【问题讨论】:

  • 将代码粘贴到问题中有助于展示您的努力。
  • 感谢您的建议# Mixxiphoid

标签: c# leap-motion


【解决方案1】:

您的代码看起来像是从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...
    }        
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    相关资源
    最近更新 更多