forked from gibsjose/leap-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sample.java
90 lines (78 loc) · 3.25 KB
/
Sample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.io.IOException;
/******************************************************************************\
* Copyright (C) 2012 Leap Motion, Inc. All rights reserved. *
* NOTICE: This developer release of Leap Motion, Inc. software is confidential *
* and intended for very limited distribution. Parties using this software must *
* accept the SDK Agreement prior to obtaining this software and related tools. *
* This software is subject to copyright. *
\******************************************************************************/
class SampleListener extends Listener {
public void onInit(Controller controller) {
System.out.println("Initialized");
}
public void onConnect(Controller controller) {
System.out.println("Connected");
}
public void onDisconnect(Controller controller) {
System.out.println("Disconnected");
}
public void onFrame(Controller controller) {
// Get the most recent frame and report some basic information
Frame frame = controller.frame();
HandArray hands = frame.hands();
long numHands = hands.size();
System.out.println("Frame id: " + frame.id()
+ ", timestamp: " + frame.timestamp()
+ ", hands: " + numHands);
if (numHands >= 1) {
// Get the first hand
Hand hand = hands.get(0);
// Check if the hand has any fingers
FingerArray fingers = hand.fingers();
long numFingers = fingers.size();
if (numFingers >= 1) {
// Calculate the hand's average finger tip position
Vector pos = new Vector(0, 0, 0);
for (int i = 0; i < numFingers; ++i) {
Finger finger = fingers.get(i);
Ray tip = finger.tip();
pos.setX(pos.getX() + tip.getPosition().getX());
pos.setY(pos.getY() + tip.getPosition().getY());
pos.setZ(pos.getZ() + tip.getPosition().getZ());
}
pos = new Vector(pos.getX()/numFingers, pos.getY()/numFingers, pos.getZ()/numFingers);
System.out.println("Hand has " + numFingers + " fingers with average tip position"
+ " (" + pos.getX() + ", " + pos.getY() + ", " + pos.getZ() + ")");
}
// Check if the hand has a palm
Ray palmRay = hand.palm();
if (palmRay != null) {
// Get the palm position and wrist direction
Vector palm = palmRay.getPosition();
Vector wrist = palmRay.getDirection();
String direction = "";
if (wrist.getX() > 0)
direction = "left";
else
direction = "right";
System.out.println("Hand is pointing to the " + direction + " with palm position"
+ " (" + palm.getX() + ", " + palm.getY() + ", " + palm.getZ() + ")");
}
}
}
}
class Sample {
public static void main(String[] args) {
// Create a sample listener and assign it to a controller to receive events
SampleListener listener = new SampleListener();
Controller controller = new Controller(listener);
// Keep this process running until Enter is pressed
System.out.println("Press Enter to quit...");
try {
System.in.read();
} catch (IOException e) {
}
//The controller must be disposed of before the listener
controller = null;
}
}