Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Swing-AWT Error in laodin and saving the image .

Have Programming Question? Ask it here!
 
 
 


Poonam
Error in laodin and saving the image .
1 Answer(s)      5 years ago
Posted in : Swing AWT



Advertisement
View Answers

September 15, 2009 at 10:26 AM


Hi Friend,

Try it:
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.imageio.ImageIO;

public class UploadImage extends JPanel {
static BufferedImage image;
static File file=null;
Image img;
public void paint(Graphics g) {
g.drawImage( image, 0, 0, null);
}
public static void main(String[] args) {
JFileChooser chooser = new JFileChooser();
chooser.addChoosableFileFilter(new ImageFileFilter());
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
try{
image = ImageIO.read(file);
}
catch(Exception ex){}
}
JFrame frame = new JFrame();
JPanel panel = new UploadImage();
frame.getContentPane().add(panel);
frame.setSize(300, 100);
frame.setVisible(true);
}
public UploadImage(){
JButton button=new JButton("Save");
this.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String fi=file.getName();
File f = new File(fi);
JFileChooser chooser1 = new JFileChooser();
chooser1.setSelectedFile(f);
int returnVal = chooser1.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File selectedFile = chooser1.getSelectedFile();
String fileName = selectedFile.getName();
System.out.println(fileName);
ImageIcon icon = new ImageIcon(fileName);
img = icon.getImage();
try{
ImageIO.write(toBufferedImage(img), "jpg", selectedFile);
}
catch(Exception e){}
}
}
});
}
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {return (BufferedImage)image;}
image = new ImageIcon(image).getImage();
boolean hasAlpha = hasAlpha(image);
BufferedImage image1 = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
int transparency = Transparency.OPAQUE;
if (hasAlpha == true) {transparency = Transparency.BITMASK;}
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
image1 = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
}
catch (HeadlessException e) {}
if (image1 == null) {
int type = BufferedImage.TYPE_INT_RGB;
if (hasAlpha == true) {type = BufferedImage.TYPE_INT_ARGB;}
image1 = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}
Graphics g = image1.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return image1;
}
public static boolean hasAlpha(Image image) {
if (image instanceof BufferedImage) {return ((BufferedImage)image).getColorModel().hasAlpha();}
PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
try {pg.grabPixels();} catch (InterruptedException e) {}
return pg.getColorModel().hasAlpha();
}
}
class ImageFileFilter extends javax.swing.filechooser.FileFilter {
public boolean accept(File file) {
if (file.isDirectory()) return false;
String name = file.getName().toLowerCase();
return (name.endsWith(".jpg") || name.endsWith(".png")|| name.endsWith(".gif"));
}
public String getDescription() { return "Images (*.gif,*.bmp, *.jpg, *.png )"; }
}

Thanks


Related Tutorials/Questions & Answers:
Error in laodin and saving the image . - Swing AWT
Error in laodin and saving the image .  I am a student who had... facing problem while laoding and saving the image with help of JFileChooser class... class UploadImage extends JPanel { static BufferedImage image; static File
Saving image and text in mysql db using spring?
Saving image and text in mysql db using spring?  Hii, My requirement is to save the employee details(including image) into mysql db.i want to save the image location in db .And actual image(binary formate) should save
Advertisements
java image loadin and saving problem - Swing AWT
java image loadin and saving problem  hey in this code i am trying to load a picture and save it.........but image is only visible whn we maximize... extends JPanel { static BufferedImage image; static File file=null; Image img
Image Upload Error
Image Upload Error  hi, I created one education website in jsp....in my registration page image uploading option also i attached in real path.......It's showing error in IndexoutboundException....can you guide me how it will resolve
saving files
saving files  i want to create a program that would allows me to save my file and attach data
php image error - Java Beginners
php image error  Hi, Please, give me a example php code for png to jpg image conversion. thank u in advance. regards, sakthi.s
Saving data in database
Saving data in database  hello i m facing a strange problem if i am submitting data form database then i can see it in database but if am submitting from app then it is not showing in to database but when i fetching form
saving data in xml
saving data in xml  Hi, I have an xml file with spring map, in that i have parent child nodes. I have jsp form in that i have put same parent child elements. when i submit jsp form I want to append same data into xml file
File saving and Writing
File saving and Writing   Hello Rose india..... I have a doubt for you plzzz kindly Help me... I have an "Arraylist" which includes some table values... What i want to ask is "I want to create a jsp page which will include
preview and saving an application form
preview and saving an application form   i have developed an application form, which is to be filled by the user.That application form is big in size so i have splitted that application form in 6 different forms ie, in first jsp
preview and saving an application form
preview and saving an application form   i have developed an application form, which is to be filled by the user.That application form is big in size so i have splitted that application form in 6 different forms ie, in first jsp
error in program when trying to load image in java - Java Beginners
error in program when trying to load image in java  Hi, I'm trying... don't know what it means. ProductInventoryVGUI Uncaught error fetching image... that error message above saying uncaught error fetching image. I don't know how
error in uploading image from jsp to oracle 10g database
error in uploading image from jsp to oracle 10g database  ... try to insert into the image into the database i got the above error please advice how to get rid of error   JSP upload image in Mysql database
storing images in directory,saving path in db2
storing images in directory,saving path in db2  i am working in a web portal that makes a simple networking site. i want to store the profiles images... the user logins, i shall retrieve the image and show it as the profile image again
saving data - JSP-Interview Questions
saving data  Dear Sir/Madam, Please teach me step by step how to save the data in JSP and using crimson editor? Thank u very much. rodmel  Hi friend, I am sending you data store code into the database
saving the changes in Microsoft sql sever - SQL
saving the changes in Microsoft sql sever  How do we commit the changes in Microsoft sql server
image
image  how to add the image in servlet code
how to upload multiple files in jsp and saving the path in database and the file in folder
how to upload multiple files in jsp and saving the path in database and the file in folder  how to upload multiple files in jsp and saving the path..., for example, i need to save a image for the photo of candidate and resume
Image
Image  how to insert image in xsl without using xml. the image was displayed in pdf..Please help me
Image
Image  how to insert image in xsl without using xml. the image was displayed in pdf..Please help me
Image
Image  how to insert image in xsl without using xml. the image was displayed in pdf..Please help me
Saving URL in local disk - Java Beginners
Saving URL in local disk  Hi Friend, How can i Save URL like "www.jagran.com " on local disk throgh java Statement. Thanks ,in advance
saving form bean with Array of objects (collection) - Struts
saving form bean with Array of objects (collection)  Hi all, this is my first post here, if I am too long or missed some thing please let me know thanks..:) I am facing problem to capture my array of objects(Order) in form bean
error
error  while iam compiling iam getting expected error
Error-
Error-   Hello, I would like to know about XSD file. I try to print XML file but I am getting error SAXException-- says Content is not allowed in prolog. Please help me
error
error  i have 404 error in my program plz tell me yhe solution about
error
error  i have 404 error in my program plz tell me yhe solution about
error
/ServletUserEnquiryForm.shtml getting an error given below SQLException caught: [Microsoft][ODBC SQL Server Driver]COUNT field incorrect or syntax error please suggest
Error
Error  I have created ajax with php for state and city. When I change state then city will not come in dropdown list and it give me error as 'Unknown Runtime Error'. This error come only in IE browser, but in other brower
change the name of the image after uploading the image
change the name of the image after uploading the image   my form consists of a textbox and uploading the image.after uploading the image i want to change the name of the image with the content of the textbox before saving
error
error  java.lang.unsupportedclassversionerror:bad major version at offset 6 how to solve this????   Hi, Please check the version of framework used and also the JDK version. This type error also comes when java file
error
error
error
error  When I deploye the example I have this message cannot Deploy HelloWorld Deployment Error for module: HelloWorld: Error occurred during deployment: Exception while deploying the app [HelloWorld
error!!!!!!!!!
error!!!!!!!!!   st=con.createStatement(); int a=Integer.parseInt(txttrno.getText()); String b=txttname.getText(); String c=txtfrom.getText(); String d=txtto.getText
error!!!!!!!!!
error!!!!!!!!!   st=con.createStatement(); int a=Integer.parseInt(txttrno.getText()); String b=txttname.getText(); String c=txtfrom.getText(); String d=txtto.getText
error!!!!!!!!!
error!!!!!!!!!   st=con.createStatement(); int a=Integer.parseInt(txttrno.getText()); String b=txttname.getText(); String c=txtfrom.getText(); String d=txtto.getText
Error
Error  Hi. I am getting error in the following code after the line I have commented as ERROR. How to solve this. Thanks in advance. package...;"); // ERROR out.println(" var pattern
Error
Error  Hi. I am getting error in the following code after the line I have commented as ERROR. How to solve this. Thanks in advance. package...;"); // ERROR out.println(" var pattern
Error
Error  Hi. I am getting error in the following code after the line I have commented as ERROR. How to solve this. Thanks in advance. package...;"); // ERROR out.println(" var pattern
Error
Error  Hi. I am getting error in the following code after the line I have commented as ERROR. How to solve this. Thanks in advance. package...;"); // ERROR out.println(" var pattern = /^\d{3,5
error
"+it); } } this is my program i am getting an error saying cannot find symbol class string
error
error  whats the error.............. import java.util.Scanner; public class g { public static void main(String[] args) { Scanner s=new Scanner(System.in); int d,x,y; System.out.println("Enter the first number
How to add save as dialoguebox to export into excel instead of directly saving..
How to add save as dialoguebox to export into excel instead of directly saving..  Hello Sir, i followed ur tutorial to export into excel from database and generated excel file at particular directory of my system
Java code for Saving Marathi (Indian local language ) in Mysql and retrieving the same
Java code for Saving Marathi (Indian local language ) in Mysql and retrieving the same  Hello , My query got solved. Thnx.I am right now... java using eclipse but after saving it shows " ???? " in place of marathi words
Save profile and image to mysql database, and view the image in another jsp page
Save profile and image to mysql database, and view the image in another jsp page  Pls. need help in saving the profile info with the image in mysql database.. some basic code pls in jsp... thanks in advance
image selection
it redirect to error image page. can you tell how to solve this problem and where...image selection  hi my issue is : I have select dropdown list when I selected image it displays some default color having image I change the color
image upload
image upload  How to access image file from database using jsp?  ... rs=st.executeQuery("SELECT images FROM image WHERE id = 1"); if(rs.next...("image/jpeg"); while((size=sImage.read(bytearray))!= -1
Image Selection - Swing AWT
{ System.err.println("Error saving...Image Selection  Hi, I need to provide the image selection facility... click on any item, the image of that item should be selected as done in windows
image scrolling
image scrolling  A brief description of scrolling image
Advertisements
 

 

 

DMCA.com