I have the following sample class which connects to twain.dll driver using the uk.co.mmscomputing.device.scanner.Scanner class to scan a page:
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();
}
}
}
It works perfectly when run through the main method, however I need it to be invoked from a Swing jButton click event: Suprisingly, it does not work the same:
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();
}
}
});
The condition:
if(type.equals(ScannerIOMetadata.ACQUIRED))
is never met so it seems that events are not being intercepted correctly when executing from a Swing Thread. Can you give me any hint ? I'm really puzzled by it and I don't know what could be the problem.....
Try making the Swing class itself listening to the ScannerListener event. In other words move the code into the Swing UI part and see if it works.