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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
package com.hackingroomba.roombacomm;
import java.awt.*;
import java.net.*;
import java.util.*;
import java.awt.image.*;
import java.io.*;
import java.net.URL;
public class RoboRealmTest
{
public static void sendImage(RoboRealmAPI rr, Image img)
{
int width = img.getWidth(null);
int height = img.getHeight(null);
// create your own copy of the data in case the parent goes away ...
int pixelInt[] = new int[width*height];
byte pixelByte[] = new byte[width*height*3];
// Get the image data
PixelGrabber grabber = new PixelGrabber(img, 0, 0, width, height, pixelInt, 0, width);
try
{
grabber.grabPixels();
int i,j;
for (j=i=0;i<width*height;)
{
int num = pixelInt[i++];
pixelByte[j++] = (byte)(num&255);
pixelByte[j++] = (byte)((num>>8)&255);
pixelByte[j++] = (byte)((num>>16)&255);
}
// send image to RR
rr.setImage(pixelByte, width, height);
}
catch (Exception e)
{
};
}
// This is where the program first starts
public static void main(String[] args)
{
byte image[] = new byte[1280*960];
RoboRealmAPI rr = new RoboRealmAPI();
if (!rr.connect("localhost"))
{
System.out.println("Could not connect to RoboRealm on localhost!");
return;
}
// load an image using Java awt
Dimension d = rr.getDimension();
if (d!=null)
{
System.out.println("Dimension "+d.width+"x"+d.height);
}
// VARIABLES
// set a custom variable to test
rr.setVariable("custom_var", "test");
// read back our custom variable ... should be equal to 'test'
String res = rr.getVariable("custom_var");
if (!res.equals("test"))
{
System.out.println("Error in custom_var");
return;
}
// delete our custom variable
if (!rr.deleteVariable("custom_var"))
{
System.out.println("Error in delete variable");
return;
}
// try to get it back again ... should be empty
res = rr.getVariable("custom_var");
if (res!=null)
{
System.out.println("Error in delete custom_var");
return;
}
// set multiple variables
String names[] = new String[2];
String values[] = new String[2];
names[0]="custom_var_1";
names[1]="custom_var_2";
values[0] = "test1";
values[1] = "test2";
rr.setVariables(names, values, 2);
// get multiple variables
Vector v = rr.getVariables("custom_var_1, custom_var_2");
if (v==null)
{
System.out.println("Error in GetVariables, did not return any results");
return;
}
else
{
if (!((String)v.elementAt(0)).equals("test1"))
{
System.out.println("Error in get/set multiple variables. Got "+(String)v.elementAt(0));
return;
}
if (!((String)v.elementAt(1)).equals("test2"))
{
System.out.println("Error in get/set multiple variables. Got "+(String)v.elementAt(1));
return;
}
}
// IMAGES
// ensure that the camera is on and processing images
rr.setCamera("on");
rr.run("on");
// execute a RGB filter on the loaded image
rr.execute("<head><version>1.50</version></head><RGB_Filter><min_value>40</min_value><channel>3</channel></RGB_Filter>");
// get the current processed image from RoboRealm and save as a PPM
d = rr.getImage(image, 1280*960);
if (d!=null)
{
rr.savePPM("c:\\temp\\test.ppm", image, d.width, d.height);
}
// get the current source image from RoboRealm and save as a PPM
d = rr.getImage("source", image, 1280*960);
if (d!=null)
{
rr.savePPM("c:\\temp\\test2.ppm", image, d.width, d.height);
}
// turn off live camera
rr.setCamera("off");
// load an image for experimentation
d = rr.loadPPM("c:\\Program Files\\RoboRealm\\remo.ppm", image, 320*240*3);
// change the current image
rr.setImage(image, d.width, d.height);
// load an image using Java awt
try
{
Image img = Toolkit.getDefaultToolkit().getImage(new URL("http://www.google.com/intl/en_ALL/images/logo.gif"));
sendImage(rr, img);
}
catch (Exception e)
{
};
// add a marker image called my_new_image
rr.setImage("my_new_image", image, d.width, d.height);
// run a .robo program
//rr.loadProgram("c:\\Program Files\\RoboRealm\\scripts\\red.robo");
// load an image from disk
rr.loadImage(null, "c:\\Program Files\\RoboRealm\\remo.gif");
// save that image back to disk .. note that we can switch extensions
rr.saveImage(null, "c:\\temp\\remo.jpg");
rr.setCamera("on");
// change the camera to another one
//rr.setCamera("CompUSA PC Camera");
try
{
Thread.sleep(2000);
}
catch (Exception e)
{
};
// now set it back
//rr.setCamera("Logitech");
// turn off processing
rr.run("off");
try
{
Thread.sleep(2000);
}
catch (Exception e)
{
};
// run once
rr.run("once");
try
{
Thread.sleep(2000);
}
catch (Exception e)
{
};
// run for 100 frames (~3.3 seconds) .. note that if your frame rate is different this
// may be longer than 4 seconds
System.out.println("running for 100 frames");
rr.run("100");
try
{
Thread.sleep(4000);
}
catch (Exception e)
{
};
// turn processing back on
rr.run("on");
// wait for the image count to exceed 1000 (assuming a 30 fps here)
System.out.println("waiting for image count to exceed 1000");
rr.waitVariable("image_count", "500", 100000);
// wait for a new image
System.out.println("waiting for a new image");
rr.waitImage(5000);
// close the RoboRealm application .. if you want too ... otherwise leave it running
//rr.close();
// disconnect from API Server
rr.disconnect();
System.out.println("Finished RoboRealmTest");
}
}
|