ikvm java转换成dll_利用IKVM.NET将Java jar包转换成可供C#调用的dll文件
C#可以直接引用C++的DLL和轉(zhuǎn)換JAVA寫(xiě)好的程序。最近由于工作原因接觸這方面比較多,根據(jù)實(shí)際需求,我們通過(guò)一個(gè)具體例子把一個(gè)JAVA方法轉(zhuǎn)換成可以由C#直接調(diào)用的DLL
C#調(diào)用c++
C#調(diào)用C++的例子網(wǎng)上很多,以一個(gè)C++的具體方法為例。
C++代碼
// 獲取一幀圖像數(shù)據(jù)
MVSMARTCAMCTRL_API int __stdcall MV_SC_GetOneFrame(IN void* handle,
IN OUT unsigned char *pData ,
IN unsigned int nDataSize,
IN OUT MV_SC_IMAGE_OUT_INFO* pstImageInfo);
// 結(jié)果數(shù)據(jù)緩存的上限
#define MV_SC_MAX_RESULT_SIZE (1024*16)
// 輸出幀的信息
typedef struct _MV_SC_IMAGE_OUT_INFO_
{
unsigned short nWidth; // 圖像寬
unsigned short nHeight; // 圖像高
unsigned int nFrameNum; // 幀號(hào)
unsigned int nFrameLen; // 當(dāng)前幀數(shù)據(jù)大小
unsigned int nTimeStampHigh; // 時(shí)間戳高32位
unsigned int nTimeStampLow; // 時(shí)間戳低32位
unsigned int nResultType; // 輸出的消息類(lèi)型
// 根據(jù)消息類(lèi)型對(duì)應(yīng)不同的結(jié)構(gòu)體
unsigned char chResult[MV_SC_MAX_RESULT_SIZE];
unsigned int nReserved[8]; // 保留
}MV_SC_IMAGE_OUT_INFO
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 獲取一幀圖像數(shù)據(jù)
MVSMARTCAMCTRL_APIint__stdcallMV_SC_GetOneFrame(INvoid*handle,
INOUTunsignedchar*pData,
INunsignedintnDataSize,
INOUTMV_SC_IMAGE_OUT_INFO*pstImageInfo);
// 結(jié)果數(shù)據(jù)緩存的上限
#define MV_SC_MAX_RESULT_SIZE?????? (1024*16)
// 輸出幀的信息
typedefstruct_MV_SC_IMAGE_OUT_INFO_
{
unsignedshortnWidth;// 圖像寬
unsignedshortnHeight;// 圖像高
unsignedintnFrameNum;// 幀號(hào)
unsignedintnFrameLen;// 當(dāng)前幀數(shù)據(jù)大小
unsignedintnTimeStampHigh;// 時(shí)間戳高32位
unsignedintnTimeStampLow;// 時(shí)間戳低32位
unsignedintnResultType;// 輸出的消息類(lèi)型
// 根據(jù)消息類(lèi)型對(duì)應(yīng)不同的結(jié)構(gòu)體
unsignedcharchResult[MV_SC_MAX_RESULT_SIZE];
unsignedintnReserved[8];// 保留
}MV_SC_IMAGE_OUT_INFO
C#代碼
/// <summary>
/// 獲得相機(jī)所拍照片
/// </summary>
/// <param name="handle"></param>
/// <returns></returns>
[DllImport("MvSmartCamCtrl.dll")]
public static extern int MV_SC_GetOneFrame(IntPtr handle, Byte[] pData, int nDataSize, out MV_SC_IMAGE_OUT_INFO pstDevInfo);
// 輸出幀的信息
[StructLayout(LayoutKind.Sequential)]
public struct MV_SC_IMAGE_OUT_INFO
{
public short nWidth; // 圖像寬
public short nHeight; // 圖像高
public int nFrameNum; // 幀號(hào)
public int nFrameLen; // 當(dāng)前幀數(shù)據(jù)大小
public int nTimeStampHigh; // 時(shí)間戳高32位
public int nTimeStampLow; // 時(shí)間戳低32位
public int nResultType; // 輸出的消息類(lèi)型
// 根據(jù)消息類(lèi)型對(duì)應(yīng)不同的結(jié)構(gòu)體
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024 * 16)]
public MV_SC_RESULT_BCR chResult;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public int[] nReserved;
}
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
/// <summary>
/// 獲得相機(jī)所拍照片
/// </summary>
/// <param name="handle"></param>
/// <returns></returns>
[DllImport("MvSmartCamCtrl.dll")]
publicstaticexternintMV_SC_GetOneFrame(IntPtrhandle,Byte[]pData,intnDataSize,outMV_SC_IMAGE_OUT_INFOpstDevInfo);
// 輸出幀的信息
[StructLayout(LayoutKind.Sequential)]
publicstructMV_SC_IMAGE_OUT_INFO
{
publicshortnWidth;// 圖像寬
publicshortnHeight;// 圖像高
publicintnFrameNum;// 幀號(hào)
publicintnFrameLen;// 當(dāng)前幀數(shù)據(jù)大小
publicintnTimeStampHigh;// 時(shí)間戳高32位
publicintnTimeStampLow;// 時(shí)間戳低32位
publicintnResultType;// 輸出的消息類(lèi)型
// 根據(jù)消息類(lèi)型對(duì)應(yīng)不同的結(jié)構(gòu)體
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=1024*16)]
publicMV_SC_RESULT_BCRchResult;
[MarshalAs(UnmanagedType.ByValArray,SizeConst=8)]
publicint[]nReserved;
}
這樣我們把這個(gè)DLL放在程序根目錄下,就能實(shí)現(xiàn)DLL方法的調(diào)用。
C#調(diào)用JAVA方法
IKVM.NET是一個(gè)針對(duì)Mono和微軟.net框架的java實(shí)現(xiàn),其設(shè)計(jì)目的是在.NET平臺(tái)上運(yùn)行java程序。它包含了以下的組件:用.NET實(shí)現(xiàn)的java虛擬機(jī),java類(lèi)庫(kù)的.NET實(shí)現(xiàn)。
致力于在java和.NET之間交互的工具。
程序需求
我們有一個(gè)JAVA寫(xiě)好的Demo,傳的參數(shù)是用Gzip進(jìn)行壓縮傳到服務(wù)器的,代碼如下:
package Demo;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import com.google.gson.Gson;
public class Demo
{
public static String doPostClient(String json, String url)
{
HttpClient httpClient = new HttpClient();
String rval = "";
PostMethod postMethod = new PostMethod(url);
try
{
Gson gson = new Gson();
InputStream in = new ByteArrayInputStream(objectToByte(json));
postMethod.setRequestBody(in);
HttpClientParams params = new HttpClientParams();
httpClient.setParams(params);
httpClient.executeMethod(postMethod);
byte[] b = postMethod.getResponseBody();
String rtnData = (String) byteToObject(b);
rval = gson.toJson(rtnData);
} catch (Exception e)
{
rval="erro:"+e.getMessage();
} finally
{
postMethod.releaseConnection();
}
return rval;
}
public static byte[] objectToByte(java.lang.Object obj)
{
byte[] bytes = null;
ObjectOutputStream oo = null;
try
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(obj.toString().getBytes("utf-8"));
gzip.close();
bytes = out.toByteArray();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
if (oo != null)
{
try
{
oo.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
return bytes;
}
private static java.lang.Object byteToObject(byte[] bytes)
{
String obj = "";
ObjectInputStream oi = null;
try
{
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
GZIPInputStream gzipi = new GZIPInputStream(bi);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gzipi, "UTF-8"));
String line;
while ((line = bufferedReader.readLine()) != null)
{
obj+=line;
}
} catch (Exception e)
{
e.printStackTrace();
} finally
{
if (oi != null)
{
try
{
oi.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
return obj;
}
}
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
packageDemo;
importjava.io.BufferedReader;
importjava.io.ByteArrayInputStream;
importjava.io.ByteArrayOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.ObjectInputStream;
importjava.io.ObjectOutputStream;
importjava.util.zip.GZIPInputStream;
importjava.util.zip.GZIPOutputStream;
importorg.apache.commons.httpclient.HttpClient;
importorg.apache.commons.httpclient.methods.PostMethod;
importorg.apache.commons.httpclient.params.HttpClientParams;
importcom.google.gson.Gson;
publicclassDemo
{
publicstaticStringdoPostClient(Stringjson,Stringurl)
{
HttpClienthttpClient=newHttpClient();
Stringrval="";
PostMethodpostMethod=newPostMethod(url);
try
{
Gsongson=newGson();
InputStreamin=newByteArrayInputStream(objectToByte(json));
postMethod.setRequestBody(in);
HttpClientParamsparams=newHttpClientParams();
httpClient.setParams(params);
httpClient.executeMethod(postMethod);
byte[]b=postMethod.getResponseBody();
StringrtnData=(String)byteToObject(b);
rval=gson.toJson(rtnData);
}catch(Exceptione)
{
rval="erro:"+e.getMessage();
}finally
{
postMethod.releaseConnection();
}
returnrval;
}
publicstaticbyte[]objectToByte(java.lang.Objectobj)
{
byte[]bytes=null;
ObjectOutputStreamoo=null;
try
{
ByteArrayOutputStreamout=newByteArrayOutputStream();
GZIPOutputStreamgzip=newGZIPOutputStream(out);
gzip.write(obj.toString().getBytes("utf-8"));
gzip.close();
bytes=out.toByteArray();
}catch(Exceptione)
{
e.printStackTrace();
}finally
{
if(oo!=null)
{
try
{
oo.close();
}catch(IOExceptione)
{
e.printStackTrace();
}
}
}
returnbytes;
}
privatestaticjava.lang.ObjectbyteToObject(byte[]bytes)
{
Stringobj="";
ObjectInputStreamoi=null;
try
{
ByteArrayInputStreambi=newByteArrayInputStream(bytes);
GZIPInputStreamgzipi=newGZIPInputStream(bi);
BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(gzipi,"UTF-8"));
Stringline;
while((line=bufferedReader.readLine())!=null)
{
obj+=line;
}
}catch(Exceptione)
{
e.printStackTrace();
}finally
{
if(oi!=null)
{
try
{
oi.close();
}catch(IOExceptione)
{
e.printStackTrace();
}
}
}
returnobj;
}
}
這個(gè)代碼我用C#改寫(xiě)了,用HttpWebRequest的方式傳到服務(wù)器,服務(wù)器那邊Gzip解壓不了,查了原因是因?yàn)镴ava與C#的Byte類(lèi)型值范圍不同,我們有兩種解決思路,一種是將這個(gè)JAVA做成webservice掛在服務(wù)器上,c#再去調(diào)用,第二種就是將這個(gè)方法編譯成可由C#直接調(diào)用的DLL,由于這個(gè)方法功能比較單一,我們選取了后者。
環(huán)境配置
IKVM.NET 下載后解壓得到BIN文件夾中的數(shù)據(jù),用于JAR包轉(zhuǎn)換和基礎(chǔ)DLL。
IKVM.OpenJDK.ClassLibrary.dll用于C#程序接入。
下載地址:https://yunpan.cn/cBHTS5fXsIe9v?訪問(wèn)密碼 0847。
將IKVM.NET的BIN文件夾的地址添加到環(huán)境變量。
計(jì)算機(jī)右鍵屬性--高級(jí)系統(tǒng)設(shè)置--高級(jí)--環(huán)境變量--在系統(tǒng)變量中找到PATH--將BIN文件夾的地址添加進(jìn)去,
在CMD中輸入ikvmc 有幫助文檔說(shuō)明環(huán)境配置成功。
Bin文件夾下的IKVM.OpenJDK.Core.dll,IKVM.Runtime.dll,IKVM.Runtime.JNI.dll和IKVM.OpenJDK.ClassLibrary.dll為公共DLL,所有轉(zhuǎn)換程序都需引用
轉(zhuǎn)換步驟
1.確定引用關(guān)系:
該Demo的結(jié)構(gòu)如下:
Demo.jar 依賴(lài)于 commons-httpclient-3.1.jar 和 gson-2.4.jar
commons-httpclient-3.1.jar 依賴(lài)于 commons-logging-1.1.3.jar 和 commons-codec-1.6.jar
我們先將gson-2.4.jar,commons-logging-1.1.3.jar,commons-codec-1.6.jar 生成DLL,語(yǔ)法如下:
ikvmc JAR包物理路徑。
win7系統(tǒng)默認(rèn)生成在C:\Users\Administrator 這個(gè)文件夾下
commons-httpclient-3.1.dll 生成語(yǔ)法如下:
ikvmc commons-httpclient-3.1.jar -r:commons-logging-1.1.3.dll -r:commons-codec-1.6.dll
我們將Demo打包的名字為JavaApi.Demo 這樣生成的 JavaApi.dll 生成語(yǔ)法如下:
ikvmc JavaApi.Demo.jar -r:commons-httpclient-3.1.dll -r:gson-2.4.dll
上面的文件都是相對(duì)應(yīng)的物理路徑,然后將所有生成的DLL添加到C#項(xiàng)目中引用,包括之前的公共DLL,引用到項(xiàng)目中所有引用的DLL如圖:
這樣就可以直接在程序中使用這個(gè)java方法了
Demo.Demo.doPostClient(js, url);
第一個(gè)Demo java程序中的package名。
第二個(gè)Demo java程序中的class名。
總結(jié)
以上是生活随笔為你收集整理的ikvm java转换成dll_利用IKVM.NET将Java jar包转换成可供C#调用的dll文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: amvu mysql_mysql--数据
- 下一篇: c#求三角形面积周长公式_此题求三角形的