Unable To acquire image from scanner in Linux SLED 11 using JSane

Unable To acquire image from scanner in Linux using JSane due to java.lang.outofboundsexception


devices[0].setOptionValue("tl-x", "0");
devices[0].setOptionValue("tl-y", "0");
devices[0].setOptionValue("br-x", "5.25");
devices[0].setOptionValue("br-y", "3.5");
                                    
Image image = devices[0].acquireImage();

It Says That 0 is out of bounds when it is supposed to chose the first scanner device
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;


import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.*;

//import com.asprise.util.jtwain.JTwainConstants;
//import com.asprise.util.jtwain.JTwainException;
//import com.asprise.util.jtwain.Source;
//import com.asprise.util.jtwain.SourceManager;
import com.asprise.util.jsane.JSane;
import com.asprise.util.jsane.JSaneDevice;
import com.mysql.jdbc.Statement;

public class Sign extends JFrame
{
	// Variables declaration
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JTextField jTextField1;
	private JComboBox jComboBox1;
	private JButton jButton1;
	private JButton jButton2;
	private JPanel contentPane;
	private JFrame frame;
	// End of variables declaration
	Connection connection = null;
	String connectionURL = null;
	String userName = null;
	String password = null;
	PreparedStatement psmnt = null;
	ResultSet rs = null;
	ArrayList rowArray = new ArrayList();  
	public Sign() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException
	{
		super();
		initializeComponent();
		this.setVisible(true);
	}


	private void initializeComponent() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException
	{
		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jTextField1 = new JTextField();
		jComboBox1 = new JComboBox();
		jButton1 = new JButton();
		jButton2 = new JButton();
		contentPane = (JPanel)this.getContentPane();

		final ResultSet rs = getProductCode();
		jLabel1.setText("Product Id:");
		jLabel2.setText("Account No.:");
		jTextField1.setText("");
		jTextField1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				try {
					jTextField1_actionPerformed(e);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}

		});

		jComboBox1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				try {
					jComboBox1_actionPerformed(e);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}

			}

		});
		updateJComboBox(rs);
		
		jButton1.setText("OK");
		jButton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				jButton1.setEnabled(false);
				jButton1_actionPerformed(e);
				JSane sane = null;
				//Source source = null;
				boolean src=false;
				try
				{
					int branchCode=0;	
					int prodId = Integer.parseInt(rowArray.get(jComboBox1.getSelectedIndex()).toString());
					//System.out.println("in ok prodId: "+prodId);
					int accountNo = Integer.parseInt(jTextField1.getText());
					//System.out.println("in ok accno: "+accountNo);
					Class.forName("com.mysql.jdbc.Driver").newInstance();
					connection = (Connection) DriverManager.getConnection(connectionURL, userName, password);
					PreparedStatement p = connection.prepareStatement("Select bd_branch_no from branch_details");
					System.out.println("1");
					ResultSet rs1 = p.executeQuery();
					while(rs1.next()){
						branchCode = rs1.getInt(1);
						//System.out.println("branchCode: "+branchCode);
					}
					String query = "Select uid_customer_id from unique_id_details  where uid_branch_code=? and uid_product_id=? and uid_account_id=?";
					PreparedStatement ps = (PreparedStatement) connection.prepareStatement(query);
					ps.setInt(1,branchCode);
					ps.setInt(2,prodId);
					ps.setInt(3,accountNo);
					ResultSet rs = ps.executeQuery();
					boolean found = rs.next();
					if(found == false){
						JOptionPane.showMessageDialog(frame, "Record not present in Uniqueiddetails table");	
					}else{
						//source = SourceManager.instance().getDefaultSource();
						
						sane = new JSane("172.16.10.78");
						JSaneDevice[] devices = sane.getAllDevices(); 
						System.out.println("2");
						if(devices == null) {
							JOptionPane.showMessageDialog(frame, "There is no (default) source on the system!");	
							//System.out.println("There is no (default) source on the system!");
							//return;
						}
						System.out.println("No Of Devices: "+devices);
						//System.out.println("Before Open");
						//source.open();
						//source.setRegion(0, 0,5.25,3.5);
						//source.setUIEnabled(false);
						devices[0].setOptionValue("tl-x", "0");
						devices[0].setOptionValue("tl-y", "0");
						devices[0].setOptionValue("br-x", "5.25");
						devices[0].setOptionValue("br-y", "3.5");
						src=true;
						Image image = devices[0].acquireImage();
						//source.saveLastAcquiredImageIntoFile("abc.jpg");
						
						//File image1 = new File("abc.jpg");
						//FileInputStream fis = new FileInputStream(image1);
						FileInputStream fis = new FileInputStream(image.toString());
						//image1.delete();
						//int length = (int)image1.length();
						int length = (int)(image.toString()).length();
						new ImageDisplayer("CitizenCredit Co-Operative Bank Ltd",image);
						int reply = JOptionPane.showConfirmDialog(null, "Image acquire sucessfully. Do you want to save?", "Confirmation", JOptionPane.YES_NO_OPTION);
						if (reply == JOptionPane.NO_OPTION){
							//source.close();
							devices[0].close();
						}else{
							psmnt = (PreparedStatement) connection.prepareStatement("Insert into sign (s_branch_code,s_product_id,s_account_no,s_image,s_maker_date_time_stamp)Values(?,?,?,?,?)");
							psmnt.setInt(1, branchCode);
							psmnt.setInt(2, prodId);
							psmnt.setInt(3, accountNo);
							psmnt.setBinaryStream(4, (InputStream)fis,length);
							java.sql.Date date = getCurrentJavaSqlDate();
							psmnt.setDate(5,(Date) date);
							int s = psmnt.executeUpdate();		
							//source.close();
							devices[0].close();
							connection.close();
						}
					}
					jButton1.setEnabled(true);
				}
				catch(Exception e1)
				{
					String message = e1.getMessage();
					JOptionPane.showMessageDialog(frame, message);
					jButton1.setEnabled(true);
//					try 
//					{
//						if(src==true)
//						{
//							//source.close();
//							//devices[0].clo
//						}
//					} 
//					catch (JTwainException e2) 
//					{
//
//						e2.printStackTrace();
//					}
					e1.printStackTrace();
					try 
					{
						connection.close();
					} 
					catch (SQLException e2) 
					{
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}
				}
			}

		});

		jButton2.setText("CANCEL");
		jButton2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				jButton2_actionPerformed(e);
			}

		});

		contentPane.setLayout(null);
		addComponent(contentPane, jLabel1, 20,21,56,16);
		addComponent(contentPane, jLabel2, 8,57,69,18);
		addComponent(contentPane, jTextField1, 76,53,100,22);
		addComponent(contentPane, jComboBox1, 76,18,100,22);
		addComponent(contentPane, jButton1, 13,126,83,28);
		addComponent(contentPane, jButton2, 126,126,83,28);

		this.setTitle("Signature Scan");
		this.setLocation(new Point(252, 56));
		this.setSize(new Dimension(229, 189));
	}

	/** Add Component Without a Layout Manager (Absolute Positioning) */
	private void addComponent(Container container,Component c,int x,int y,int width,int height)
	{
		c.setBounds(x,y,width,height);
		container.add(c);
	}


	private void jTextField1_actionPerformed(ActionEvent e) throws SQLException
	{
		System.out.println("\njTextField1_actionPerformed(ActionEvent e) called.");


	}

	private void jComboBox1_actionPerformed(ActionEvent e) throws SQLException
	{
		System.out.println("\njComboBox1_actionPerformed(ActionEvent e) called.");

		Object o = jComboBox1.getSelectedItem();

		System.out.println(">>" + ((o==null)? "null" : o.toString()) + " is selected.");
		System.out.println("rs code: "+rowArray.get(jComboBox1.getSelectedIndex()).toString());

	}

	private void jButton1_actionPerformed(ActionEvent e)
	{
		System.out.println("\njButton1_actionPerformed(ActionEvent e) called.");
	}

	private void jButton2_actionPerformed(ActionEvent e)
	{
		System.out.println("\njButton2_actionPerformed(ActionEvent e) called.");
		System.exit(0);

	}

	public ResultSet getProductCode() throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException {
		String dbName =null;
		String ipAddress = null;
		String portNo=null;
		/*String userName = null;
        String password = null;*/

		try{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			File proFile = new File("/usr/connect.properties");
			if(proFile.exists()){
				Properties pro = new Properties();
				FileInputStream in = new FileInputStream(proFile);
				pro.load(in);
				System.out.println("All key are given: " + pro.keySet());
				dbName = pro.getProperty("DB_NAME");
				ipAddress = pro.getProperty("DB_IP");
				portNo=pro.getProperty("DB_PORT");
				userName = pro.getProperty("DB_USERNAME");
				password = pro.getProperty("DB_PWD");
				connectionURL = "jdbc:mysql://"+ipAddress+":"+portNo+"/"+dbName;
				System.out.println(" "+"jdbc:mysql://"+ipAddress+":"+portNo+"/"+dbName);
			}
			else{
				JOptionPane.showMessageDialog(frame, "Properties File Not Found");	
			}
			connection = (Connection) DriverManager.getConnection(connectionURL, userName, password);
			Statement stmt= (Statement) connection.createStatement();
			rs=stmt.executeQuery("Select cgl_code, cgl_desc from code_gl where cgl_code in('0','101','102','103','104','201')");
			// connection.close();
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		return rs;
	}
	public void updateJComboBox(ResultSet rs) throws SQLException {
		while(rs.next())
		{	rowArray.add(rs.getString(1));

		jComboBox1.addItem(rs.getString("cgl_desc"));

		}
	}


	public static java.sql.Date getCurrentJavaSqlDate() {
		java.util.Date today = new java.util.Date();
		return new java.sql.Date(today.getTime());
	}
	public static BufferedImage convertToGrayscale(BufferedImage source) { 
		BufferedImageOp op = new ColorConvertOp(
				ColorSpace.getInstance(ColorSpace.CS_GRAY), null); 
		return op.filter(source, null);
	}
	//============================= Testing ================================//
	//=                                                                    =//
	//= The following main method is just for testing this class you built.=//
	//= After testing,you may simply delete it.                            =//
	//======================================================================//
	public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException
	{
		JFrame.setDefaultLookAndFeelDecorated(true);
		
		JDialog.setDefaultLookAndFeelDecorated(true);
		try
		{
			
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
			
		}
		catch (Exception ex)
		{
			System.out.println("Failed loading L&F: ");
			System.out.println(ex);
		}
		new Sign();
	}
	//= End of Testing =


}

Open in new window

CCBRONETAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
objectsConnect With a Mentor Commented:
jsane does not talk directly to the scanner, it communicates with the backend saned process which needs to be running

http://josi.objects.com.au/sane.html
0
 
objectsCommented:
that suggests it is not finding any scsanners
is saned running and finding scanners?
0
 
CCBRONETAuthor Commented:
????
0
 
CCBRONETAuthor Commented:
LOL
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.