Uk.co.mmscomputing twain scanner

I use this mmscomputing library as a java applet to scan an image or document. Using a swing, I created one scan button that acquires a scanner by calling the scanner.acquire () method for the mmscomputing jar ..

and then putting that scanned image in jpanel for display.

The problem is that when I launch my applet and press the scan button. scanning works fine. It includes the following: 3,4,5,6,7,5,4,3

and then a second time by pressing the scan button again. Twain claims it includes: 3,4,5,4,3

It does not go into an image transfer state and does not transmit a state and, therefore, does not fall below the CODE IF cycle

if (type.equals (ScannerIOMetadata.ACQUIRED))

so I can not see the new scanned image in my jpanel a second time ...

and then a third time by clicking on the scan button. it works fine again .. gets into all states. So I mean: for an alternative, the Java applet starts up again or again..it works. what will be the problem ..?

I want, every time I press the scan button, it should get me a new image in Jpanel .. but it does alternative times. can I decisively set or change two states in order to enter the 6th and 7th states. or is there some kind of twain source initialization issue a second time? because the applet restarts perfectly every time .. or some way to reinitialize the applet objects every time I click the scan button .. it would look like I restart the applet every time I click the scan button ...

I do not understand.

The following is sample code:

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
source share
1 answer

I am not an expert, but when ScannerIOMetadata.STATECHANGEyou do not need to check whether the scan is complete?

And if you need to initialize the scanner again.

Something like that:

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

All Articles