Java
sir,
I make a mysql database to insert images and their features ...
I attached my code to retrive the images by the given feature in the JFrame
But it cannot see the pictures in the Frame....
Plz help me...
package gui;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.FileDescriptor;
import java.io.FileReader;
import java.io.InputStream;
import javax.swing.JTextField;
import java.sql.*;
import javax.swing.JTextArea;
public class RetieveImage implements WindowListener, ActionListener {
Frame fr = new Frame("Retrieve Image");
// Label Label1 = new Label("Label1 ");
JTextField Field1 = new JTextField("", 20);
JTextArea Area1=new JTextArea("",5,5);
Label Label1 = new Label(" Enter Texture Feature");
JTextField val1= new JTextField("",20);
Label Label2 = new Label(" Enter Color Feature ");
JTextField val2=new JTextField("",20);
Label Label3 = new Label(" Enter Shape Feature ");
JTextField val3=new JTextField("",20);
Button Button1 = new Button("Browse Image");
Button Button2 = new Button("Search from database");
Canvas Canvas1 = new Canvas();
FileDialog fd = new FileDialog(fr, "Open", FileDialog.LOAD);
FileReader fs = new FileReader(FileDescriptor.in);
Image Image1;
void initialize() {
fr.setSize(500, 500);
fr.setLocation(300, 300);
fr.setBackground(Color.PINK);
fr.setLayout(new FlowLayout());
// fr.add(Label1);
fr.add(Field1);
fr.add(Button1);
fr.add(Area1);
fr.add(Label1);
fr.add(val1);
fr.add(Label2);
fr.add(val2);
fr.add(Label3);
fr.add(val3);
fr.add(Button2);
Canvas1.setSize(1000, 1000);
fr.add(Canvas1);
fr.addWindowListener(this);
Button1.addActionListener(this);
Button2.addActionListener(this);
fr.show();
}
void imageload() {
fd.show();
if (fd.getFile() == null) {
Field1.setText("You have not chosen any image files yet");
} else {
String d = (fd.getDirectory() + fd.getFile());
Field1.setText(fd.getDirectory() + fd.getFile());
Area1.setBackground(Color.RED);
}
}
public void paint(Graphics g) {
g.drawImage(Image1, 0, 0, 1000, 1000, Canvas1);
}
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();
} else {
System.out.println("hai");
Convertor();
//imageload();
}
}
public static void main(String args[]) {
RetieveImage a = new RetieveImage();
a.initialize();
}
private void Convertor() {
System.out.println("Retrieve Image from database!");
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "cbir";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+db, user, pass);
try {
String feature1 = val1.getText();
String feature2 = val2.getText();
String feature3 = val3.getText();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT type_blob FROM enchancement WHERE typ_string2 like'%"+feature2+"%'");
// ResultSet rs = null;
// Declare statement.
PreparedStatement psmnt = null;
// declare InputStream object to store binary stream of given image.
InputStream sImage;
try {
if(rs.next()) {
byte[] bytearray = new byte[1048576];
int size=0;
sImage = rs.getBinaryStream(1);
//response.reset();
//response.setContentType("image/jpeg");
while((size=sImage.read(bytearray))!= -1 ){
//response.getOutputStream().write(bytearray,0,size);
}
}
}
catch(Exception ex){
System.out.println("error :"+ex);
}
finally {
// close all the connections.
rs.close();
psmnt.close();
con.close();
}
System.out.println(rs);
} catch (SQLException s) {
System.out.println("Table is all ready exists!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
View Answers
April 8, 2010 at 11:03 AM
Hi Friend,
Creates a JLabel instance, initialize it and add to Frame. Then using the setIcon() method of JLabel class, display the retrieved image.
We have modified your converter() method.
private void Convertor() {
System.out.println("Retrieve Image from database!");
Connection con = null;
String url = "jdbc:
mysql://localhost:3306/";;
String db = "register";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+db, user, pass);
String feature1 = val1.getText();
String feature2 = val2.getText();
String feature3 = val3.getText();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT image FROM enhancement WHERE typ_string2 like'%"+feature2+"%'");
InputStream sImage;
byte[] bytes=new byte[1024];
if (rs.next()) {
bytes = rs.getBytes("image");
}
if (bytes != null) {
File f=new File("img.jpg");
InputStream in = new ByteArrayInputStream(bytes);
BufferedImage image = ImageIO.read(in);
ImageIO.write(image, "jpg", f);
String path=f.getName();
ImageIcon icon = createImageIcon(path,"");
l.setIcon(icon);
}
}
catch(Exception ex){
System.out.println("error :"+ex);
}
}
protected static ImageIcon createImageIcon(String path,String description) {
java.net.URL imgURL = RetieveImage.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
Hope that it helps.
Thanks
Related Tutorials/Questions & Answers:
javajava diff bt core
java and
java Advertisements
java java why iterator in
java if we for loop
Java Java Whether
Java is pure object oriented Language
JAVAJAVA how the name came for
java language as "
JAVA javajava explain technologies are used in
java now days and structure
java javajava different between
java & core
java javajava is
java open source
javajava what is
java reflection
java java in
java does not pointers concept but what is nullpointers in
java?
nullpointer is a runtime Exception
javawhat is the size of array in
java ? what is the size of array in
java ?
what is the mean of finalize in
java javajava why to set classpath in
java javajava RARP implementation using
java socket
javajava sample code for RARP using
java JavaJava how to do
java in command prompt
javajava how use
java method
javajava is
java purely object oriented language
javajava why multiple inheritance is not possible in
java javajava give a simple example for inheritance in
java javajava give a simple example for inheritance in
java javajava why to set classpath in
java javajava why to set classpath in
java javajava why to set classpath in
java java java What is ?static? keyword
javajava Does
java allows multiline comments
javajava Write a
java code to print "ABABBABCABABBA
javajava write a program in
java to acess the email
javajava send me
java interview questions
javajava what are
JAVA applications development tools
JavaJava Whether
Java is Programming Language or it is SOftware
javajava explain object oriented concept in
java java java difference between class and interface
JavaJava how to draw class diagrams in
java javajava write a
java program using filenotfoundexception
javajava how to edit text document by using
java
then how to edit starting and ending of text document by using
java javajava different between
java & core
java
print("code sample
java java how can use sleep in
java
which book learn of
java language
java java How to set
java Policy for applet using jdk 6
javajava pattern code for a given words
java pattern code for a given words pattern
javajava dear,
i want a field for date picker using
java/
java script
javajava create
java program for delete and update the details,without using database, just a normal
java program
javajava why methods in
java raise exceptions
Have a look at the following link:
Java Exceptions
javajava code to search the nodes how to write the
java code to search the nodes using routers
javajava online telephone directory i need coding for online telephone directory..by using
java....pls help me
javajava different between
java & core
java
print("code sample
javajava how to invoke one chart
java file from another
java file
javajava how to prepare the
java
Hi Friend,
If you want to learn how to install
java, creating and running a
java program then go through the following links:
http://www.roseindia.net/
java/beginners/index.shtml
http
javajava
java swing
Swing is a principal GUI toolkit for the
Java programming language. It is a part of the JFC (
Java Foundation Classes), which is an API for providing a graphical user interface for
Java programs
javajava what is the need of
java if
java is not there what will happen... work unless you have
Java installed, and more are created every day.
Java... to scientific supercomputers, cell phones to the Internet,
Java is everywhere!
http