好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#调用Java方法实例详解

c#可以直接引用c++的dll和转换java写好的程序。最近由于工作原因接触这方面比较多,根据实际需求,我们通过一个具体例子把一个java方法转换成可以由c#直接调用的dll

c#调用c++

c#调用c++的例子网上很多,以一个c++的具体方法为例。

c++代码

?

// 获取一帧图像数据

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

// 结果数据缓存的上限

#define mv_sc_max_result_size (1024*16)

// 输出帧的信息

typedef struct _mv_sc_image_out_info_

{

unsigned short nwidth; // 图像宽

unsigned short nheight; // 图像高

unsigned int nframenum; // 帧号

unsigned int nframelen; // 当前帧数据大小

unsigned int ntimestamphigh; // 时间戳高32位

unsigned int ntimestamplow; // 时间戳低32位

unsigned int nresulttype; // 输出的消息类型

// 根据消息类型对应不同的结构体

unsigned char chresult[mv_sc_max_result_size];

unsigned int nreserved[8]; // 保留

}mv_sc_image_out_info

c#代码

/// <summary>

/// 获得相机所拍照片

/// </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; // 帧号

public int nframelen; // 当前帧数据大小

public int ntimestamphigh; // 时间戳高32位

public int ntimestamplow; // 时间戳低32位

public int nresulttype; // 输出的消息类型

// 根据消息类型对应不同的结构体

[marshalas(unmanagedtype.byvaltstr, sizeconst = 1024 * 16)]

public mv_sc_result_bcr chresult;

[marshalas(unmanagedtype.byvalarray, sizeconst = 8)]

public int [] nreserved;

}

这样我们把这个dll放在程序根目录下,就能实现dll方法的调用。

c#调用java方法

ikvm.net是一个针对mono和微软.net框架的java实现,其设计目的是在.net平台上运行java程序。它包含了以下的组件:

用.net实现的java虚拟机,java类库的.net实现。

致力于在java和.net之间交互的工具。

程序需求

我们有一个java写好的demo,传的参数是用gzip进行压缩传到服务器的,代码如下:

?

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;

}

}

这个代码我用c#改写了,用httpwebrequest的方式传到服务器,服务器那边gzip解压不了,查了原因是因为java与c#的byte类型值范围不同,我们有两种解决思路,一种是将这个java做成webservice挂在服务器上,c#再去调用,第二种就是将这个方法编译成可由c#直接调用的dll,由于这个方法功能比较单一,我们选取了后者。

环境配置

ikvm.net 下载后解压得到bin文件夹中的数据,用于jar包转换和基础dll。

ikvm.openjdk.classlibrary.dll用于c#程序接入。

下载地址:https://yunpan.cn/cbhts5fxsie9v 访问密码 0847。

将ikvm.net的bin文件夹的地址添加到环境变量。

计算机右键属性--高级系统设置--高级--环境变量--在系统变量中找到path--将bin文件夹的地址添加进去,
在cmd中输入ikvmc 有帮助文档说明环境配置成功。

bin文件夹下的ikvm.openjdk.core.dll,ikvm.runtime.dll,ikvm.runtime.jni.dll和ikvm.openjdk.classlibrary.dll为公共dll,所有转换程序都需引用

转换步骤

1.确定引用关系:

该demo的结构如下:

demo.jar 依赖于 commons-httpclient-3.1.jar 和 gson-2.4.jar

commons-httpclient-3.1.jar 依赖于 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,语法如下:

ikvmc jar包物理路径。

win7系统默认生成在c:\users\administrator 这个文件夹下

commons-httpclient-3.1.dll 生成语法如下:

ikvmc commons-httpclient-3.1.jar -r:commons-logging-1.1.3.dll -r:commons-codec-1.6.dll

我们将demo打包的名字为javaapi.demo 这样生成的 javaapi.dll 生成语法如下:

ikvmc javaapi.demo.jar -r:commons-httpclient-3.1.dll -r:gson-2.4.dll

上面的文件都是相对应的物理路径,然后将所有生成的dll添加到c#项目中引用,包括之前的公共dll,引用到项目中所有引用的dll如图:

这样就可以直接在程序中使用这个java方法了

demo.demo.dopostclient(js, url);

第一个demo java程序中的package名。

第二个demo java程序中的class名。

以上所述是小编给大家介绍的c#调用java方法实例详解的全部叙述,希望对大家有所帮助,如果大家想了解更多内容敬请关注服务器之家网站!

原文链接:http://www.cnblogs.com/kaoleba/p/5653414.html

dy("nrwz");

查看更多关于C#调用Java方法实例详解的详细内容...

  阅读:49次