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



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

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Poonam
Error in laodin and saving the image .
1 Answer(s)      5 years and 4 months 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 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
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
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
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 add the image in servlet code
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
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-   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
error
error
 

 

 

DMCA.com