Notice: MediaWiki has been updated. Report any rough edges to marcan@marcan.st
Java JNA Wrapper: Difference between revisions
(started JNA wrapper page) |
(cleanup, small demo, sample code) |
||
Line 8: | Line 8: | ||
== Current Status == | == Current Status == | ||
See https://github.com/OpenKinect/libfreenect/pull/131 | |||
== Compilation == | == Compilation == | ||
'''Maven''' | |||
If you have Maven (http://maven.apache.org/) installed, compiling could be as easy as: | |||
$ cd ./wrappers/java/ | |||
$ mvn package | |||
'''RedHat/Fedora Linux''' | '''RedHat/Fedora Linux''' | ||
Line 24: | Line 30: | ||
<br>2. Move to the Java wrapper directory: | <br>2. Move to the Java wrapper directory: | ||
$ cd ./wrappers/java/ | $ cd ./wrappers/java/ | ||
3. Make a ./lib directory and a ./jars directory | |||
<br>4. Download jna.jar into ./jars (available from https://jna.dev.java.net/servlets/ProjectDocumentList) | <br>4. Download jna.jar into ./jars (available from https://jna.dev.java.net/servlets/ProjectDocumentList) | ||
<br>5. Copy the libfreenect library (e.g. libfreenect.so.0.0.1) into ./lib | <br>5. Copy the libfreenect library (e.g. libfreenect.so.0.0.1) into ./lib | ||
Line 35: | Line 41: | ||
== Demo == | == Demo == | ||
<source lang="java" border="1"> | |||
import org.openkinect.freenect.*; | |||
public class JNATest { | |||
public static void main(String[] args) throws InterruptedException { | |||
// DECLARATIONS | |||
Context ctx = null; | |||
Device dev = null; | |||
// INITIALIZE DEVICE | |||
ctx = Freenect.createContext(); | |||
if (ctx.numDevices() > 0) { | |||
dev = ctx.openDevice(0); | |||
} else { | |||
System.err.println("No kinects detected. Exiting."); | |||
System.exit(0); | |||
} | |||
// TILT UP, DOWN, & RETURN | |||
dev.setTiltAngle(20); | |||
Thread.sleep(4000); | |||
dev.setTiltAngle(-20); | |||
Thread.sleep(4000); | |||
dev.setTiltAngle(0); | |||
// SHUT DOWN | |||
if (ctx != null) | |||
if (dev != null) { | |||
dev.close(); | |||
} | |||
ctx.shutdown(); | |||
} | |||
} | |||
</source> | |||
These instructions assume that you've followed the Compilation instructions above and have created ./wrappers/java/jars and ./wrappers/java/lib with the appropriate files. | |||
1. Save as JNATest.java in ./wrappers/java/ | |||
<br>2. Compile: | |||
$ javac -classpath ./jars/freenect-jna.jar JNATest.java | |||
3. Execute: | |||
$ su -c "java -Djna.library.path=./lib -classpath .:./jars/jna.jar:./jars/freenect-jna.jar JNATest" | |||
This demo should tilt the Kinect up 20 degrees, wait 4 seconds, tilt it down 40 degrees (20 degrees from level), wait 4 seconds, and return to level. | |||
== Sample Code == | == Sample Code == | ||
See ./wrappers/java/src/test/java/org/openkinect/freenect/FreenectTest.java | |||
[[Category:Wrappers]] | [[Category:Wrappers]] |
Revision as of 20:41, 17 December 2010
Effort to create a Java JNA (Java Native Access) wrapper for libfreenect.
About
JNA provides Java programs easy access to native shared libraries. See https://jna.dev.java.net/
Who is involved
Coordinator/Development: Mark Renouf
Current Status
See https://github.com/OpenKinect/libfreenect/pull/131
Compilation
Maven
If you have Maven (http://maven.apache.org/) installed, compiling could be as easy as:
$ cd ./wrappers/java/ $ mvn package
RedHat/Fedora Linux
These are command line instructions and don't make many assumptions about configuration Java class paths or native library paths. Since only only JAR (jna.jar) and one native library (libfreenect) are needed, we assume that you'll put a copy of each in a relative path directory. This could be made even simpler if your classpath and library environment variables are properly tuned, but this approach should work they are not.
1. Install libfreenect based RedHat/Fedora instructions here: http://openkinect.org/wiki/Installation/Compilation_Guides Until JNA code has been merged into https://github.com/OpenKinect/libfreenect.git you'll need to work with the java-updates branch of tweakt's fork:
$ git clone https://github.com/tweakt/libfreenect.git $ cd libfreenect $ git checkout java-updates
Note that you probably don't have to go beyond the "clone the repository" step, but probably a good idea to make sure that glview is working before proceeding.
2. Move to the Java wrapper directory:
$ cd ./wrappers/java/
3. Make a ./lib directory and a ./jars directory
4. Download jna.jar into ./jars (available from https://jna.dev.java.net/servlets/ProjectDocumentList)
5. Copy the libfreenect library (e.g. libfreenect.so.0.0.1) into ./lib
6. Build the libfreenect JNA jar:
$ rm -r ./tmpjar $ mkdir ./tmpjar $ javac -d ./tmpjar -classpath ./jars/jna.jar ./src/main/java/org/openkinect/freenect/*.java $ jar -cvf ./jars/freenect-jna.jar -C tmpjar org $ rm -r ./tmpjar
Demo
import org.openkinect.freenect.*;
public class JNATest {
public static void main(String[] args) throws InterruptedException {
// DECLARATIONS
Context ctx = null;
Device dev = null;
// INITIALIZE DEVICE
ctx = Freenect.createContext();
if (ctx.numDevices() > 0) {
dev = ctx.openDevice(0);
} else {
System.err.println("No kinects detected. Exiting.");
System.exit(0);
}
// TILT UP, DOWN, & RETURN
dev.setTiltAngle(20);
Thread.sleep(4000);
dev.setTiltAngle(-20);
Thread.sleep(4000);
dev.setTiltAngle(0);
// SHUT DOWN
if (ctx != null)
if (dev != null) {
dev.close();
}
ctx.shutdown();
}
}
These instructions assume that you've followed the Compilation instructions above and have created ./wrappers/java/jars and ./wrappers/java/lib with the appropriate files.
1. Save as JNATest.java in ./wrappers/java/
2. Compile:
$ javac -classpath ./jars/freenect-jna.jar JNATest.java
3. Execute:
$ su -c "java -Djna.library.path=./lib -classpath .:./jars/jna.jar:./jars/freenect-jna.jar JNATest"
This demo should tilt the Kinect up 20 degrees, wait 4 seconds, tilt it down 40 degrees (20 degrees from level), wait 4 seconds, and return to level.
Sample Code
See ./wrappers/java/src/test/java/org/openkinect/freenect/FreenectTest.java