文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Java利用JavaCPP调用算法示例

2024-04-02 19:55

关注

配置liunx 环境系统


配置so 文件存放路径
[root@arch2 ~]# cat /etc/ld.so.conf.d/so.conf 
/opt/app/tools/so/

从新调用ldconfig 命令
ldconfig

配置java 项目

配置pom 文件


<?xml version="1.0" encoding="UTF-8"?>
<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>org.jacdong</groupId>
 <artifactId>javacpptest</artifactId>
 <version>0.1-SNAPSHOT</version>
 
   <properties>
   
        <include.path>${basedir}/cppbuild</include.path>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <javacpp.cppbuild.skip>true
        </javacpp.cppbuild.skip> <!-- To skip execution of cppbuild.sh: -Djavacpp.cppbuild.skip=true -->
        <javacpp.parser.skip>false
        </javacpp.parser.skip>     <!-- To skip header file parsing phase: -Djavacpp.parser.skip=true  -->
        <javacpp.compiler.skip>true
        </javacpp.compiler.skip> <!-- To skip native compilation phase: -Djavacpp.compiler.skip=true -->
        <javacpp.moduleId>${project.artifactId}</javacpp.moduleId>
        <javacpp.platform.root></javacpp.platform.root>
        <javacpp.platform.compiler></javacpp.platform.compiler>
        <javacpp.platform.extension></javacpp.platform.extension>
        <javacpp.platform.linux-x86>linux-x86</javacpp.platform.linux-x86>
        <javacpp.platform.linux-x86_64>linux-x86_64</javacpp.platform.linux-x86_64>
        <javacpp.platform>linux-x86_64</javacpp.platform>

        <skipTests>true</skipTests>

    </properties>

 <dependencies>
  <dependency>
   <groupId>org.bytedeco</groupId>
   <artifactId>javacpp</artifactId>
   <version>1.5.3</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/junit/junit -->
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
  </dependency>
 </dependencies>

 <build>
  <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
            </plugin>
            <plugin>
                <groupId>org.bytedeco</groupId>
                <artifactId>javacpp</artifactId>
                <version>1.5.3</version>
                <configuration>
                    <properties>${javacpp.platform}</properties>
                    <propertyKeysAndValues>
                        <property>
                            <name>platform.root</name>
                            <value>${javacpp.platform.root}</value>
                        </property>
                        <property>
                            <name>platform.compiler</name>
                            <value>${javacpp.platform.compiler}</value>
                        </property>
                        <property>
                            <name>platform.extension</name>
                            <value>${javacpp.platform.extension}</value>
                        </property>
                    </propertyKeysAndValues>
                    <classPath>${project.build.outputDirectory}</classPath>
                    <includePaths>
                        <includePath>${basedir}/src/main/resources/linux-x86_64</includePath>
                    </includePaths>
                    <linkPaths>
                    </linkPaths>
                </configuration>
                <executions>
                    <execution>
                        <id>javacpp.parser</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                        <configuration>
                           <skip>false</skip>
                            <outputDirectory>${project.build.sourceDirectory}</outputDirectory>
                            <classOrPackageName>com.jacdong.preset.*</classOrPackageName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <forkMode>once</forkMode>
                    <environmentVariables>
                        <LD_LIBRARY_PATH>target/classes/linux-x86_64</LD_LIBRARY_PATH>
                    </environmentVariables>
                </configuration>
            </plugin>
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
     <execution>
      <id>build-jnilib</id>
      <phase>process-classes</phase>
      <goals>
       <goal>exec</goal>
      </goals>
      <configuration>
       <executable>java</executable>   
                            <commandlineArgs>-jar ./libs/javacpp-1.5.jar -Xcompiler -g -Xcompiler -ggdb -nodelete -d target/classes/linux-x86_64 -Xcompiler -I${env.JAVA_HOME}/include -Xcompiler -I${env.JAVA_HOME}/include/linux -Xcompiler -fvisibility=default -Xcompiler -rdynamic -Xcompiler -std=c++11 -classpath target/classes com.jacdong.wrapper.JavaCPPTest</commandlineArgs> 
       </configuration>
     </execution>
     
     <execution>
                        <id>echoPath</id>
                        <phase>install</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>echo</executable>
                            <commandlineArgs>"sudo cp ${basedir}/target/classes/linux-x86_64/*.so /usr/lib/;sudo ldconfig"</commandlineArgs>

                        </configuration>
                    </execution>
    </executions>
   </plugin>
   <plugin>
    <artifactId>maven-clean-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
     <filesets>
      <fileset>
       <directory>src/main/java/com/jacdong/rtcm/wrapper/</directory>
       <includes>
        <include>JavaCPPTestTmp.java</include>
       </includes>
       <followSymlinks>false</followSymlinks>
      </fileset>
     </filesets>
    </configuration>
   </plugin>
  </plugins>
  <pluginManagement>
   <plugins>
    <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
    <plugin>
     <groupId>org.eclipse.m2e</groupId>
     <artifactId>lifecycle-mapping</artifactId>
     <version>1.0.0</version>
     <configuration>
      <lifecycleMappingMetadata>
       <pluginExecutions>
        <pluginExecution>
         <pluginExecutionFilter>
          <groupId>org.bytedeco</groupId>
          <artifactId>javacpp</artifactId>
          <versionRange>
           [1.5.3,)
          </versionRange>
          <goals>
           <goal>build</goal>
          </goals>
         </pluginExecutionFilter>
         <action>
          <ignore></ignore>
         </action>
        </pluginExecution>
        <pluginExecution>
         <pluginExecutionFilter>
          <groupId>
           org.codehaus.mojo
          </groupId>
          <artifactId>
           exec-maven-plugin
          </artifactId>
          <versionRange>
           [1.1,)
          </versionRange>
          <goals>
           <goal>exec</goal>
          </goals>
         </pluginExecutionFilter>
         <action>
          <ignore></ignore>
         </action>
        </pluginExecution>
       </pluginExecutions>
      </lifecycleMappingMetadata>
     </configuration>
    </plugin>
   </plugins>
  </pluginManagement>
 </build>
</project>

配置文件当中需要主要的几点


<!--第一次生成javacpp 文件的时候,skip 一定要设置为false ,后期对于生成的类进行了处理之后,为了避免每次运行都重启,可以把skip 设置为true-->
<configuration>
    <skip>false</skip>
    <outputDirectory>${project.build.sourceDirectory}</outputDirectory>                                       <classOrPackageName>com.jacdong.preset.* </classOrPackageName>
</configuration>

<!--commandlineArgs 一定不要换行-->
<configuration>
 <executable>java</executable>   
    <commandlineArgs>-jar ./libs/javacpp-1.5.jar -Xcompiler -g -Xcompiler -ggdb -nodelete -d target/classes/linux-x86_64 -Xcompiler -I${env.JAVA_HOME}/include -Xcompiler -I${env.JAVA_HOME}/include/linux -Xcompiler -fvisibility=default -Xcompiler -rdynamic -Xcompiler -std=c++11 -classpath target/classes com.jacdong.wrapper.SingleStationIono</commandlineArgs> 
</configuration>

定义自己的map 类


@Properties(target = "com.jacdong.wrapper.JavaCPPTestTmp",
value = {@Platform(include = {"javacpp.h"},
        compiler = "cpp11")})
public class JavacppMapper implements InfoMapper{

 public void map(InfoMap infoMap) {
        infoMap.put(new Info().cppNames("_stdcall").skip())
        .put();
}
}

注意点

在利用javacpp 的时候,经常会出现找不到so 文件的情况,这个时候,大家可以把so 文件copy 到/usr/lib 目录下,然后更改相应的权限。

到此这篇关于Java利用JavaCPP调用算法示例的文章就介绍到这了,更多相关Java JavaCPP调用内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯