我有以下示例类使用uk.co.mmscomputing.device.scanner.Scanner类连接到twain.dll驱动程序来扫描页面:
public class ScannerManager implements ScannerListener{
static ScannerManager app;
Scanner scanner;
String fileName;
public ScannerManager() throws ScannerIOException{
scanner=Scanner.getDevice();
scanner.addListener(this);
scanner.acquire();
}
public void update(ScannerIOMetadata.Type type, ScannerIOMetadata metadata){
if(type.equals(ScannerIOMetadata.ACQUIRED)){
BufferedImage image=metadata.getImage();
System.out.println("ACQUIRED!");
String file = UIMessages.showInputMessage("Enter file name");
try{
ImageIO.write(image, "png", new File(Constants.getPathBusta() + File.separatorChar + file));
fileName = Constants.getPathBusta() + File.separatorChar + file;
}catch(Exception e){
e.printStackTrace();
}
}else if(type.equals(ScannerIOMetadata.NEGOTIATE)){
ScannerDevice device=metadata.getDevice();
try{
device.setShowUserInterface(true);
device.setShowProgressBar(true);
device.setResolution(100);
}catch(Exception e){
e.printStackTrace();
}
}else if(type.equals(ScannerIOMetadata.STATECHANGE)){
System.err.println(metadata.getStateStr());
if(metadata.isFinished()){
System.exit(0);
}
}else if(type.equals(ScannerIOMetadata.EXCEPTION)){
metadata.getException().printStackTrace();
}
}
public static void main(String[] argv){
try{
app=new ScannerManager( );
}catch(Exception e){
e.printStackTrace();
}
}
}
在通过main方法运行时它非常有效,但我需要从 Swing jButton click事件中调用它: 令人惊讶的是,它的作用并不相同:
buttonAcquire.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
ScannerManager scanner = new ScannerManager();
} catch (ScannerIOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
defaultCursor();
}
}
});
条件:
if(type.equals(ScannerIOMetadata.ACQUIRED))
永远不会遇到,因此从Swing Thread执行时似乎没有正确拦截事件。你能给我一些提示吗?我真的很困惑,我不知道会出现什么问题......
答案 0 :(得分:1)
尝试使Swing类本身监听ScannerListener事件。换句话说,将代码移动到Swing UI部分,看它是否有效。