Developing OPENCV programs with Java-1. Development environment Installation and configuration

Source: Internet
Author: User

Preface: See a lot of OpenCV's tutorials on the net are based on C and C + +, as a Java programmer expressed deep disobedience, then studied some, write the text to remember.

First, download and install the required development environment

Java Development Program, of course, the necessary natural is the JDK, about the installation of the JDK and environment variable configuration is no longer verbose, I believe you have already installed the local, this article uses JDK1.8, WINDOWS64 development environment, other environment is not tested, But theoretically it should all be possible.

Next is the choice of the editor, here or recommend idea, if you are a Java programmer, you must have heard, if you are an Android developer, then you should hear, Android Studio is based on idea of the community version of the development. I'm using idea's latest version of idea 2016.1 to download IntelliJ idea 2016. Full platform support, whether you're Windows or Mac or Linux. The installation process is no different from the more common software. After installation, it is recommended to write a "Hello World" applet test, if not successfully recommend to the Internet to search the installation process.

Down to the most important OPENCV debut. Currently the latest version of OpenCV is 3.1.0, so the latter are based on OpenCV3.1.0. Using Java development programs is not as complex as Visual Studio environment configuration, because Java is a method that calls directly into the OpenCV jar package. To the OpenCV home page to download the appropriate platform for the compressed package (though it is an. exe, smart you will definitely find a compressed package). After the download is done, we find a directory: Opencv->build->java note that the Java directory file is what we need, and the rest is not needed.

Second, the first Java version of the OPENCV program

So far we're almost ready to start the first Java version of the OPENCV program. The first program we will read a picture, OpenCV logo, and then the image of the information used in the format of the mat to print out.

Operation Result:

First we build a MAVEN project, and Java developers should know about Maven because the project is managed based on Maven and Git (which is mentioned later). Start idea, select "Create New Project", select "Maven" in the left sidebar of the popup screen, and if you have already tested "Hello World" program with think, then the JDK should already be set up, if not, you need to be in the right column " Project SDK Select the JDK that you installed locally. Interface

Then click "Next" next to set up the MAVEN project GroupId and Artifactid, here I fill in "GroupId:com.superbool", "Artifactid:easy_opencv", and then click "Next" Set the project name and save location, "Project Name:easy_opecv", "Project Location:d:\github\easy_opencv", and then click "Finish" to complete the creation of the MAVEN project, After the code is written in the project, there is no need to create a new project. Of course, the name and save path can be changed by yourself.

Before starting the first program, we have to create a few directories, we create a OPENCV directory under the parent directory of the EASY_OPENCV project, and then copy the contents of the Build_>java directory in the previously extracted OpenCV. Then create our first class Firstopencv.java under the Src->main->java directory.

Create a good MAVEN project as shown (you can also create these directories manually, for a detailed explanation of the role of each directory in the next article):

If you feel that the above steps are too cumbersome, then you can clone my code EASY_OPENCV directly on GitHub and switch to the Day1 branch.

Finally began to write code, the first Java-based OPENCV code:

1  Public classFIRSTOPENCV {2     Static {3 system.loadlibrary (core.native_library_name);4         //Note that when the program is running, you need to add the line in VM option to indicate the path to the OpenCV DLL file5         //-djava.library.path= $PROJECT _dir$\opencv\x646     }7 8      Public Static voidMain (string[] args) {9Mat mat = Imgcodecs.imread ("C:/opencv.png");Ten System.out.println (MAT); One     } A}

And so on, "Core.native_library_name" and "Imgcodecs.imread ()" Method are marked red, the original is OPENCV jar package has not been quoted in, so there is a step operation, OpenCV jar package, Click on the upper left corner of the Red Box logo icon, pop up the "Project Structure" option interface, select "Modules", "Dependencies", "+", "1 JARs or Dependencies ..." Select the "Opencv-310.jar" under the OpenCV directory, and then see if the code is floating red lines are gone.

Then use the CTR+SHIFT+F10 shortcut key to run the first main method, Ah Oh, the program throws an exception:

1Exception in thread "main"java.lang.UnsatisfiedLinkError:no opencv_java310 in Java.library.path2At Java.lang.ClassLoader.loadLibrary (classloader.java:1864)3At Java.lang.Runtime.loadLibrary0 (runtime.java:870)4At Java.lang.System.loadLibrary (system.java:1122)5At Com.superbool.firstopencv.<clinit> (firstopencv.java:12)6 At JAVA.LANG.CLASS.FORNAME0 (Native Method)7At Java.lang.Class.forName (class.java:264)8At Com.intellij.rt.execution.application.AppMain.main (appmain.java:122)9 TenProcess finished with exit code 1

Because the program Java program is called the local OpenCV dynamic link library, so when the JVM can not find the location of the. dll file, the solution is also very simple, click on the box "Firstopencv", "Edit Configurations "Pop up the following interface, add"-djava.library.path=\ $PROJECT _dirs\$\opencv\x64 "in the" VM Options "column (note that there is no slash in front of the dollar symbol), If it is a 32-bit system, please fill in "\x32".

Then use the CTR+SHIFT+F10 shortcut key to run the program, is not the result is finally out, here only the reading of the mat image of the basic information.

However, since OpenCV's official jar package does not yet provide the same Highgui interface as C + +, it is not possible to display the mat image on the interface for the time being, but in the next section we will write a way to display the mat image and explain the role of MAVEN in each directory.

Developing OPENCV programs with Java-1. Development environment Installation and configuration

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.