libgphoto2-jna

This project is a Java API and JNA bindings for libgphoto2.  It allows Java applications to access cameras using the libgphoto2 library.  It is similar to the gphoto2-java project.  It has currently been tested on Ubuntu and Raspberry Pi and was developed as one part of a larger (proprietary) project.

Part of the motivation for building this library was to work around an issue in which the gphoto2 command-line interface stops responding when running on the Raspberry Pi.

How it Works

Java Native Access (JNA) lets Java make calls to native shared libraries.   libgphoto2-jna uses JNA bindings generated by jnaerator which have been modified and wrapped by an easy-to-use API.

Prerequisites

To use libgphoto2-jna, you will need:

  • Linux – gphoto2 does not run on Windows (unless you build it under Cygwin but I haven’t tested it).
  • ghoto2 or at a minimum, libgphoto2.so from the gphoto2 project.
  • a camera supported by gphoto2
  • Java JDK with JNA support and probably some development environment like Netbeans or Eclipse
  • Apache Ant (if not part of your IDE)
  • a git client

If you are a Java developer, you most likely have all these tools installed already.  On Debian/Ubuntu/Raspbian you can install the the prerequisites and test your setup like this:

sudo apt-get install openjdk-7-jdk libjna-java git ant gphoto2 
sudo apt-get install libgphoto2-6   #some distros may use libgphoto2-2 instead
gphoto2 --capture-image-and-download

How to Build

Make sure you have all the prerequisites installed, then run the following:

git clone https://github.com/angryelectron/libgphoto2-jna.git
cd libgphoto2-jna
ant #or open the project in netbeans IDE

The output, libgphoto2-jna.jar will be found in the “dist” directory and documentation can be found in “dist/javadoc”

How to Use

  1. Build the library (above), then add libgphoto2-jna.jar  to your Java project’s classpath.
  2. Make sure libgphoto2 is installed and that libgphoto2.so is in your library path.
  3. Read the API javadoc ( see dist/javadoc) and checkout the examples.

Here is an example showing how to take a pictures:

void example() throws IOException {
    GPhoto2 camera = new GPhoto2();
    camera.open();
    camera.capture();
    camera.close();
}

Here is another example showing how to modify several camera settings and then take 5 shots using burst mode.

void setBurstMode() throws IOException {
    GPhoto2 camera = new GPhoto2();
    camera.open();

    GPhoto2Config config = new GPhoto2Config(camera);
    config.readConfig();
    config.setParameter("burstnumber", "5");
    config.setParameter("capturetarget", "Memory card");
    config.writeConfig();
    camera.capture();
    camera.close();
}

Troubleshooting

Missing libgphoto2.so

The most common cause of this error is trying to use libgphoto2-jna on Windows.  libgphoto2.so is a Linux library,  which must be in your path.  Newer distros and packages may include libgphoto2.so.2 or libgphoto2.so.6 but not libgphoto2.so.    There are two fixes:  edit GPhoto2.Java line 90 and change the name of the library, or create a symbolic link.  For example:

sudo ln -s /usr/lib/i386-linux-gnu/libgphoto2.so.6 /usr/lib/i386-linux-gnu/libgphoto2.so

Other Issues

Please use the issue tracker on GitHub to report any other problems.