2.0 Environment Setup for OpenCV with Java
A correctly configured development environment is an absolute prerequisite for any practical work in computer vision. An improper setup can lead to frustrating errors that are difficult to diagnose. This section provides a systematic, step-by-step guide to downloading the official OpenCV library and integrating it with the popular Eclipse Integrated Development Environment (IDE) for Java development.
2.1 Installing the OpenCV Library
Obtaining the necessary OpenCV library files is the first step. The process is straightforward:
- Navigate to the official OpenCV homepage at http://opencv.org/.
- Access the Downloads page from the main navigation menu.
- Download the appropriate executable file for your operating system (e.g., opencv-3.1.0.exe for Windows).
- Run the executable to extract the library files to a folder on your system (e.g., C:/opencv/). Within the extracted contents, locate the build/java folder. Inside, you will find the crucial Java JAR file, such as opencv-310.jar. This file contains the Java classes we will use.
2.2 Configuring OpenCV with the Eclipse IDE
Once you have the OpenCV JAR file, you need to configure your Eclipse project to use it. This involves linking both the Java library and the underlying native libraries.
Setting the Java Build Path
Follow these steps to add the OpenCV JAR to a new or existing Java project in Eclipse:
- Open Eclipse and create a new Java Project (File -> New -> Java Project).
- Right-click on the newly created project in the Package Explorer.
- From the context menu, select Build Path -> Configure Build Path….
- In the Properties window that appears, navigate to the Libraries tab.
- Click the Add External JARs… button.
- Browse to the location where you saved the OpenCV JAR file (e.g., C:/opencv/build/java/opencv-310.jar) and select it.
- Click Apply and Close. The OpenCV JAR will now appear under “Referenced Libraries” in your project.
Setting the Path for Native Libraries (DLLs)
The Java library (.jar) is a wrapper around the core OpenCV code, which is written in C++ and compiled into native libraries (e.g., .dll files on Windows). Your Java application needs to know where to find these files to execute correctly.
These native library files are located within the extracted OpenCV folder, typically in build/java/x64 for 64-bit systems or build/java/x86 for 32-bit systems.
- Return to the Java Build Path window for your project (Right-click project -> Build Path -> Configure Build Path…).
- Under the Libraries tab, you will see the OpenCV JAR you added. Expand it by clicking the small triangle next to it.
- You will see an item named Native library location. Select it and click the Edit… button.
- In the “Native Library Folder Configuration” window, click External Folder….
- Browse to the folder containing the native libraries for your system’s architecture (e.g., C:/opencv/build/java/x64).
- Click OK to confirm the path, then Apply and Close to save the project configuration.
With both the Java JAR and the native libraries correctly linked, your development environment is now fully configured and ready for development. We can now explore how images are represented and handled in code.