Opencv With Java
Opencv With Java
Getting started...
i. Download the JDK 7 from the Oracle website and install it
ii. Download and install the latest version of the Eclipse IDE (Luna, version 4.4) from the Eclipse Downloads
page (suggested package: Eclipse IDE for Java Developers)
iii. Download the OpenCV library (version 2.4.10) from http://opencv.org/
viii. Press Configure and use the default compilers for Unix Makefiles
ix. In the Ungrouped Entries group, insert the path to the Apache Ant executable (e.g., /opt/apache-ant1.9.4/bin/ant )
x. In the BUILD group, unselect:
BUILD_PERF_TESTS
BUILD_SHARED_LIBRARY to make the Java bindings dynamic library all-sufficient
BUILD_TESTS
xi. In the CMAKE group, set to Debug (or Release ) the CMAKE_BUILD_TYPE
xii. In the JAVA group:
insert the Java AWT include path (e.g., /usr/lib/jvm/java-7-oracle/include/ )
insert the Java AWT library path (e.g., /usr/lib/jvm/java-7-oracle/include/jawt.h )
insert the Java include path (e.g., /usr/lib/jvm/java-7-oracle/include/ )
insert the alternative Java include path (e.g., /usr/lib/jvm/java-7-oracle/include/linux )
insert the JVM library path (e.g., /usr/lib/jvm/java-7-oracle/include/jni.h )
xiii. Press Configure two times
xiv. The CMake window should appear with a white background, now: press Generate and close CMake
{
public static void main(String[] args)
{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
System.out.println("mat = " + mat.dump());
}
}
Expected output:
mat = [1, 0, 0;
0, 1, 0;
0, 0, 1]
{
this.capture.read(frame);
if (!frame.empty())
{
Imgproc.cvtColor(frame, frame, Imgproc.COLOR_BGR2GRAY);
imageToShow = mat2Image(frame);
}
}
catch (Exception e)
{
System.err.println("ERROR: " + e.getMessage());
}
}
return imageToShow;
}
private Image mat2Image(Mat frame)
{
MatOfByte buffer = new MatOfByte();
Highgui.imencode(".png", frame, buffer);
return new Image(new ByteArrayInputStream(buffer.toArray()));
}
public void setRootElement(Pane root)
{
this.rootElement = root;
}
OpenCV Javadoc
The OpenCV Javadoc, describing all the OpenCV APIs, are available at the address:
http://docs.opencv.org/java/
Known bugs
On Linux, for some (old) webcams, OpenCV can report the following error(s):
HIGHGUI ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV`
VIDIOC_STREAMON: Bad file descriptor
Such a problem can be resolved by setting the LD_PRELOAD environment variable to /usr/lib/x86_64-linuxgnu/libv4l/v4l1compat.so (or /usr/lib/i386-linux-gnu/libv4l/v4l1compat.so for 32 bit systems) directly in Eclipse