uk.co.mmscomputing twain scanner

5k Views Asked by At

I am using this mmscomputing library as java applet to scan an image or document. Using swings,awt i created one scan button which is acquiring scanner by calling scanner.acquire() method of mmscomputing jar..

and then placing that scanned image into jpanel for displaying.

Problem is, first time when i start my applet and hitting my scan button..scanning works fine..Twain states it goes into are: 3,4,5,6,7,5,4,3

then second time,hitting my scan button again .. Twain states it goes into are: 3,4,5,4,3

It's not going into image transfer ready and transferring state and thus not into below CODE IF loop

if (type.equals(ScannerIOMetadata.ACQUIRED))

so i am not able to see the new scanned image into my jpanel second time...

then third time, hitting my scan button .. again it works fine.. getting into all states. So i mean, For alternatively turns or restarting the java applet again ..it works. what would be the issue.. ?

I want, every time when i hit scan button it should get me a new image into Jpanel.. but it's doing alternative times. can i forcefully explicitly set or change twain states to come into 6th and 7th states.. or is there some twain source initialisation problem occurs second time? because restarting applet is doing fine every time.. or some way to reinitialise applet objects everytime on clicking scan button..as it would feel like I am restarting applet everytime on clicking scan button...

I am not getting it..

Below is the 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();
                    }
            }
          }
1

There are 1 best solutions below

0
On

I'm not an expert, but when ScannerIOMetadata.STATECHANGE shouldn't you check if the scanning is complete?

And if it is you should initialize the scanner again.

Something like that:

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