package uk.co.mmscomputing.application.imageviewer; import java.io.*; import java.awt.*; import java.awt.image.*; import javax.swing.*; import uk.co.mmscomputing.device.scanner.*; import uk.co.mmscomputing.device.twain.*; import uk.co.mmscomputing.device.sane.*; public class ScannerTab extends ImageTab implements ScannerListener{ static private int no=1; public ScannerTab(java.util.Properties properties){ super(properties); } protected void setButtonPanel(JPanel gui){ super.setButtonPanel(gui); Scanner scanner=Scanner.getDevice(); // get a device and set GUI panel up if(scanner!=null){ gui.add(scanner.getScanGUI()); scanner.addListener(this); } } public void update(ScannerIOMetadata.Type type, final ScannerIOMetadata metadata){ if(type.equals(ScannerIOMetadata.ACQUIRED)){ // acquired BufferedImage final BufferedImage image=metadata.getImage();// make reference copy here to avoid race condition new Thread(){ public void run(){ try{ addImage("scan_"+(no++),image); }catch(Exception e){ System.out.println("9\b"+getClass().getName()+".update:\n\t"+e); System.err.println(getClass().getName()+".update:\n\t"+e); e.printStackTrace(); } } }.start(); }else if(type.equals(ScannerIOMetadata.FILE)){ // acquired image as file (twain only for the time being) final File file=metadata.getFile(); // make reference copy here to avoid race condition new Thread(){ public void run(){ try{ open(file.getPath()); }catch(Exception e){ System.out.println("9\b"+getClass().getName()+".update:\n\t"+e); System.err.println(getClass().getName()+".update:\n\t"+e); e.printStackTrace(); }finally{ if(!file.delete()){ System.out.println("9\b"+getClass().getName()+".update:\n\tCould not delete: "+file.getPath()); System.err.println(getClass().getName()+".update:\n\tCould not delete: "+file.getPath()); } } } }.start(); }else if(type.equals(ScannerIOMetadata.NEGOTIATE)){ negotiate(metadata); }else if(type.equals(ScannerIOMetadata.STATECHANGE)){ System.out.println("Scanner State "+metadata.getStateStr()); if(metadata instanceof TwainIOMetadata){ // TWAIN only ! if(metadata.isState(TwainConstants.STATE_TRANSFERREADY)){ // state = 6 TwainSource source = ((TwainIOMetadata)metadata).getSource(); try{ TwainImageInfo imageInfo=new TwainImageInfo(source); imageInfo.get(); System.out.println(imageInfo.toString()); }catch(Exception e){ System.out.println("3\b"+getClass().getName()+".update:\n\tCannot retrieve image information.\n\t"+e); } try{ TwainImageLayout imageLayout=new TwainImageLayout(source); imageLayout.get(); System.out.println(imageLayout.toString()); }catch(Exception e){ System.out.println("3\b"+getClass().getName()+".update:\n\tCannot retrieve image layout.\n\t"+e); } }else if(metadata.isState(TwainConstants.STATE_TRANSFERRING)){ // state = 7 // In state 4: supportTwainExtImageInfo=source.getCapability(TwainConstants.ICAP_EXTIMAGEINFO).booleanValue(); TwainSource source = ((TwainIOMetadata)metadata).getSource(); try{ int[] tweis=new int[0x1240-0x1200]; for(int i=0;i