-
Notifications
You must be signed in to change notification settings - Fork 2
/
javatest.java.in
167 lines (127 loc) · 7.04 KB
/
javatest.java.in
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
import swig.org.gphoto2.*;
public class javatest {
public static void main(String argv[]) {
final String sCameraModel="USB PTP Class Camera"; // "Canon Powershot A75";
System.out.println("loading JNI library \"gphoto2_jni\"");
System.loadLibrary("gphoto2_jni");
System.out.println(" JNI: Creating context...");
_GPContext context = gphoto2.gp_context_new();
// create and load list of ports - this should list all usb ports of all devices on the system, not just cameras
System.out.println(" JNI: gphoto2.create_null_p_p__GPPortInfoList()");
SWIGTYPE_p_p__GPPortInfoList ppInfoList=gphoto2.create_null_p_p__GPPortInfoList(); // create the empty pointer
System.out.println(" JNI: gphoto2.gp_port_info_list_new()");
gphoto2.gp_port_info_list_new(ppInfoList); // create the new (empty) list and get a pointer to it
System.out.println(" JNI: gphoto2.dereference_p_p__GPPortInfoList()");
_GPPortInfoList infolist=gphoto2.dereference_p_p__GPPortInfoList(ppInfoList); // dereference the pointer to get the (empty list) java object
System.out.println(" JNI: gphoto2.gp_port_info_list_load()");
int result = gphoto2.gp_port_info_list_load(infolist); // populate the list object with current data
if (result!=0) throw new RuntimeException("unexpected JNI result '" + result + "'");
_GPPortInfo portinfo = new _GPPortInfo();
/*
int k=gphoto2.gp_port_info_list_lookup_path (infolist, "usb:");
result=gphoto2.gp_port_info_list_get_info (infolist, k, portinfo);
if (result!=0) throw new RuntimeException("unexpected JNI result '" + result + "'");
*/
if (gphoto2.gp_port_info_list_count(infolist)==0) throw new RuntimeException("no cameras detected ");
// for each port in the list
for(int nCurPortNum=0; nCurPortNum<gphoto2.gp_port_info_list_count(infolist); nCurPortNum++)
{
System.out.println(" JNI: gphoto2.gp_port_info_list_get_info()");
_GPPortInfo portinfotmp=new _GPPortInfo();
gphoto2.gp_port_info_list_get_info(infolist, nCurPortNum, portinfotmp); // get more info about the current port
System.out.println(" port " + nCurPortNum + " = '" + portinfotmp.getPath() + "'");
if ("usb:".equalsIgnoreCase(portinfotmp.getPath()) ) {
portinfo=portinfotmp;
}
portinfotmp=null;
}
// release the info list resources
gphoto2.gp_port_info_list_free(infolist);
infolist=null;
ppInfoList=null;
// TODO is this really a camera on this port?
System.out.println(" JNI: gphoto2.create_null_p_p__Camera()");
SWIGTYPE_p_p__Camera ppCamera=gphoto2.create_null_p_p__Camera();
System.out.println(" JNI: gphoto2.gp_camera_new() - Creating camera...");
result=gphoto2.gp_camera_new (ppCamera);
if (result!=0) throw new RuntimeException("unexpected JNI result '" + result + "'");
System.out.println(" JNI: gphoto2.dereference_p_p__Camera()");
_Camera cam=gphoto2.dereference_p_p__Camera(ppCamera);
System.out.println(" JNI: gphoto2.create_null_p_p__CameraAbilitiesList()");
SWIGTYPE_p_p__CameraAbilitiesList p_al=gphoto2.create_null_p_p__CameraAbilitiesList();
System.out.println(" JNI: gphoto2.gp_abilities_list_new()");
result=gphoto2.gp_abilities_list_new(p_al);
if (result!=0) throw new RuntimeException("unexpected JNI result '" + result + "'");
System.out.println(" JNI: gphoto2.dereference_p_p__CameraAbilitiesList()");
_CameraAbilitiesList al=gphoto2.dereference_p_p__CameraAbilitiesList(p_al);
System.out.println(" JNI: gphoto2.gp_abilities_list_load() - Loading Abilities...");
result=gphoto2.gp_abilities_list_load(al, context);
if (result!=0) throw new RuntimeException("unexpected JNI result '" + result + "'");
System.out.println(" JNI: gphoto2.gp_camera_set_port_info() - Setting PortInfo on Camera...");
result=gphoto2.gp_camera_set_port_info(cam, portinfo);
if (result!=0) throw new RuntimeException("unexpected JNI result '" + result + "'");
System.out.println(" JNI: gphoto2.gp_abilities_list_get_abilities() - Using Camera Model '" + sCameraModel + "'");
CameraAbilities abilities=new CameraAbilities();
result=gphoto2.gp_abilities_list_get_abilities(al, gphoto2.gp_abilities_list_lookup_model(al, sCameraModel), abilities);
if (result!=0) throw new RuntimeException("unexpected JNI result '" + result + "'");
System.out.println(" JNI: gphoto2.gp_abilities_list_free()");
result=gphoto2.gp_abilities_list_free(al);
if (result!=0) throw new RuntimeException("unexpected JNI result '" + result + "'");
System.out.println(" JNI: gphoto2.gp_camera_set_abilities()");
result=gphoto2.gp_camera_set_abilities(cam, abilities);
if (result!=0) throw new RuntimeException("unexpected JNI result '" + result + "'");
for (int i = 0; result!=0 && i<5; i++) {
System.out.println(" JNI: gphoto2.gp_camera_init() - Initializing Camera...");
result = gphoto2.gp_camera_init(cam, context);
if (result != 0) {
System.out.println(" << gphoto2.gp_camera_init() returned " + result + "...");
try {
System.out.println(" **Sleeping 1000 msec...");
Thread.sleep(1000); // pause for a bit
} catch(InterruptedException e) {}
// -52 = camera not found? (when run with camera off)
// -60 = not a camera? (when tried to init usb mouse as camera, got -52 here too)
}
}
if (result == -60 || result == -52) { // looks like it wasn't a camera
System.out.println("(port '" + portinfo.getPath() + "' was not a camera?)");
}
/* TODO change timeout
SWIGTYPE_p_GPPort p = cam.getPort();
_GPPort pp = new _GPPort();
pp.setTimeout(1000*30);
*/
// Now, take a picture
System.out.println(" JNI: gphoto2.gp_camera_capture() - Capturing Image...");
CameraFilePath cfpOut = new CameraFilePath();
result=-1;
// int nLoops=0;
for (int i=0; result!=0 && i<5; i++) {
result = gphoto2.gp_camera_capture(cam, CameraCaptureType.GP_CAPTURE_IMAGE, cfpOut, context);
if (result != 0) System.out.println("unexpected JNI result '" + result + "', retrying...");
}
if (result != 0) throw new RuntimeException("unexpected JNI result '" + result + "'");
// TODO download image from camera
/*
* for each folder
* for each file
* check date
* download latest
* delete all
*/
/*
* Don't forget to clean up when you don't need the camera any
* more. Please use unref instead of destroy - you'll never know
* if some part of the program still needs the camera.
*/
System.out.println(" JNI: gphoto2.gp_camera_unref() - Releasing Camera...");
result = gphoto2.gp_camera_exit(cam, context);
result = gphoto2.gp_camera_unref(cam);
System.out.println("exiting");
}
}
/*
* Local Variables:
* mode: java
* End:
*/