下载jni4net,学习里面的代码实例
在环境变量中设置好JAVA_HOME和C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe
解压JavaTwain,在dll目录中运行工程,编译出JavaTwain.dll
从jni4net中拷贝bin和lib到项目中
运行generateProxies.cmd
运行run.cmd
参考原文:Java TWAIN with Dynamic .NET TWAIN and jni4net
示例解析
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);
}