jna学习网站
JNA Examples
1、返回String, pch.h头文件
// pch.h: 这是预编译标头文件。
// 下方列出的文件仅编译一次,提高了将来生成的生成性能。
// 这还将影响 IntelliSense 性能,包括代码完成和许多代码浏览功能。
// 但是,如果此处列出的文件中的任何一个在生成之间有更新,它们全部都将被重新编译。
// 请勿在此处添加要频繁更新的文件,这将使得性能优势无效。
#ifndef PCH_H
#define PCH_H
// 添加要在此处预编译的标头
#include "framework.h"
#ifdef __cplusplus
#define EXPORT extern "C" __declspec(dllexport)
#else
#define EXPORT __declspec(dllexport)
#endif
struct VideoAiReq {
//行人告警频率
int intervalP;
//停车告警频率
int intervalC;
//应急车道告警频率
int intervalR;
//视频流地址
char* videoUrl;
//结果请求地址
char* resultUrl;
//无人机id
char* deviceId;
//控制文件绝对路径 有则算法开始输出
char* startFile;
};
EXPORT char* startVideoAi(VideoAiReq videoAiReq);
#endif //PCH_H
pch.cpp代码
// pch.cpp: 与预编译标头对应的源文件
#include "pch.h"
#include "iostream"
char* startVideoAi(VideoAiReq videoAiReq) {
std::cout << videoAiReq.deviceId << std::endl;
std::cout << videoAiReq.startFile << std::endl;
std::cout << videoAiReq.intervalP << std::endl;
std::cout << videoAiReq.videoUrl << std::endl;
std::cout << videoAiReq.resultUrl << std::endl;
std::cout << videoAiReq.intervalC << std::endl;
std::cout << videoAiReq.intervalR << std::endl;
return videoAiReq.startFile;
}
// 当使用预编译的头时,需要使用此源文件,编译才能成功。
编译成动态库 videoai1
java代码如下
VideoAiLibrary.java 动态库
package com.pojo.util;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import java.nio.ByteBuffer;
/**
* JNA Wrapper for library <b>mycpp3</b><br>
* This file was autogenerated by <a href="http://jnaerator.googlecode.com/">JNAerator</a>,<br>
* a tool written by <a href="http://ochafik.com/">Olivier Chafik</a> that <a href="http://code.google.com/p/jnaerator/wiki/CreditsAndLicense">uses a few opensource projects.</a>.<br>
* For help, please visit <a href="http://nativelibs4java.googlecode.com/">NativeLibs4Java</a> , <a href="http://rococoa.dev.java.net/">Rococoa</a>, or <a href="http://jna.dev.java.net/">JNA</a>.
*/
public interface VideoAiLibrary extends Library {
public static final String JNA_LIBRARY_NAME = "videoai1";
public static final VideoAiLibrary INSTANCE = (VideoAiLibrary) Native.load(VideoAiLibrary.JNA_LIBRARY_NAME, VideoAiLibrary.class);
/**
* Original signature : <code>void startVideoAi(char*, char*, char*, char*, int)</code><br>
* <i>native declaration : C:\Users\Administrator\mycpp3.h:6</i>
*/
String startVideoAi(VideoAiReq videoAiReq);
}
传参实体类
package com.pojo.util;
import com.sun.jna.Structure;
/**
* 属性顺序和c++代码一致
*/
@Structure.FieldOrder({"intervalP", "intervalC", "intervalR", "videoUrl", "resultUrl", "deviceId", "startFile"})
public class VideoAiReq extends Structure {
/**
* 行人告警频率
*/
public int intervalP;
/**
* 停车告警频率
*/
public int intervalC;
/**
* 应急车道告警频率
*/
public int intervalR;
/**
* 视频流地址
*/
public String videoUrl;
/**
* 结果请求地址
*/
public String resultUrl;
/**
* 无人机id
*/
public String deviceId;
/**
* 控制文件绝对路径 有则算法开始输出
*/
public String startFile;
public int getIntervalP() {
return intervalP;
}
public void setIntervalP(int intervalP) {
this.intervalP = intervalP;
}
public int getIntervalC() {
return intervalC;
}
public void setIntervalC(int intervalC) {
this.intervalC = intervalC;
}
public int getIntervalR() {
return intervalR;
}
public void setIntervalR(int intervalR) {
this.intervalR = intervalR;
}
public String getVideoUrl() {
return videoUrl;
}
public void setVideoUrl(String videoUrl) {
this.videoUrl = videoUrl;
}
public String getResultUrl() {
return resultUrl;
}
public void setResultUrl(String resultUrl) {
this.resultUrl = resultUrl;
}
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public String getStartFile() {
return startFile;
}
public void setStartFile(String startFile) {
this.startFile = startFile;
}
public VideoAiReq() {
}
}
测试类 和测试结果
package com.pojo.util;
import lombok.SneakyThrows;
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class JnaTest {
@SneakyThrows
public static void main(String[] args) {
VideoAiReq videoAiReq = new VideoAiReq();
videoAiReq.setIntervalP(10);
videoAiReq.setIntervalC(20);
videoAiReq.setIntervalR(30);
videoAiReq.setVideoUrl("videoUrl");
videoAiReq.setStartFile("startFile");
videoAiReq.setDeviceId("deviceId");
videoAiReq.setResultUrl("resultUrl");
String ss = VideoAiLibrary.INSTANCE.startVideoAi(videoAiReq);
System.out.println("return===" + ss);
// Path newFile = Paths.get("D:\\home\\smart\\newfile.txt");
// if (!Files.exists(newFile)) {
// Files.createFile(newFile);
// }
// Files.deleteIfExists(newFile);
}
}