Files
libgphoto2/bindings/java/javatest.java.in
Hans Ulrich Niedermann 6625d9006e more java/SWIG build stuff
git-svn-id: https://svn.code.sf.net/p/gphoto/code/trunk/libgphoto2@8829 67ed7778-7388-44ab-90cf-0a291f65f57c
2006-05-28 23:44:29 +00:00

168 lines
7.0 KiB
Java

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:
*/