/
leapimages.cpp
107 lines (81 loc) · 2.75 KB
/
leapimages.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <map>
#include <cmath>
#include <stdio.h>
#include <unistd.h>
#include <Leap.h>
using Leap::Controller;
using Leap::Frame;
using Leap::Tool;
using Leap::ToolList;
using Leap::Gesture;
using Leap::GestureList;
using Leap::CircleGesture;
using Leap::SwipeGesture;
const float RAD_TO_DEG = 57.2957795f;
class SampleListener : public Leap::Listener {
public:
virtual void onInit(const Controller&);
virtual void onConnect(const Controller&);
virtual void onDisconnect(const Controller&);
virtual void onExit(const Controller&);
virtual void onFrame(const Controller&);
virtual void onFocusGained(const Controller&);
virtual void onFocusLost(const Controller&);
virtual void onDeviceChange(const Controller&);
virtual void onServiceConnect(const Controller&);
virtual void onServiceDisconnect(const Controller&);
private:
};
const std::string fingerNames[] = {"Thumb", "Index", "Middle", "Ring", "Pinky"};
const std::string boneNames[] = {"Metacarpal", "Proximal", "Middle", "Distal"};
const std::string stateNames[] = {"STATE_INVALID", "STATE_START", "STATE_UPDATE", "STATE_END"};
void SampleListener::onInit(const Controller& controller) {
}
void SampleListener::onConnect(const Controller& controller) {
controller.enableGesture(Gesture::TYPE_CIRCLE);
controller.enableGesture(Gesture::TYPE_SWIPE);
}
void SampleListener::onDisconnect(const Controller& controller) {
}
void SampleListener::onExit(const Controller& controller) {
}
long counter = 0;
std::map<int, int> circle_pos;
void dump(const char *position, Leap::Image img) {
char name[60];
sprintf(name, "%s%06d.pgm", position, counter);
FILE *f = fopen(name, "wb+");
std::cout << img.width() << ", " << img.height() << std::endl;
fprintf(f, "P5\n%d %d\n255\n", img.width(), img.height());
fwrite(img.data(), img.width()*img.height(), 1, f);
fflush(f);
fclose(f);
}
void SampleListener::onFrame(const Controller& controller) {
const Frame frame = controller.frame();
const Leap::ImageList imgs = frame.images();
dump("left", imgs[0]);
dump("right", imgs[1]);
++counter;
}
void SampleListener::onFocusGained(const Controller& controller) {
}
void SampleListener::onFocusLost(const Controller& controller) {
}
void SampleListener::onDeviceChange(const Controller& controller) {
}
void SampleListener::onServiceConnect(const Controller& controller) {
}
void SampleListener::onServiceDisconnect(const Controller& controller) {
}
int main(int argc, char** argv) {
SampleListener listener;
Controller controller;
controller.addListener(listener);
controller.setPolicy(Leap::Controller::POLICY_IMAGES);
sleep(1000 * 1000 * 1000l);
// Remove the sample listener when done
controller.removeListener(listener);
return 0;
}