注意如果CMake在您的系统中不到Java,则在运⾏JAVA_HOME环境变量之前,使⽤安装JDK的路径设置JAVA_HOME环境变量。例如:export
JAVA_HOME=/usr/lib/jvm/java-6-oracle
现在开始构建:make -j8
要么msbuild / m OpenCV.sln / t:Build / p:Configuration = Release / v:m
除此之外,还将创建⼀个包含Java接⼝(bin/opencv-244.jar)和包含Java绑定和所有OpenCV内容(lib/libopencv_java244.so或
bin/Release/opencv_java244.dll分别))的本地动态库的jar 。稍后我们会使⽤这些⽂件。
Ant的Java⽰例注意在opencv/samples/java/ant⽂件夹中提供了描述的⽰例OpenCV库。创建⼀个⽂件夹,您将开发此⽰例应⽤程序。
在此⽂件夹中l,使⽤任何⽂本编辑器创建具有以下内容的⽂件:< project name = “SimpleSample” basedir = “。”default = “rebuild-run” >
< property name = “src.dir” value = “src” />
< property name = “lib.dir” value = “$ {ocvJarDir}” />
< path id = “classpath” >
< fileset dir = “$ {lib.dir}” includes = “** / *。jar” />
path >
< property name = “build.dir” value = “build” />
< property name = “classes.dir” value = “$ {build.dir} / classes” />
< property name = “jar.dir” value = “$ {build.dir} / jar” />
< property name = “main-class” value = “$ {ant.project.name}” />
< target name = “clean” >
< delete dir = “$ {build.dir}” />
target >
< target name = “compile” >
< mkdir dir = “$ {classes.dir}” />
< javac includeantruntime = “false” srcdir = “$ {src.dir}” destdir = “$ {classes.dir}” classpathref = “classpath” /> target >
< target name = “jar” depends = “compile” >
< mkdir dir = “$ {jar.dir}” />
< jar destfile = “$ {jar.dir} / $ {ant.project.name} .jar” basedir = “$ {classes.dir}” >
< manifest >
< attribute name = “Main-Class” value = “$ {main-class}” />
manifest >
jar >
target >
< target name = “run” depends = “jar” >
“ java fork = ”true“ classname = ”$ {main-class}“ >
< sysproperty key = “java.library.path” path = “$ {ocvLibDir}” />
为什么现在都用cmake< classpath >
< path refid = “classpath” />
< path location = “$ {jar.dir} / $ {ant.project.name} .jar” />
classpath >
java >
target >
< target name = “rebuild” depends = “clean,jar” />
< target name = “rebuild-run” depends = “clean,run” />
project >注意该XML⽂件可以重⽤于构建其他Java应⽤程序。它描述了第3 - 12⾏中的通⽤⽂件夹结构和⽤于编译和运⾏应⽤程序的常⽤⽬标。当重⽤该XML时,不要忘记修改第1⾏中的项⽬名称,也是主类的名称(第14⾏)。OpenCV jar和jni lib的路径预期为第5⾏中的参数(“$ {ocvJarDir}”和第37⾏中的“$ {ocvLibDir}”),但为⽅便起见,您可以对这些路径进⾏硬编码。有关其构建⽂件格式的详细说明,请参阅Ant⽂档。在src⽂件旁边创建⼀个⽂件夹l和⼀个SimpleSample.java⽂件。
将以下Java代码放⼊SimpleSample.java⽂件中:import Core;
import Mat;
import CvType;
import Scalar;
class SimpleSample {
static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
public static void main(String[] args) {
System.out.println("Welcome to OpenCV " + Core.VERSION);
Mat m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0));
System.out.println("OpenCV Mat: " + m);
Mat mr1 = m.row(1);
mr1.setTo(new Scalar(1));
Mat mc5 = m.col(5);
mc5.setTo(new Scalar(5));
System.out.println("OpenCV Mat data:\n" + m.dump());
}
}
在控制台中的⽂件夹中运⾏以下命令l:ant -DocvJarDir = path / to / dir / contained / opencv-244.jar -DocvLibDir =
path / to / dir / contained / opencv_java244 / native / library
例如:ant -DocvJarDir = X:\ opencv-2.4.4 \ bin -DocvLibDir = X:\ opencv-2.4.4 \ bin \ Release
该命令应启动[重新建⽴和运⾏样品。你应该在屏幕上看到这样的东西:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。