java使用JNA调取C++动态库

java使用JNA调取C++动态库

JNA(Java Native Access):提供一组Java工具类用于在运行期间动态访问系统本地库(native library:如Window的dll)而不需要编写任何Native/JNI代码。开发人员只要在一个java接口中描述目标native library的函数与结构,JNA将自动实现Java接口到native function的映射。

常用的指针对应:

Native TypeJava Typevoid **PointerByReferencevoid*Pointerchar**PointerByReferencechar&PointerByReferencechar*Pointerint&IntByReferenceint*IntByReference

程序示例:

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");    }}}

  

推荐阅读