Click here to Skip to main content
15,118,323 members
Articles / Programming Languages / Java
Posted 31 Jan 2014


14 bookmarked

How to Use OpenCV with Java under NetBeans IDE

Rate me:
Please Sign up or sign in to vote.
4.14/5 (10 votes)
21 Apr 2014CPOL4 min read
How to capture video from webcam and video file (*.AVI)


The project in this tip is how to capture video from webcam and video file (*.AVI). This project is made in OpenCV and NetBeans IDE platform.

This tip will help developers who loves the Java and OpenCV environment. The application is totally made in netbeans 6.9.1 version environment. The application shows the how to use the OpenCV with netbeans IDE. In this application multithreading concept is used, to capture video from file or web camera. This application is totally a demonstration for how to create applications in netbeans. In this tip, I explain how to configure the netbeans IDE in order to execute OpenCV appliactions.

Image 1

OpenCV 2.4.8 used for Java application, OpenCV supports Java from OpenCV 2.4.4 version. OpenCV 2.4.4 and OpenCV 2.4.6 version does not support VideoCapture (filename) function for read from file. It does not read the video file. In this project, the application reads the *.AVI file format video.


It stands for Open Source Computer Vision, it was designed especially for computational efficiency with strong focus on real time applications. It is written in optimized C/C++, and can take advantage of multi-core processing. In Image processing, it has been a great boon for the developers.

OpenCV is mostly a high-level library implementing algorithms for calibration techniques (Camera Calibration), feature detection (Feature) and tracking (Optical Flow), shape analysis (Geometry, Contour Processing), motion analysis (Motion Templates, Estimators), 3D reconstruction (View Morphing), object segmentation and recognition (Histogram, Embedded Hidden Markov Models, Eigen Objects). The essential feature of the library along with functionality and quality is performance.

OpenCV Basics: How To Start Work?

Start with Netbeans IDE: Configuring

The following steps show how to configure the netbeans 6.9.1 version with OpenCV 2.4.8 version. First of all, create the project. Then select the library shown in projects window. Then follow the following steps.

Step 1: Add Library

Select Libraries from netbeans project, right Click on Libraries, click "Add Library". Select Libraries> right click Libraries>Click Add Library.

Image 2

Step 2: Create Library

Click on "Create" button to create new library. Enter the Library Name, as "OpenCV2.4.8". Click on "OK" Button.

Image 3

Step 3: Customize Library

Select Classpath tab to add class path. Click on "Add JAR/Folder" to add class path. Select file path "c:\opencv\build\java\opencv-248.jar". Click on "OK" button.

Image 4

Step 4: Add Library

Select Configured library as,"opencv2.4.8". Click on "Add Library" button to add library to project work space.

Image 5

Step 5: Project Properties

Select project, right click on "Properties". Select "Run" option, Change VM options to add native library "-Djava.library.path="C:\opencv\build\java\x86"". If you have a 32-bit system, you need to select the x86 folder instead of x64.

Image 6

Using the Code

First, declare all variables and important objects to use:

private DaemonThread myThread = null;
int count = 0; 
VideoCapture webSource = null;
Mat frame = new Mat();
MatOfByte mem = new MatOfByte();
String File_path="";

Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution. A multithreading is a specialized form of multitasking. Multithreading requires less overhead than multitasking processing.

Multithreaded function is used for displaying video frame by frame. On clicking "Start" button, this function starts execution concurrently. On clicking "Stop" button, this function stops execution.

Creating a Thread

Java defines two ways in which this can be accomplished:

  • You can implement the Runnable interface.
  • You can extend the Thread class itself.

Create Thread by Implementing Runnable

The easiest way to create a thread is to create a class that implements the Runnable interface. To implement Runnable, a class needs to only implement a single method called run(), which is declared like this:

public void run() 

You will define the code that constitutes the new thread inside run() method. It is important to understand that run() can call other methods, use other classes, and declare variables, just like the main thread can.

After you create a class that implements Runnable, you will instantiate an object of type Thread from within that class. Thread defines several constructors. The one that we will use is shown here:

Thread(Runnable threadOb, String threadName) ; 

Here, threadOb is an instance of a class that implements the Runnable interface and the name of the new thread is specified by threadName.

After the new thread is created, it will not start running until you call its start( ) method, which is declared within Thread. The start( ) method is shown here:

void start(); 

The following run function grabs the video frame from webcam or video file. this block is synchronized.

public void run()
   synchronized(this)   {
       while(runnable)       {
          if(webSource.grab())       {
       try           {
    Highgui.imencode(".bmp", frame, mem);
    Image im = ByteArrayInputStream(mem.toArray()));
     BufferedImage buff = (BufferedImage) im;
     Graphics g=jPanel1.getGraphics();

      if (g.drawImage(buff, 0, 0, getWidth(), getHeight() -150 , 0, 0, buff.getWidth(), buff.getHeight(), null))
      if(runnable == false)
        System.out.println("Going to wait()");
     catch(Exception ex)

Points of Interest

  • Learn how to open video file
  • Learn how to open web camera
  • Learn how to configure the netbeans 6.9.1 IDE version
  • Learn how to use Multithreading


  • Java 2: The Complete Reference 5nd Edition - Herbert Schildt
  • The OpenCV Tutorials Release PDF
  • Head First Java 2nd Edition – Kathy Sierra and Bert Bates

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


  • 29 Jan 20114- First release


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


About the Author

Software Developer
India India
I am Soham Gandhi from India. I studied BE in Information Technology at University of Pune. ( 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.

Home Page (Blog)

You Tube Channel

Comments and Discussions

QuestionAyuda!!!!! Pin
Member 1270656127-Aug-16 7:38
MemberMember 1270656127-Aug-16 7:38 
QuestionOpenCV with Netbeans Pin
Member 1186478926-Aug-15 22:59
MemberMember 1186478926-Aug-15 22:59 
AnswerRe: OpenCV with Netbeans Pin
SOHAM_GANDHI22-Dec-15 0:33
MemberSOHAM_GANDHI22-Dec-15 0:33 
QuestionError while executing "java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_Mat()J" Pin
Member 117391364-Jun-15 23:17
MemberMember 117391364-Jun-15 23:17 
QuestionHighgui.imencode not found in opencv 3.0 Pin
Member 1159360620-May-15 2:14
MemberMember 1159360620-May-15 2:14 
AnswerRe: Highgui.imencode not found in opencv 3.0 Pin
Member 117391364-Jun-15 6:05
MemberMember 117391364-Jun-15 6:05 
GeneralRe: Highgui.imencode not found in opencv 3.0 Pin
SOHAM_GANDHI16-Jun-15 1:54
MemberSOHAM_GANDHI16-Jun-15 1:54 
Question"junit" and "junit_4" libraries could not be found in CameraCapture Pin
Anurag Panchal27-Mar-15 19:09
MemberAnurag Panchal27-Mar-15 19:09 
AnswerRe: "junit" and "junit_4" libraries could not be found in CameraCapture Pin
SOHAM_GANDHI27-Mar-15 19:53
MemberSOHAM_GANDHI27-Mar-15 19:53 
QuestionI don't have "C:\opencv\build\java\x86 folder" at its location. Pin
Member 1113789322-Jan-15 8:33
MemberMember 1113789322-Jan-15 8:33 
AnswerRe: I don't have "C:\opencv\build\java\x86 folder" at its location. Pin
SOHAM_GANDHI22-Jan-15 19:03
MemberSOHAM_GANDHI22-Jan-15 19:03 
AnswerRe: I don't have "C:\opencv\build\java\x86 folder" at its location. Pin
Member 116581332-May-15 7:24
MemberMember 116581332-May-15 7:24 
GeneralRe: I don't have "C:\opencv\build\java\x86 folder" at its location. Pin
SOHAM_GANDHI5-May-15 19:25
MemberSOHAM_GANDHI5-May-15 19:25 
GeneralNICE Pin
CHARLIE9126-May-14 20:35
MemberCHARLIE9126-May-14 20:35 
Questioni am getting this error...plz help... Pin
Member 1059640414-Feb-14 0:22
MemberMember 1059640414-Feb-14 0:22 
AnswerRe: i am getting this error...plz help... Pin
SOHAM_GANDHI20-Feb-14 7:35
MemberSOHAM_GANDHI20-Feb-14 7:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.