Examples of using OpenCV in java in NetBeans

Source: Internet
Author: User
Tags thread class netbeans

This article will help developers who prefer the java and OpenCV environments. This program is completely created using netbeans 6.9.1. This application shows how to use OpenCV in the netbeans environment. In this example, the concept of multithreading is used to capture videos from a network camera or video file. This example shows how to use netbeans to create a program. In this article, I reveal how to configure netbeans to execute OpenCV programs.

OpenCV2.4.8 supports java development. From 2.4.4, opencv began to support java development. OpenCV 2.4.4 and OpenCV 2.4.6 do not support capturing videos from files. They do not support reading video files. In this project, opencv will read video files in AVI format.
OpenCV
It indicates a cross-platform computer vision library. He was designed to handle the efficient memory processing of real-time systems. It is written by optimized c/c ++. It implements multi-core processing. In image processing, it presents disruptive performance to developers.
In general, OpenCV uses the calibration technology (camera calibration), feature recognition and tracking (optical flow), Model Analysis (Geometric, contour processing), action analysis (action template, estimation value ), 3D reproduction (image deformation), object segmentation and recognition (histogram, embedded hidden Markov model, inherent object) algorithms. The essential features of this database are presented by functions, and the processing results are of superior quality.
OpenCV basics: how to use it?
Configure Netbeans IDE:
The following steps demonstrate how to configure OpenCV 2.4.8 in netbeans6.9.1. First, create a project. Then select the class library to display in the project window. Follow these steps. Netbeans IDE
Step 1: add a class library
Select a class library from the netbeans project. Mail class Library, click "Add Library", select the Libraries folder, mail Libraries, and click Add Library.

Step 2: create a class library

Click "Create" to Create a new class library. Enter the class library name, for example, "OpenCV2.4.8 ". Click "OK"

 

Step 2: create a class library
Click "Create" to Create a new class library. Enter the class library name, for example, "OpenCV2.4.8 ". Click OK.
Step 3: Customize the class library
Select the Classpath label to add the class path. Click "Add JAR/Folder" to Add the class path. Select the file path "c: opencvuildjavaopencv-248.jar ". Click OK.
Step 4: add a class library
Select the configuration library, for example, "opencv2.4.8 ". Click "Add Library" to Add Redu to the project directory.
Step 5: project properties
Select a project and right-click "Properties ". Select "Run" option, Change VM options to add native library "-Djava. library. path =" C: opencvuildjava "".

Code
First, declare all variables and important objects to use:

 

The code is as follows: Copy code
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 multi-threaded programming. A multi-threaded program contains two or more code blocks that can run simultaneously. Each such code block is called as a thread. Then, each thread defines a separate path to execute the code block. A multi-threaded program is an instance of a multi-task program. Multithreading requires a little less than multi-task.
The multi-threaded section of the palace is used to display a video frame by frame. Click the "Start" button to run this function concurrently. Click the Stop button to terminate the function.
Create a thread
Java defines two ways to create a thread:
You can use the Runnable interface.
You can implement it by extending the Thread class itself.
Use Runnable to create a thread
The simplest way to create a thread is to create a class that implements the Runnable interface. To implement the Runnable interface, this class only needs to implement one run () method. The statement of this method is as follows:
1

The code is as follows: Copy code
Public void run ()

You need to define a code block containing the new thread in the run () method. You must understand that the run () method can call other methods like the main thread, use other classes, and declare variables.
After you create a class that implements the Runnable interface, you will instantiate an object of the Thread type in that class. Thread defines a series of constructors. The constructor we will use is as follows:

The code is as follows: Copy code
Thread (Runnable threadOb, String threadName );

Here, threadOb is an instance that implements the Runnable interface, and threadName is the name of the new thread.
When the thread is created, it will not stop running until you call the start () method defined in the thread. The start () method is as follows:

The code is as follows: Copy code


Void start ();

The following run function captures a video from a network camera or video file by frame. This process is synchronized.

The code is as follows: Copy code
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 ");
             }
        }
     }
  }
}

Point of interest

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.