2012-05-22 36 views
3

我正在使用此mmscomputing庫作爲java applet來掃描圖像或文檔。 使用鞦韆,awt我創建了一個掃描按鈕,它是通過調用mmscomputing jar的scanner.acquire()方法來獲取掃描器。uk.co.mmscomputing twain scanner

然後將該掃描圖像放入jpanel中進行顯示。

問題是,當我開始我的小程序,打第一次我的掃描button..scanning工作fine..Twain指出它進入爲:3,4,5,6,7,5,4,3

那麼第二次,又打我的掃描按鈕.. 吐溫指出它進入有:3,4,5,4,3

它不會成圖像傳送就緒和轉移狀態,因此不進下面的代碼IF迴路

if(type.equals(ScannerIOMetadata.ACQUIRED))

所以我無法看到新的掃描圖像到我的jpanel第二次...

然後第三次,點擊我的掃描按鈕..再次它工作正常..進入所有狀態。 所以我的意思是,爲了交替轉動或重新啓動Java小程序..它的作品。 會是什麼問題..?

我想,每當我點擊掃描按鈕它應該讓我一個新的形象進入Jpanel ..但它是做替代時間。 我可以強制明確設置或更改第二和第七狀態的第二個狀態。 或者是否有第二次發生一些雙源初始化問題? ,因爲重新啓動小程序每次都做得很好......或者某種方式每次點擊掃描按鈕時重新初始化小程序對象..因爲它會覺得我每次點擊掃描按鈕時都會重新啓動applet ...

我沒有它..

下面是示例代碼:

import uk.co.mmscomputing.device.twain.TwainConstants; 
import uk.co.mmscomputing.device.twain.TwainIOMetadata; 
import uk.co.mmscomputing.device.twain.TwainSource; 
import uk.co.mmscomputing.device.twain.TwainSourceManager; 


public class XXCrop extends JApplet implements PlugIn, ScannerListener 
{ 

    private JToolBar jtoolbar = new JToolBar("Toolbar", JToolBar.HORIZONTAL); 
    ImagePanel ipanel; 
    Image im =null; 
    BufferedImage imageforCrop; 
    ImagePlus imp=null; 
    int imageWidth; 
    int imageHeight; 
    private static final long serialVersionUID = 1L; 
    Container content = null; 
    private JPanel jContentPane = null; 
    private JButton jButton = null; 
    private JButton jButton1 = null; 
    JCheckBox clipBox = null; 
    JPanel crpdpanel=null; 
    JPanel cpanel=null; 
    private Scanner scanner=null; 
    private TwainSource ts ; 
    private boolean  is20; 






    ImagePanel imagePanel,imagePanel2 ; 




    public static void main(String[] args) { 
     new XXCrop().setVisible(true); 
    } 

    public void run(String arg0) { 

     new XXCrop().setVisible(false); 
     repaint(); 
    } 

    /** 
    * This is the default constructor 
    */ 
    public XXCrop() { 
     super(); 
     init(); 
     try { 
      scanner = Scanner.getDevice(); 

      if(scanner!=null) 
      { 
      scanner.addListener(this); 
      } 

     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 


    } 

    /** 
    * This method initializes this 
    * 
    * @return void 
    */ 
    public void init() 
    { 
     this.setSize(1200, 600); 
     this.setLayout(null); 
     //this.revalidate(); 
     this.setContentPane(getJContentPane()); 
    } 



    private JToolBar getJToolBar() 
    { 
     jtoolbar.add(getJButton1()); 
     jtoolbar.add(getJButton()); 


     jtoolbar.setName("My Toolbar"); 
     jtoolbar.addSeparator(); 
     Rectangle r=new Rectangle(0, 0,1024, 30); 
     jtoolbar.setBounds(r); 
     return jtoolbar; 
    } 

    private JPanel getJContentPane() 
    { 
     if (jContentPane == null) 
     { 
      jContentPane = new JPanel(); 
      jContentPane.setLayout(null); 
      jContentPane.add(getJToolBar()); 
     } 
     return jContentPane; 
    } 


    private JButton getJButton() { 
     if (jButton == null) { 
      jButton = new JButton(); 
      jButton.setBounds(new Rectangle(4, 16, 131, 42)); 
      jButton.setText("Select Device"); 
      jButton.addActionListener(new java.awt.event.ActionListener() { 
       public void actionPerformed(java.awt.event.ActionEvent e) { 
        if (scanner.isBusy() == false) { 
         selectDevice(); 
        } 

       } 
      }); 
     } 
     return jButton; 
    } 


    /* Select the twain source! */ 
    public void selectDevice() { 

     try { 
      scanner.select(); 
     } catch (ScannerIOException e1) { 
      IJ.error(e1.toString()); 
     } 

    } 

    private JButton getJButton1() 
    { 
     if (jButton1 == null) { 
      jButton1 = new JButton(); 
      jButton1.setBounds(new Rectangle(35,0, 30, 30)); 
      jButton1.setText("Scan"); 
      jButton1.addActionListener(new java.awt.event.ActionListener() { 
       public void actionPerformed(java.awt.event.ActionEvent e) 
       {//jContentPane.remove(crpdpanel); 
        //jContentPane.removeAll(); 
       //jContentPane.repaint(); 
       //jContentPane.revalidate(); 

        getScan(); 
       } 
      }); 
     } 
     return jButton1; 
    } 

    public void getScan() 
    { 

     try 
     { 


     //scanner = Scanner.getDevice(); 
     //scanner.addListener(this); 
      scanner.acquire(); 
     } 
     catch (ScannerIOException e1) 
     { 
      IJ.showMessage("Access denied! \nTwain dialog maybe already opened!"); 
      e1.printStackTrace(); 
     } 
    } 


    public Image getImage() 
    { 
     Image image = imp.getImage(); 
     return image; 
    } 


    /*Image cimg; 

    public Image getCimg() 
     { 
      return cimg; 
     }*/ 

    public void update(ScannerIOMetadata.Type type, ScannerIOMetadata metadata) { 



     if (type.equals(ScannerIOMetadata.ACQUIRED)) 
     { 

      //imagePanel.revalidate(); 
      //imagePanel.repaint(); 
      //imagePanel.invalidate(); 

      //jContentPane.remove(ipanel); 
      //ipanel.repaint(); 


    if(imp!=null) 
    { 
     jContentPane.remove(ipanel); 
     jContentPane.remove(cpanel); 
     jContentPane.remove(crpdpanel); 
    } 


      imp = new ImagePlus("Scan", metadata.getImage()); 
      //imp.show(); 
      im = imp.getImage(); 


      //imagePanel = new ImagePanel(im,imageWidth,imageHeight); 
      imagePanel = new ImagePanel(im); 
      imagePanel.updateUI(); 

      imagePanel.repaint(); 
      imagePanel.revalidate(); 

      ClipMover mover = new ClipMover(imagePanel); 
      imagePanel.addMouseListener(mover); 
      imagePanel.addMouseMotionListener(mover); 

       ipanel = imagePanel.getPanel(); 

      ipanel.setBorder(new LineBorder(Color.blue,1)); 
      ipanel.setBorder(BorderFactory.createTitledBorder("Scanned Image")); 
      ipanel.setBounds(0, 30,600, 600); 
      ipanel.repaint(); 
      ipanel.revalidate(); 
      ipanel.updateUI(); 
      jContentPane.add(ipanel); 
      jContentPane.getRootPane().revalidate(); 
      jContentPane.updateUI(); 
      //jContentPane.repaint(); 

      // cimg=imagePanel.getCimg(); 

      // ImagePanel cpanel = (ImagePanel) imagePanel.getUIPanel(); 
      /* 
      cpanel.setBounds(700, 30,600, 800); 
      jContentPane.add(imagePanel.getUIPanel()); 
      */ 
       cpanel = imagePanel.getUIPanel(); 
      cpanel.setBounds(700, 30,300, 150); 
      cpanel.repaint(); 
      cpanel.setBorder(new LineBorder(Color.blue,1)); 
      cpanel.setBorder(BorderFactory.createTitledBorder("Cropping Image")); 
      jContentPane.add(cpanel); 


      jContentPane.repaint(); 
      jContentPane.revalidate(); 




      metadata.setImage(null); 
      try { 
       new uk.co.mmscomputing.concurrent.Semaphore(0, true).tryAcquire(2000, null); 
      } catch (InterruptedException e) { 
       IJ.error(e.getMessage()); 

      } 

     } 




     else if (type.equals(ScannerIOMetadata.NEGOTIATE)) { 
      ScannerDevice device = metadata.getDevice(); 
      try { 
       device.setResolution(100); 
      } catch (ScannerIOException e) { 
       IJ.error(e.getMessage()); 
      } 

       try{ 
       device.setShowUserInterface(false); 
      // device.setShowProgressBar(true); 
     // device.setRegionOfInterest(0,0,210.0,300.0); 
       device.setResolution(100); }catch(Exception e){ 
       e.printStackTrace(); } 

     } 


     else if (type.equals(ScannerIOMetadata.STATECHANGE)) { 
      System.out.println("Scanner State "+metadata.getStateStr()); 
      System.out.println("Scanner State "+metadata.getState()); 
      //switch (metadata.ACQUIRED){}; 
      ts = ((TwainIOMetadata)metadata).getSource(); 
     //ts.setCancel(false); 
      //ts.getState() 
      //TwainConstants.STATE_TRANSFERREADY 
      ((TwainIOMetadata)metadata).setState(6); 
      if ((metadata.getLastState() == 3) && (metadata.getState() == 4)){} 
      // IJ.error(metadata.getStateStr()); 
     } 

     else if (type.equals(ScannerIOMetadata.EXCEPTION)) { 
      IJ.error(metadata.getException().toString()); 
     } 


    } 


    public void stop(){             // execute before System.exit 
      if(scanner!=null){             // make sure user waits for scanner to finish! 
       scanner.waitToExit(); 

        ts.setCancel(true); 

        try { 
         scanner.setCancel(true); 
        } catch (ScannerIOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
      } 
      } 

回答

3

我不是專家,但是當ScannerIOMetadata.STATECHANGE你不應該檢查掃描是否已經完成?

如果是的話,你應該再次初始化掃描儀。

類似的東西:

if (metadata.isFinished()) 
{ 
    twainScanner = (TwainScanner) Scanner.getDevice(); 
}