Click here to Skip to main content
Click here to Skip to main content
Go to top

How to Use OpenCV with Java under NetBeans IDE

, 21 Apr 2014
Rate this:
Please Sign up or sign in to vote.
How to capture video from webcam and video file (*.AVI)

Introduction

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.

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.

OpenCV

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.

Step 2: Create Library

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

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.

Step 4: Add Library

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

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.

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           {
    webSource.retrieve(frame);
    Highgui.imencode(".bmp", frame, mem);
    Image im = ImageIO.read(new 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()");
        this.wait();
       }
 }
     catch(Exception ex)
             {
            System.out.println("Error");
             }
        }
     }
  }
}

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

References

  • Java 2: The Complete Reference 5nd Edition - Herbert Schildt
  • The OpenCV Tutorials Release 2.4.8.0 PDF
  • http://www.opencv.org/
  • 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.

History

  • 29 Jan 20114- First release

License

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

Share

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.
 
Home Page (Blog)
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralNICE PinmemberCHARLIE9126-May-14 19:35 
Questioni am getting this error...plz help... PinmemberMember 1059640413-Feb-14 23:22 
AnswerRe: i am getting this error...plz help... PinmemberSOHAM GANDHI20-Feb-14 6:35 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 21 Apr 2014
Article Copyright 2014 by SOHAM_GANDHI
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid