For the last couple of weeks I was trying to find a way to get images through my webcam using Java since I need to use the Real Time Java Specification (RTSJ) for a project at the University, My first option was the Java Media Framework (JMF) but after I tried it and not being able to detect any webcam that I had and founding that Sun hasn't work in it for 6 years, then I decided to look for other options, the first thing that came to my mind was the Java Native Interface (JNI) and V4L2, but after the frustration of understanding how to get an image out of my camera by using V4L2 I decided to search for OpenCV under Linux... and it was a brilliant idea, since I found that there were some Java binding already made, you can read more about them
here and you will need to download the following
files and i recomend you to use the
sample files to test it.
I got the OpenCV library from the Ubuntu repository, but if you want to compile the last version of the library here are some
instructions.
Then for some reason that I really don't understand you will need
Processing, I downloaded both the compiled version and the source code.
Now you will need to copy the following files in the same folder:
- core.jar
You will find this file in processing compiled version in the following path processing-1.0.3/lib
- processing directory
It contains some source code from processing, I got it from the following path processing/core/src, rember this is in the source package.
- OpenCV.jar libOpenCV.libjni libOpenCV.so
You will find it in the following path OpenCV/library, remember this must go in the output directory where the JAR file is generated.
- hypermedia directory
It contains some source code that you will need, path OpenCV/source/java
I finaly added one of the source examples codes, then I created a new project with existing source code and the I compiled... everything should go fine, after that if you try to run the proyect from Netbeans you will get the following error:
!!! required library not found : no OpenCV in java.library.path Verify that the java.library.path property is correctly set and 'libcxcore.so', 'libcv.so', 'libcvaux.so', 'libml.so', and 'libhighgui.so' are placed (or linked) in one of your system shared libraries folder
Exception in thread "main" java.lang.UnsatisfiedLinkError: hypermedia.video.OpenCV.capture(III)V at hypermedia.video.OpenCV.capture(Native Method)
Then I tried to run it as follows
java -Djava.library.path=/usr/lib -jar myfile.jar but it didn't work, so I went to the NetBeansProjects directory and look for the output JAR file, which usualy is located in the dist project subdirecory, there I created simbolic links to all the libraries that I needed, for example
ln -s /usr/lib/libcxcore.so libcxcore.so, I
cross my fingers run the above command with the terminal and
IT WORKED!!!
NOTE: The next thing to do in order to be able to run the project from Netbeans is to copy the symbolic links and libOpenCV.so and libOpenCV.jnilib to the jre/lib/i386.