Click here to Skip to main content
Click here to Skip to main content

How to Use OpenCV with Java under Eclipse IDE

By , 3 Feb 2014
Rate this:
Please Sign up or sign in to vote.
Introduction

The project in this, how to use OpenCV with Java under Eclipse IDE platform. This will help developers who love Java and OpenCV environment. The application is totally made in Eclipse INDIGO version environment. The application shows how to use the OpenCV with Eclipse IDE.

In this application, we see how to capture video from webcam. This application is totally a demonstration for how to create applications in Eclipse INDIGO. In this, I explain how to configure the Eclipse INDIGO IDE in order to execute OpenCV applications.

Eclipse

Eclipse is free and an Open source software. Eclipse is multi-language software development environment. Eclipse is IDE, i.e., Integrated Development Environment. Eclipse can be used to develop an application in java. Eclipse is mostly used for JAVA. It contains workspace & plug-ins in system for customizing the environment. By means of various plug-ins, other programming languages including Ada, C, C++, COBOL, Perl, PHP, Python, Ruby (including Ruby on Rails framework), Scala, and Scheme. The IDE is often called Eclipse ADT for Ada, Eclipse CDT for C/C++, Eclipse JDT for Java, and Eclipse PDT for PHP.

Eclipse: How To Start Work?

Start with Eclipse INDIGO IDE: Configuring

The following steps show how to configure the Eclipse INDIGO version with OpenCV 2.4.8 version. First of all, create the project. Then select the properties from Project Menu. Then follow the steps listed below.

Step 1: Add External JARs File

Select Java Build Path, Select Libraries, Click on "Add External JARs". find opencv-248.jar file in "C:\opencv\build\java", select file, click open.

Properties>>Select Java Build Path>> Select Libraries >> Click Add External JARs >> Select File "C:\opencv\build\java\opencv-248.jar" >> Click Ok.

Step 2: Add Native Library

Select "Native Library Location" then click on "Edit" Button. put Location path "C:\opencv\build\java\x86"

Click OK.

Select Native Library Location >> Edit >> Location path ="C:\opencv\build\java\x86" >> OK.

Step 3: Finish

Finally, click on OK button to add in project.

Package Explorer

Finally all details are seen in Package Explorer. Default Package contains the VideoCap.java file, JRE System Library containing the all JavaSE packages and Reference libraries containing the opencv-2.4.8 package.

Using the Code

The following sample code is for web camera video capture. Below, I will be explaining the program with details, that how the program is executing.

//
// sample program to run in eclipse.
//
import org.opencv.core.*;
import org.opencv.highgui.Highgui;        
import org.opencv.highgui.VideoCapture;        
        
public class VideoCap {
    public static void main (String args[]){
    	System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    	VideoCapture camera = new VideoCapture(0);
    	
    	if(!camera.isOpened()){
    		System.out.println("Error");
    	}
    	else {
    		Mat frame = new Mat();
    	    while(true){
    	    	if (camera.read(frame)){
    	    		System.out.println("Frame Obtained");
    	    		System.out.println("Captured Frame Width " + 
    	    		frame.width() + " Height " + frame.height());
    	    		Highgui.imwrite("camera.jpg", frame);
    	    		System.out.println("OK");
    	    		break;
    	    	}
    	    }	
    	}
    	camera.release();
    }
}   

First of all, import the respective package in application. Following are the basic packages for implementing video capture from webcam or video file.

  • Core: Core package includes having basic data structures, including multi dimensional array Mat and basic function used by modules.
  • Highgui: Easy to use interface to images, videocapture, video codec, and simple GUI capabilities.
  • VideoCapture: VideoCapture subpackage of Highgui. VideoCapture is responsible for reading video from webcam or video file. It is also responsible for setting or getting video properties like frame height, frame width.
import org.opencv.core.*;
import org.opencv.highgui.Highgui;        
import org.opencv.highgui.VideoCapture;  

Secondly, we need to load the native library. This command must be executed once per Java process to using native OpenCV methods. If it does not get used, it throws an exception like "UnsatisfiedLink errors."

System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 

The following statement states that the camera variable is initialized with VideoCapture with 0 "Zero" parameter. It means that zero is first device ID. It accesses the first web camera. Video Camera is also initialized with video file, just pass video file path.

VideoCapture camera = new VideoCapture(0); 

OR

VideoCapture camera = new VideoCapture("F\\video.avi"); 

isOpned function is used to check if the binding of the class to a video source that means device index or video source file path was successful or not to use.

if(!camera.isOpened()){
    		System.out.println("Error");
    	} 

Mat class with two data parts, first the matrix header which contains information such as the size of the matrix and second a pointer to the matrix contains the pixel values. The matrix header size is constant.

Mat frame = new Mat();

Following is an infinite loop till the frame is not obtained. If frame has been obtained, it breaks the loop. The following loop prints the frame height and width, it also writes the frame to file and break the loop.

	while(true){
    	    	if (camera.read(frame)){
    	    		System.out.println("Frame Obtained");
    	    		System.out.println("Captured Frame Width " + 
    	    		frame.width() + " Height " + frame.height());
    	    		Highgui.imwrite("camera.jpg", frame);
    	    		System.out.println("OK");
    	    		break;
    	    	}
    	    }	  

imwrite is a static method from the Highgui class. This method is used to write an image to a file. In the following frame variable is written to camera.jpg file.

Highgui.imwrite("camera.jpg", frame); 

For closing the video, we need to call its release function.

camera.release();

Point of Interest

  • Learn how to Open WebCamera
  • Learn how to Configure Eclipse

Reference

  • Java 2: The Complete Reference 5nd Edition - Herbert Schildt
  • The OpenCV Tutorials Release 2.4.8.0 PDF
  • http://www.opencv.org/

Sorry

Sorry for my English. If you notice errors or can suggest a more correct version, please let me know.

History

  • 3rd February, 2014: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

SOHAM_GANDHI
Software Developer
India India
I am Soham Gandhi from India. I studied BE in Information Technology at University of Pune. (www.unipune.ac.in). I have been learning OpenCV, Enjoying it.
My research interest in Image Processing, Artificial Intelligence, Security Systems, Security Metrics. I work on OpenCV, EmguCV, Visual Studio 2008/10, Java platform.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 3 Feb 2014
Article Copyright 2014 by SOHAM_GANDHI
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid