gpt4 book ai didi

java - Java 中的 Leap Motion 错过了点击手势

转载 作者:行者123 更新时间:2023-11-30 08:02:25 25 4
gpt4 key购买 nike

我编写了一个测试代码来查看我的点击手势是否被 Leap Motion 接受。运行代码后,我注意到我必须非常积极地点击,即使如此,每隔几次点击我也只能收到一次点击。我该如何更改我的代码来防止这种情况发生?

(请注意,我对编码很陌生,所以请详细说明,谢谢)

文件#1

    package com.leaptoarduino;
import com.leapmotion.leap.Controller;
import com.leapmotion.leap.Gesture;
public class Leapd
{
//Main
public static final void main(String args[])
{
//Initialize serial communications
RS232Protocol serial = new RS232Protocol();
serial.connect("COM3");
//Initialize the Leapd listener
LeapdListener leap = new LeapdListener(serial);
Controller controller = new Controller();
controller.addListener(leap);
controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
//Set up controller for gestures
controller.config().setFloat("Gesture.KeyTap.MinDownVelocity", 5.0f);
controller.config().setFloat("Gesture.KeyTap.HistorySeconds", .8f);
controller.config().setFloat("Gesture.KeyTap.MinDistance", 20.0f);
controller.config().save();
}

}

文件#2

    package com.leaptoarduino;
import com.leapmotion.leap.*;
import com.leapmotion.leap.Gesture.Type;
import com.leapmotion.leap.KeyTapGesture;
public class LeapdListener extends Listener
{
//Serial port that will be used to communicate with the Arduino
private RS232Protocol serial;
//Constructor
public LeapdListener(RS232Protocol serial)
{
this.serial = serial;
}
//Member function: onInit
public void onInit(Controller controller)
{
System.out.println("Initialized");
}
//Member function: onConncect
public void onConnect(Controller controller)
{
System.out.println("Connected");
}
//Member Function: onDisconnect
public void onDisconnect(Controller controller)
{
System.out.println("Disconnected");
}
//Member Function: onExit
public void onExit(Controller controller)
{
System.out.println("Exited");
}
//Member Function: onFrame
public void onFrame(Controller controller)
{

//Get the most recent frame
Frame frame = controller.frame();
//Verify a hand is in view
if (frame.hands().count() > 0)
{
GestureList gestures = frame.gestures();
for (Gesture gesture: gestures)
{
if (gesture.type() == Type.TYPE_KEY_TAP)
{
KeyTapGesture keytap = new KeyTapGesture (gesture);
System.out.println("KEYTAPPED");
}
}

//Send the tap data to the Arduino
// TBD
//Give the Arduino some time to process our data
try { Thread.sleep(30); }
catch (InterruptedException e) { e.printStackTrace(); }
}
}
}

最佳答案

当您在 OnFrame 处理程序中 hibernate 线程时,您将错过来自 Leap Motion 服务的帧。您的部分问题可能是在那些丢失的帧中识别了点击手势。 Frame.gestures() 采用“sinceFrame”参数来避免此问题:frame.gestures(sinceFrame) 为您提供sinceFrame 和当前帧之间发生的所有手势,因此您不会错过任何手势当帧被丢弃或跳过时。您要做的就是每次运行 onFrame 处理程序时将当前帧保存到 lastFrameProcessed 变量中。您将此lastFrameProcessed 变量传递给gestures() 以获取完整的手势列表。像这样的东西:

Frame lastFrameProcessed = Frame.invalid();
public void onFrame(Controller controller)
{

//Get the most recent frame
Frame frame = controller.frame();
//Verify a hand is in view
if (frame.hands().count() > 0)
{
GestureList gestures = frame.gestures(lastFrameProcessed);
for (Gesture gesture: gestures)
{
if (gesture.type() == Type.TYPE_KEY_TAP)
{
KeyTapGesture keytap = new KeyTapGesture (gesture);
System.out.println("KEYTAPPED");
}
}

//Send the tap data to the Arduino
// TBD
//Give the Arduino some time to process our data
try { Thread.sleep(30); }
catch (InterruptedException e) { e.printStackTrace(); }
}
lastFrameProcessed = frame;
}

关于java - Java 中的 Leap Motion 错过了点击手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31693994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com