如何通過jni4net,在Java應用中調用C#接口
Java開發者如果想要調用Windows的接口,需要使用JNI來創建一個橋接的DLL。jni4net為Java虛擬機(JVM)和.Net運行時(CLR)之間提供了橋梁。
下載jni4net,學習里面的代碼實例
</li>在環境變量中設置好JAVA_HOME和C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe
</li>解壓JavaTwain,在dll目錄中運行工程,編譯出JavaTwain.dll
</li>從jni4net中拷貝bin和lib到項目中
</li>運行generateProxies.cmd
</li>運行run.cmd

示例解析
C#層
在C#工程中添加引用DynamicDotNetTWAIN.dll

初始化Dynamic .NET TWAIN組件
public DotNetScanner()
{
// initialize TWAIN Component
try
{
dynamicDotNetTwain = new Dynamsoft.DotNet.TWAIN.DynamicDotNetTwain();
dynamicDotNetTwain.OnPostAllTransfers += new Dynamsoft.DotNet.TWAIN.Delegate.OnPostAllTransfersHandler(this.dynamicDotNetTwain_OnPostAllTransfers);
dynamicDotNetTwain.MaxImagesInBuffer = 64;
dynamicDotNetTwain.IfAppendImage = true;
dynamicDotNetTwain.IfThrowException = true;
dynamicDotNetTwain.IfShowUI = false;
dynamicDotNetTwain.IfThrowException = true;
dynamicDotNetTwain.ScanInNewProcess = true;
}
catch
{
MessageBox.Show(dynamicDotNetTwain.ErrorString);
}
}創建JVM和CLR交互的接口
public interface IJavaProxy
{
bool AcquireImage(int iIndex);
String[] GetSources();
bool RegisterListener(INativeProxy proxy);
void CloseSource();
}
public interface INativeProxy
{
bool Notify(String message, String value);
}掃描圖片,通知Java層加載
public bool AcquireImage(int iIndex)
{
try
{
//dynamicDotNetTwain.CloseSource();
bool success = dynamicDotNetTwain.SelectSourceByIndex(Convert.ToInt16(iIndex));
dynamicDotNetTwain.OpenSource();
dynamicDotNetTwain.AcquireImage();
}
catch (Dynamsoft.DotNet.TWAIN.TwainException exp)
{
String errorstr = "";
errorstr += "Error " + exp.Code + "\r\n" + "Description: " + exp.Message + "\r\nPosition: " + exp.TargetSite + "\r\nHelp: " + exp.HelpLink + "\r\n";
MessageBox.Show(errorstr);
}
catch (Exception exp)
{
String errorstr = "";
errorstr += "ErrorMessage: " + exp.Message + "\r\n";
MessageBox.Show(errorstr);
}
return true;
}
private void dynamicDotNetTwain_OnPostAllTransfers()
{
//MessageBox.Show("dynamicDotNetTwain_OnPostAllTransfers");
if (dynamicDotNetTwain.MaxImagesInBuffer < 1)
{
return;
}
Image img = dynamicDotNetTwain.GetImage(0);
img = resizeImage(img, new Size(480, 640));
img.Save("twain.png");
if (listener != null)
{
listener.Notify("data ready", "twain.png");
}
}Java層
加載JavaTwain.j4n.dll
private void initTWAIN() {
try {
Bridge.init();
Bridge.LoadAndRegisterAssemblyFrom(new java.io.File("JavaTwain.j4n.dll"));
}
catch (Exception e) {
e.printStackTrace();
}
mScanner = new DotNetScanner();
mScanner.RegisterListener(this);
}使用Swing來顯示UI
public ScanDocuments() {
super(new BorderLayout());
initTWAIN();
//Create a file chooser
mFileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
".png", "png");
mFileChooser.setFileFilter(filter);
mLoad = new JButton("Load");
mLoad.addActionListener(this);
mScan = new JButton("Scan");
mScan.addActionListener(this);
// get sources
mSources = mScanner.GetSources();
if (mSources != null) {
mSourceList = new JComboBox(mSources);
}
else {
mSourceList = new JComboBox(new String[]{"N/A"});
}
mSourceList.setSelectedIndex(0);
// button panel
JPanel buttonPanel = new JPanel();
buttonPanel.add(mSourceList);
buttonPanel.add(mScan);
buttonPanel.add(mLoad);
add(buttonPanel, BorderLayout.PAGE_START);
// image panel
JPanel imageViewer = new JPanel();
mImage = new JLabel();
mImage.setSize(480, 640);
imageViewer.add(mImage);
add(imageViewer, BorderLayout.CENTER);
}來自: http://www.codepool.biz/ocr-barcode-twain/twain-sdk/java-twain-with-dynamic-net-twain-and-jni4net...
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!