使用Maven构建带有OpenCV的Java项目

5
我需要构建一个涉及OpenCV的项目。虽然很棒,但由于某些原因,库无法进入java.library.path。并且当我尝试使用库运行代码时,出现以下错误:
*java.lang.UnsatisfiedLinkError: no jniopencv_highgui in java.library.path* pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>ru.intemsys.reget.server</groupId>
  <artifactId>reget-server</artifactId>
  <version>0.1-alpha</version>
  <packaging>jar</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.build.timestamp.format>yyyyMMddhhmm</maven.build.timestamp.format>
    <platform.name>${os.name}-${os.arch}</platform.name>
    <product.year>2014</product.year>
    <jdkVersion>1.6</jdkVersion>
    <javacpp.version>0.7</javacpp.version>
    <javacv.version>0.7</javacv.version>
  </properties>

  <dependencies>
    <dependency>
        <groupId>commons-daemon</groupId>
        <artifactId>commons-daemon</artifactId>
        <version>1.0.10</version>
    </dependency>

      <dependency>
          <groupId>com.googlecode.javacpp</groupId>
          <artifactId>javacpp</artifactId>
          <version>${javacpp.version}</version>
      </dependency>
      <dependency>
          <groupId>com.googlecode.javacv</groupId>
          <artifactId>javacv</artifactId>
          <version>${javacv.version}</version>
      </dependency>
      <dependency>
          <groupId>com.googlecode.javacv</groupId>
          <artifactId>javacv</artifactId>
          <version>${javacv.version}</version>
          <classifier>linux-x86</classifier>
      </dependency>

      <dependency>
          <groupId>com.googlecode.javacv</groupId>
          <artifactId>javacv</artifactId>
          <version>${javacv.version}</version>
          <classifier>linux-x86_64</classifier>
      </dependency>

    <dependency>
        <groupId>com.xeiam.xchart</groupId>
        <artifactId>xchart</artifactId>
        <version>2.3.1</version>
    </dependency>

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
  </dependencies>

  <repositories>
      <repository>
          <id>javacpp</id>
          <name>JavaCPP</name>
          <url>http://maven2.javacpp.googlecode.com/git/</url>
      </repository>
      <repository>
          <id>javacv</id>
          <name>JavaCV</name>
          <url>http://maven2.javacv.googlecode.com/git/</url>
      </repository>
  </repositories>

  <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${jdkVersion}</source>
                    <target>${jdkVersion}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeScope>compile</includeScope>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <classpathLayoutType>simple</classpathLayoutType>
                            <mainClass>ru.intemsys.reget.server.App</mainClass>
                            <packageName>ru.intemsys.reget.server</packageName>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <finalName>${artifactId}-${version}</finalName>
                </configuration>
            </plugin>
        </plugins>
  </build>

    <profiles>
        <profile>
            <id>linux</id>
            <activation>
                <os><name>linux</name></os>
            </activation>
            <properties>
                <os.name>linux</os.name>
            </properties>
        </profile>
        <profile>
            <id>macosx</id>
            <activation>
                <os><name>mac os x</name></os>
            </activation>
            <properties>
                <os.name>macosx</os.name>
            </properties>
        </profile>
        <profile>
            <id>windows</id>
            <activation>
                <os><family>windows</family></os>
            </activation>
            <properties>
                <os.name>windows</os.name>
            </properties>
        </profile>
        <profile>
            <id>i386</id>
            <activation>
                <os><arch>i386</arch></os>
            </activation>
            <properties>
                <os.arch>x86</os.arch>
            </properties>
        </profile>
        <profile>
            <id>i486</id>
            <activation>
                <os><arch>i486</arch></os>
            </activation>
            <properties>
                <os.arch>x86</os.arch>
            </properties>
        </profile>
        <profile>
            <id>i586</id>
            <activation>
                <os><arch>i586</arch></os>
            </activation>
            <properties>
                <os.arch>x86</os.arch>
            </properties>
        </profile>
        <profile>
            <id>i686</id>
            <activation>
                <os><arch>i686</arch></os>
            </activation>
            <properties>
                <os.arch>x86</os.arch>
            </properties>
        </profile>
        <profile>
            <id>amd64</id>
            <activation>
                <os><arch>amd64</arch></os>
            </activation>
            <properties>
                <os.arch>x86_64</os.arch>
            </properties>
        </profile>
        <profile>
            <id>x86-64</id>
            <activation>
                <os><arch>x86-64</arch></os>
            </activation>
            <properties>
                <os.arch>x86_64</os.arch>
            </properties>
        </profile>
    </profiles>
</project>

mvn -version

Apache Maven 3.0.4 Maven安装路径: /usr/share/maven Java版本: 1.7.0_25, 厂商: Oracle Corporation Java安装路径: /usr/lib/jvm/java-7-openjdk-i386/jre 系统默认语言环境: ru_RU, 平台编码: UTF-8 操作系统名称: "linux", 版本号: "3.11.0-14-generic", 架构: "i386", 家族: "unix"

uname -a

Linux PC-1 3.11.0-14-generic #21-Ubuntu SMP Tue Nov 12 17:07:40 UTC 2013 i686 i686 i686 GNU/Linux


你要运行什么命令?在我看来,java.library.path选项不会自动设置。它应该指向一个包含OpenCV本地模块的目录。 - Tome
@Tome,mvn package。所有*.so文件都会被视为结果*.jar文件中的文件,位于目录/com/googlecode/javacv/cpp/linux-x86_64/和/com/googlecode/javacv/cpp/linux-x86/中。 - ftp27
据我所知,当打包在JAR文件中时,so文件不会被使用,它们必须直接从文件系统中访问,因此通常您会得到这样的结构:/lib/用于存放jar文件,而/bin/(或其他名称)用于存放so/dll文件。/bin/必须是java.library.path VM选项的值,在运行程序时必须给出该选项。 - Tome
2个回答

2

0

在2023年在Linux上安装OpenCV

安装并构建OpenCV

# Install minimal prerequisites (Ubuntu 18.04 as reference)
sudo apt update && sudo apt install -y cmake g++ wget unzip

# Download and unpack sources
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.x.zip

unzip opencv.zip
unzip opencv_contrib.zip

# Create build directory and switch into it
mkdir -p build && cd build

# Configure
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.x/modules ../opencv-4.x

# Build
cmake --build .

# Install
sudo make install

默认情况下,OpenCV将安装到/usr/local目录。

➤  tree /usr/local/share/java/opencv4
/usr/local/share/java/opencv4
├── libopencv_java470.so
└── opencv-470.jar


0 directories, 2 files

所以现在您可以使用您构建的Java绑定或添加maven pom.xml 依赖。最好匹配版本,4.7.04.7.0,但在我的情况下,maven repo 中的版本落后了。

        <dependency>
            <groupId>org.openpnp</groupId>
            <artifactId>opencv</artifactId>
            <version>4.6.0-0</version>
        </dependency>

使用环境参数LD_LIBRARY_PATH运行Java

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/share/java/opencv4/

从Java中加载OpenCV

System.loadLibrary("opencv_java470"); // If bindings version match use System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接