概述
项目需要用到 重采样算法,JAVA 没有现成的,只能通过 JNA 调用 C++ 的 DLL 实现,JNA中,它提供了一个动态的C语言编写的转发器,可以自动实现Java和C的数据类型映射。不再需要编写C动态链接库。
实现需求
根据 一个数组数据,算法根据数组生成多个相邻、相似的点。
案例:
原始数组:
[1,2,3,4,5,6,5,4,3,2,1]
生成10个:
[0.9975707933099867,2.0827107433705034,3.242373563930609,4.227786236390621,5.5680645300875815,5.6819573969217645,4.3206979326831965,3.3497212671714256,2.1727162369435766,1.115611206113079]
生成20个:
[1.0006736067597888,1.6136225699097613,2.0834240705596923,2.6000134251334908,3.2387063724897835,3.8026902204918467,4.232982835368568,4.801594298327119,5.562876758327512,6.0005892018875,5.686344251045411,4.934418414307205,4.317107510714494,3.885808545074988,3.3535095539415303,2.7104608356964937,2.167467521772395,1.7063151122165208,1.1234001156030098,0.45134020733353886]
实现效果
实践
gredle 或 maven
版本有很多,我用比较旧的,若有不适可以用新的
implementation 'net.java.dev.jna:jna:4.0.0'
<dependency><groupId>net.java.dev.jna</groupId><artifactId>jna</artifactId><version>4.0.0</version>
</dependency>
NasalResamplingService
创建接口并声明方法,方法名与DLL中方法名对应
public interface NasalResamplingService extends Library {/*** @FileName NasalResamplingService.java* @description 重采样算法* @author lanys* @date 2024-4-15 17:14:04* @param srcArray 原始数组* @param desArray 生成后的数组(C++赋值)* @param desArraySize 生成后数组长度* @param srcArraySize 原始数据长度*/void myMatlabResample(double[] srcArray, double[] desArray, double desArraySize, double srcArraySize);}
DllUtils
@Slf4j
public class DllUtils {private NasalResamplingService instance;/** DLL 文件地址 */private final static String path = "文件地址";public DllUtils() {instance = (NasalResamplingService) Native.loadLibrary(path, NasalResamplingService.class);}public double[] resampling(double[] array, Integer extent) {TimeInterval timer = DateUtil.timer();if (array != null && array.length > 0) {double[] desArray = null;if (extent != null) {desArray = new double[extent];} else {desArray = new double[2000];}// 参数一:原始数据数组// 参数二:重采样数组// 参数三:重采样数据长度,固定2000// 参数四:原始数据个数(长度)instance.myMatlabResample(array, desArray, desArray.length, array.length);log.info("[DLL工具-重采样算法]DLL所需时间:{} 毫秒", timer.interval());return desArray;}return null;}/*** @FileName DllUtils.java* @description 关闭DLL释放内存* @author lanys* @date 2024-4-16 11:11:59*/public void dispose() {try {log.info("[DLL工具-释放DLL内存]");// 关闭DLL,防止内存泄漏NativeLibrary instance = NativeLibrary.getInstance(path);instance.dispose();}catch (Exception e){log.error("[DLL工具-释放内存]数据异常:",e);}}
}
Controller(自己写吧,很简单)
DllUtils dllUtils = new DllUtils();
double[] desArray = dllUtils.resampling(resamplingDTO.getSourceArray(), resamplingDTO.getGeneratingLength());
dllUtils.dispose();