用 ANT 构建一个 jar(对于 javacv 0.9):
安装opencv(和/或ffmpeg)
从https://github.com/bytedeco/javacv下载 javacv
创建一个 Java 项目并在根目录中创建目录“lib”、“lib32”和“lib64”,并将 x64 jar 从 javacv 添加到 lib64 - 对于 lib32 和其他没有位版本的 lib 相同
将以下 jars 添加到项目的 buildpaht(添加外部 JAR...)并为 opencv(和/或 ffmpeg)设置本机库路径
- 样本测试类
public class Main {
public static void main(String[] args) throws Exception, IOException {
OpenCVFrameGrabber frameGrabber = new OpenCVFrameGrabber(new File(".\\tmp_files\\small.mp4"));
frameGrabber.start();
IplImage origImg = frameGrabber.grab();
//create canvas frame named 'Demo'
final CanvasFrame canvas = new CanvasFrame("Demo");
//Show image in canvas frame
canvas.showImage(origImg);
//This will close canvas frame on exit
canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
}
}
- ANT 脚本
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="main" name="Main">
<property environment="env"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.6"/>
<property name="source" value="1.6"/>
<property name="build.dir" value="bin"/>
<property name="src.dir" value="src"/>
<property name="lib.dir" value="lib"/>
<property name="lib32.dir" value="lib32"/>
<property name="lib64.dir" value="lib64"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="jar.file" value="${jar.dir}/buildOpenCvTest32_64.jar"/>
<property name="manifest.file" value="${jar.dir}/MANIFEST.MF"/>
<property name="main.class" value="test.Main"/>
<path id="external.jars">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
<fileset dir="${libV.dir}" includes="**/*.jar"/>
</path>
<path id="project.classpath">
<pathelement location="${src.dir}"/>
<path refid="external.jars" />
</path>
<target name="init">
<mkdir dir="${build.dir}"/>
<mkdir dir="${classes.dir}"/>
<mkdir dir="${jar.dir}"/>
<copy includeemptydirs="false" todir="${build.dir}">
<fileset dir="${src.dir}">
<exclude name="**/*.launch"/>
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="cleanall" depends="clean"/>
<target name="build" depends="init">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" source="${source}" target="${target}" classpathref="project.classpath">
<src path="${src.dir}"/>
</javac>
</target>
<target name="build-jar" depends="build">
<delete file="${jar.file}" />
<delete file="${manifest.file}" />
<manifest file="${manifest.file}" >
<attribute name="built-by" value="${user.name}" />
<attribute name="Main-Class" value="${main.class}" />
</manifest>
<jar destfile="${jar.file}"
basedir="${build.dir}"
manifest="${manifest.file}">
<fileset dir="${classes.dir}" includes="**/*.class" />
<zipgroupfileset dir="${lib.dir}" includes="**/*.jar" />
<zipgroupfileset dir="${lib32.dir}" includes="**/*.jar" />
<zipgroupfileset dir="${lib64.dir}" includes="**/*.jar" />
</jar>
</target>
<target name="main" depends="build-jar">
<description>Main target</description>
</target>
</project>
- 运行 ANT - jar 应该在每个 32 位和 64 位 Windows 系统上运行。同样适用于 ffmpeg 库和 unix 系统