import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.sql.*;
class MyCanvas extends Canvas
{
Image img;
public MyCanvas(Image img)
{
this.img = img;
}
public void paint (Graphics g)
{
if (img != null)
{
g.drawImage(img, 0, 0, 1000, 1000, this);
}
}
public void setImage (Image img)
{
this.img = img;
}
}
public class ImagesTest implements ActionListener, WindowListener
{
Frame fr = new Frame ("CBIRS");
Label Label1 = new Label(" ");
Button Button1 = new Button("OPEN IMAGE");
Button Button2 = new Button("FIND IMAGE");
Image Image1,img1,img2;
MyCanvas Canvas1;
public String d;
public FileDialog fd = new FileDialog(fr,"Open", FileDialog.LOAD);
int iw1, ih1;
int iw2, ih2;
int pixels1[];
int pixels2[];
public double[] hist1 = new double[256];
public double[] hist2 = new double[256];
public double[] dist = new double[5];
public int y;
double distance = 0;
double distance1 = 0;
int j=0;
void initialize ()
{
fr.setSize(1000,1000);
fr.setLocation(300,300);
fr.setBackground(Color.black);
fr.setLayout(new FlowLayout());
fr.add(Label1);
fr.add(Button1);
fr.add(Button2);
fr.addWindowListener(this);
Button1.addActionListener(this);
Button2.addActionListener(this);
Canvas1 = new MyCanvas(null);
Canvas1.setSize(1000,1000);
fr.add(Canvas1);
fr.setTitle("CBIRS");
fr.show();
}
public void imageload ()
{
fd.show();
if(fd.getFile() == null)
{
Label1.setText("You have not chosen any image files yet");
}
else
{
d = (fd.getDirectory() + fd.getFile());
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image1 = toolkit.getImage(d);
Canvas1.setImage(Image1);
Canvas1.repaint();
}
}
public void QueryHist()
{
try
{
//getting pixels
d = (fd.getDirectory() + fd.getFile());
Toolkit toolkit = Toolkit.getDefaultToolkit();
img1 = toolkit.getImage(d);
iw1 = img1.getWidth(null);
ih1 = img1.getHeight(null);
pixels1 = new int[iw1*ih1];
PixelGrabber pg = new PixelGrabber(img1,0,0,iw1,ih1,pixels1,0,iw1);
pg.grabPixels();
//calculating hist array
for (int i=0; i<iw1*ih1; i++)
{
int p1 = pixels1[i];
int r1 = 0xff & (p1 >> 16);
int g1 = 0xff & (p1 >> 8);
int b1 = 0xff & (p1);
y = (int) (.33 * r1 + .56 * g1+ .11 * b1);
hist1[y]++;
}
}
catch (InterruptedException e)
{
System.out.println("interrupted");
return;
}
}
public void RefHist()
{
try
{
for(int j=0;j<5;j++)
{
int i=0;
String url="jdbc:odbc:ImageData";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection(url);
PreparedStatement psmnt1 = connection.prepareStatement("select Array from ImageOLE where
ID="+(j+1234)+" ");
ResultSet rs = psmnt1.executeQuery();
if(rs.next())
{
InputStream fis1;
FileOutputStream fos;
fis1 = rs.getBinaryStream("Array");
DataInputStream in = new DataInputStream(fis1);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String c=" ";
String a[]=new String[256];
while ((c = br.readLine()) != null)
{
a[i]=c;
i++;
}
for(i=0;i<256;i++)
{
hist2[i]=Double.parseDouble(a[i]);
}
fis1.close();
}
rs.close();
psmnt1.close();
connection.close();
}
Eudis();
}
catch(SQLException e)
{
System.out.println("SQL error");
return;
}
catch(IOException e)
{
System.out.println("IOException");
return;
}
catch(ClassNotFoundException e)
{
System.out.println("class not found");
return;
}
}
//calculating Eu distance
public void Eudis()
{
int j=0;
try
{
QueryHist();
for(int i=0;i<256;i++)
{
distance = distance + Math.pow((hist1[i] - hist2[i]), 2);
}
distance1= Math.sqrt(distance);
dist[j]=distance1;
//putting distance into database
String url="jdbc:odbc:ImageData";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection(url);
Statement smnt = c.createStatement();
int row=smnt.executeUpdate("update ImageOLE set Distance="+dist[j]+" where ID="+(j+1)+" ");
j=j+1;
c.commit();
}
catch(SQLException e)
{
System.out.println("SQL error");
return;
}
catch(ClassNotFoundException e)
{
System.out.println("class not found");
return;
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowActivated(WindowEvent e)
{
}
public void windowClosed(WindowEvent e)
{
}
public void windowDeactivated(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowOpened(WindowEvent e)
{
}
public void actionPerformed(ActionEvent event)
{
Button b = (Button)event.getSource();
if(b == Button1)
{
imageload();
}
if(b == Button2)
{
RefHist();
}
}
public static void main(String args[])
{
ImagesTest a = new ImagesTest();
a.initialize();
}
}
this my code and Exception is
H:\java>jdk1.6.0_13\bin\javac ImagesTest.java
Note: ImagesTest.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
H:\java>jdk1.6.0_13\bin\java ImagesTest
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:99
1)
at java.lang.Double.parseDouble(Double.java:510)
at ImagesTest.RefHist(ImagesTest.java:149)
at ImagesTest.actionPerformed(ImagesTest.java:246)
at java.awt.Button.processActionEvent(Button.java:392)
at java.awt.Button.processEvent(Button.java:360)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
plz tell me where's the problem?