Aplicação para escanear documentos utilizando mmscomputing

0 respostas
H

Descrição do Projeto: Uma aplicação em Java que seja capaz, de digitalizar um documento e que me retorne uma BufferedImage, para que eu manipule como eu bem quiser.

Bibliotecas Utilizadas:

IJ.jar | mmsc.jar | uk.co.mmscomputing.device.twain.jar

Sistema Operacional:

Windows 7 Professional ~ 64 bits

Impressora/Scanner Utilizada(o):

Epson Stylus CX7300

Código de Exemplo :
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package paint;

/*
 A scanner plugin for ImageJ which uses the free mm's computing java library 
 (available at http://www.mms-computing.co.uk/)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.*/
import ij.IJ;
import ij.ImagePlus;
import ij.plugin.PlugIn;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import uk.co.mmscomputing.device.scanner.Scanner;
import uk.co.mmscomputing.device.scanner.ScannerDevice;
import uk.co.mmscomputing.device.scanner.ScannerIOException;
import uk.co.mmscomputing.device.scanner.ScannerIOMetadata;
import uk.co.mmscomputing.device.scanner.ScannerListener;
import javax.swing.WindowConstants;
import uk.co.mmscomputing.device.twain.TwainScanner;

public class Scan extends JFrame implements PlugIn, ScannerListener {

    private static final long serialVersionUID = 1L;
    private JPanel jContentPane = null;
    private JButton jButton = null;
    private JButton jButton1 = null;
    private Scanner scanner;

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

    public void run(String arg0) {

        new Scan().setVisible(true);
    }

    /**
     * This is the default constructor
     */
    public Scan() {
        super();
        initialize();
        try {
            scanner = (TwainScanner) Scanner.getDevice();
            scanner.addListener(this);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * This method initializes this
     *
     * @return void
     */
    private void initialize() {
        this.setSize(300, 120);
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        this.setResizable(false);
        this.setContentPane(getJContentPane());
        this.setTitle("Scan");
        this.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent e) {

            }
        });
    }

    /**
     * This method initializes jContentPane
     *
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(null);
            jContentPane.add(getJButton(), null);
            jContentPane.add(getJButton1(), null);
        }
        return jContentPane;
    }

    /**
     * This method initializes jButton
     *
     * @return javax.swing.JButton
     */
    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;
    }

    /**
     * This method initializes jButton1
     *
     * @return javax.swing.JButton
     */
    private JButton getJButton1() {
        if (jButton1 == null) {
            jButton1 = new JButton();
            jButton1.setBounds(new Rectangle(160, 16, 131, 42));
            jButton1.setText("Scan");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {

                    getScan();

                }
            });
        }
        return jButton1;
    }

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

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

    }

    /* Get the scan! */
    public void getScan() {

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

        }

    }

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

            ImagePlus imp = new ImagePlus("Scan", metadata.getImage());
            imp.show();
            metadata.setImage(null);
            try {
                new uk.co.mmscomputing.concurrent.Semaphore(0, true).tryAcquire(2000, null);
            } catch (InterruptedException e) {
                IJ.error(e.getMessage());
                // e.printStackTrace();
            }

        } else if (type.equals(ScannerIOMetadata.NEGOTIATE)) {
            ScannerDevice device = metadata.getDevice();
            try {
                device.setResolution(300);
            } catch (ScannerIOException e) {
                IJ.error(e.getMessage());
            }
            /*
             * More options if necessary! try{
             * device.setShowUserInterface(true);
             * 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)) {

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

        } else if (metadata.isFinished()){
            
        }

    }

} // @jve:decl-index=0:visual-constraint="95,130"

Prints da Aplicação em Funcionamento:

[img]http://i234.photobucket.com/albums/ee194/Alfrad/TelaPrincipal.png[/img] [img]http://i234.photobucket.com/albums/ee194/Alfrad/SelectSource.png[/img]

Descrição do Problema: Então, como vocês estão vendo, no lugar onde era pra selecionar o Scanner, não existe nada/nenhuma
opção pra ser selecionada, e se for clicado em Scan sem selecionar nenhuma Impressora/Scanner, me é retornado as seguintes exceções.

Exceções de Execução:
JarLib.load: Successfully loaded library [jar:file:/C:/Users/NexTI-Wolves/Desktop/twain/uk.co.mmscomputing.device.twain.jar!/uk/co/mmscomputing/device/twain/win64/jtwain.dll] from jar file location
uk.co.mmscomputing.device.twain.TwainIOException: uk.co.mmscomputing.device.twain.TwainScanner.setException:
    Failed during call to twain source.
	cc=No Data Source
	rc=1
uk.co.mmscomputing.device.twain.TwainFailureException: Failed during call to twain source.
	cc=No Data Source
	rc=1
	at uk.co.mmscomputing.device.twain.TwainSourceManager.call(TwainSourceManager.java:27)
	at uk.co.mmscomputing.device.twain.TwainIdentity.open(TwainIdentity.java:70)
	at uk.co.mmscomputing.device.twain.TwainSource.open(TwainSource.java:99)
	at uk.co.mmscomputing.device.twain.TwainSourceManager.openSource(TwainSourceManager.java:93)
	at uk.co.mmscomputing.device.twain.jtwain.cbexecute(jtwain.java:234)
	at uk.co.mmscomputing.device.twain.jtwain.ncallSourceManager(Native Method)
	at uk.co.mmscomputing.device.twain.jtwain.callSourceManager(jtwain.java:104)
	at uk.co.mmscomputing.device.twain.TwainSourceManager.call(TwainSourceManager.java:24)
	at uk.co.mmscomputing.device.twain.TwainIdentity.userSelect(TwainIdentity.java:66)
	at uk.co.mmscomputing.device.twain.TwainSourceManager.selectSource(TwainSourceManager.java:46)
	at uk.co.mmscomputing.device.twain.jtwain.cbexecute(jtwain.java:219)
	at uk.co.mmscomputing.device.twain.jtwain.nstart(Native Method)
	at uk.co.mmscomputing.device.twain.jtwain.access$200(jtwain.java:12)
	at uk.co.mmscomputing.device.twain.jtwain$1.run(jtwain.java:338)

Observações: Por favor galera, no que vocês puderem me ajudar eu serei muito grato, cada um, com minimo de ajuda que seja estará me ajudando pra caramba, isto é uma pequena parte do meu TCC. Abraços.

Criado 29 de agosto de 2014
Respostas 0
Participantes 1