如何通过jni4net,在Java应用中调用C#接口

  1. 下载Dynamic .NET TWAIN

  2. 下载jni4net,学习里面的代码实例

  3. 在环境变量中设置好JAVA_HOME和C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe

  4. 解压JavaTwain,在dll目录中运行工程,编译出JavaTwain.dll

  5. 从jni4net中拷贝bin和lib到项目中

  6. 运行generateProxies.cmd

  7. 运行run.cmd

    02162028_n2W8.png

参考原文:Java TWAIN with Dynamic .NET TWAIN and jni4net

示例解析

C#层

在C#工程中添加引用DynamicDotNetTWAIN.dll

02162029_FLzk.png

初始化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);
 }


转载于:https://my.oschina.net/yushulx/blog/286390

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值