java使用JNA调取C++动态库
JNA(Java Native Access):提供一组Java工具类用于在运行期间动态访问系统本地库(native library:如Window的dll)而不需要编写任何Native/JNI代码。开发人员只要在一个java接口中描述目标native library的函数与结构,JNA将自动实现Java接口到native function的映射。
常用的指针对应:
Native Type | Java Type |
void ** | PointerByReference |
void* | Pointer |
char** | PointerByReference |
char& | PointerByReference |
char* | Pointer |
int& | IntByReference |
int* | IntByReference |
程序示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | public class TestSdkUtil { private static final Logger logger = LoggerFactory.getLogger(TestSdkUtil. class ); protected static SdkUtil.CallBackDll CallBackDll = null ; private static String stereo3d_server = "/lib/libstereo3d_server.so" ; /** * 接口类,用来和动态库建立联系,并声明动态库中的方法 */ public interface CallBackDll extends Library { SdkUtil.CallBackDll instance = (SdkUtil.CallBackDll) Native.loadLibrary(stereo3d_server, SdkUtil.CallBackDll. class ); int getHandle_server(PointerByReference phandle, String severIndexAddr); int getHandleChar_server(PointerByReference phandle, String severIndexAddr); int Save_GunCameraObject(Pointer phandle, resolution_param_t resolution, 3d_pos_param_t pos, euler_angle_param_t angle, double fovy, Pointer filename, IntByReference id); int Modify_GunCameraObject(Pointer phandle, IntByReference id, resolution_param_t resolution, 3d_pos_param_t pos, euler_angle_param_t angle, double fovy, Pointer filename); int destroy_server(PointerByReference phandle); } public static void getInstance() { try { logger.info( "==========读取libstereo3d_server.so文件开始==========" ); CallBackDll = SdkUtil.CallBackDll.instance; logger.info( "==========读取libstereo3d_server.so文件开完成=========" ); } catch (Exception e) { logger.info( "==========读取libstereo3d_server.so文件开失败=========" + e); } } public static ResultVO Save_GunCameraObject(PointerByReference phandle, resolution_param_t resolution, 3d_pos_param_t pos, euler_angle_param_t angle, double fovy, String filename, IntByReference id) { ResultVO vo = new ResultVO(); try { if (CallBackDll == null ) { getInstance(); } Pointer fn = new Memory( 1024 ); int result = CallBackDll.Save_GunCameraObject(phandle.getValue(), resolution, pos, angle, fovy, fn, id); vo.setId(id.getValue()); vo.setFilename(fn.getString( 0 )); vo.setCode(result); } catch (Exception e) { } return vo; } public static ResultVO Modify_GunCameraObject(PointerByReference phandle, int id, resolution_param_t resolution, 3d_pos_param_t pos, euler_angle_param_t angle, double fovy, String filename) { ResultVO vo = new ResultVO(); try { if (CallBackDll == null ) { getInstance(); } IntByReference idRef = new IntByReference(); idRef.setValue(id); Pointer fn = new Memory( 1024 ); int result = CallBackDll.Modify_GunCameraObject(phandle.getValue(), idRef, resolution, pos, angle, fovy, fn); vo.setId(idRef.getValue()); vo.setFilename(fn.getString( 0 )); vo.setCode(result); } catch (Exception e) { } return vo; } } /** * 对象: 图像分辨率参数 */ public class resolution_param_t extends Structure { public int width; public int height; @Override protected List getFieldOrder() { return Arrays.asList( "width" , "height" ); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?