使用JNA框架比用原來JNI要方便多了,對於DLL不需要額外的包裝,直接就能夠使用:
1.JNA框架
a.定義:JNA(Java Native Access)框架是一個開源的Java框架,是SUN公司主導開發的,建立在經典的JNI的基礎之上的一個框架
b.作用:JNA提供一組Java工具類用於在運行期動態訪問系統本地庫(native library:如Window的dll)而不需要編寫任何Native/JNI代碼。開發人員只要在一個java接口中描述目標native library的函數與結構,JNA將自動實現Java接口到native function的映射。
c.導入maven依賴
<dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna</artifactId> <version>5.5.0</version> </dependency>
2.調用科大訊飛windows api的方法
public class XunfeiSpeech {
public interface MscLibrary extends Library {
// DLL文件默認路徑為項目根目錄,若DLL文件存放在項目外,請使用絕對路徑
MscLibrary INSTANCE = Native.load("D:\\app\\yuyinhecheng\\bin\\msc_x64", MscLibrary.class);
int MSPLogin(String username, String password, String param);
int MSPLogout();
String QTTSSessionBegin(String params, IntByReference errorCode);
int QTTSTextPut(String sessionID, String textString, int textLen, String params);
Pointer QTTSAudioGet(String sessionID, IntByReference audioLen, IntByReference synthStatus, IntByReference errorCode);
int QTTSSessionEnd(String sessionID, String hints);
}
...
然后在Java代碼中就可以使用了:
int loginCode = MscLibrary.INSTANCE.MSPLogin(null, null, login_params);
