1 Using OpenCV Java with Eclipse {#tutorial_java_eclipse} 2 ============================== 3 4 Since version 2.4.4 [OpenCV supports Java](http://opencv.org/opencv-java-api.html). In this tutorial 5 I will explain how to setup development environment for using OpenCV Java with Eclipse in 6 **Windows**, so you can enjoy the benefits of garbage collected, very refactorable (rename variable, 7 extract method and whatnot) modern language that enables you to write code with less effort and make 8 less mistakes. Here we go. 9 10 Configuring Eclipse 11 ------------------- 12 13 First, obtain a fresh release of OpenCV [from download page](http://opencv.org/downloads.html) and 14 extract it under a simple location like `C:\OpenCV-2.4.6\`. I am using version 2.4.6, but the steps 15 are more or less the same for other versions. 16 17 Now, we will define OpenCV as a user library in Eclipse, so we can reuse the configuration for any 18 project. Launch Eclipse and select Window --\> Preferences from the menu. 19 20  21 22 Navigate under Java --\> Build Path --\> User Libraries and click New.... 23 24  25 26 Enter a name, e.g. OpenCV-2.4.6, for your new library. 27 28  29 30 Now select your new user library and click Add External JARs.... 31 32  33 34 Browse through `C:\OpenCV-2.4.6\build\java\` and select opencv-246.jar. After adding the jar, 35 extend the opencv-246.jar and select Native library location and press Edit.... 36 37  38 39 Select External Folder... and browse to select the folder `C:\OpenCV-2.4.6\build\java\x64`. If you 40 have a 32-bit system you need to select the x86 folder instead of x64. 41 42  43 44 Your user library configuration should look like this: 45 46  47 48 Testing the configuration on a new Java project 49 ----------------------------------------------- 50 51 Now start creating a new Java project. 52 53  54 55 On the Java Settings step, under Libraries tab, select Add Library... and select OpenCV-2.4.6, then 56 click Finish. 57 58  59 60  61 62 Libraries should look like this: 63 64  65 66 Now you have created and configured a new Java project it is time to test it. Create a new java 67 file. Here is a starter code for your convenience: 68 @code{.java} 69 import org.opencv.core.Core; 70 import org.opencv.core.CvType; 71 import org.opencv.core.Mat; 72 73 public class Hello 74 { 75 public static void main( String[] args ) 76 { 77 System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); 78 Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 ); 79 System.out.println( "mat = " + mat.dump() ); 80 } 81 } 82 @endcode 83 When you run the code you should see 3x3 identity matrix as output. 84 85  86 87 That is it, whenever you start a new project just add the OpenCV user library that you have defined 88 to your project and you are good to go. Enjoy your powerful, less painful development environment :) 89