I am trying to save an image that is formed with a BufferedImage. I get the BufferedImage by doing
(BufferedImage) fg;
fg is an image of my jPanel's graphics. I am successful at saving the image by hardcoding the path in directly as follows:
ImageIO.write((BufferedImage)fg,"png",new File("C:\\Users\\Geiger\\Documents\\test.png"));
But when I attempt to add the JFileChooser to the mix the image that is saved comes up being blank with nothing but the jPanel's background color.
My code for my attempt at utilizing the JFileChooser is as follows:
JFileChooser jfc = new JFileChooser();
int retVal = jfc.showSaveDialog(null);
if(retVal==JFileChooser.APPROVE_OPTION){
File f = jfc.getSelectedFile();
String test = f.getAbsolutePath();
ImageIO.write((BufferedImage)fg,"png",new File(test));
}
EDIT:To clarify on the issue a little bit more:
The issue is not that a file doesn't appear its that the graphics don't appear on the image when using the JFileChooser object.
I update my image when the JFrame has a mouse presses event:
fg = jPanel2.createImage(jPanel2.getWidth(), jPanel2.getHeight());
try to put this line of code I think that's what you need:
ImageIO.write(buffer, "png", fileDialog.getSelectedFile());
Hope that helps
Graphics2D graphics2D = image.createGraphics();
scribblePane.paint(graphics2D);
Use these two lines of code to add graphics.
That's works for me
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("*.png", "png"));
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
ImageIO.write((BufferedImage) image, "png", new File(file.getAbsolutePath()));
} catch (IOException ex) {
System.out.println("Failed to save image!");
}
} else {
System.out.println("No file choosen!");
}
}
Related
When using ImageIO.read() on a .tif image the pixel intensity values are being distorted. I believe this is probably due to it being loaded as an rgb which results in vastly different pixel intensities. Is there any way around this? or any other library that allows the import of single banded .tif images?
the images below demonstrate the issue that is occurring. This is the image, The left image is the correct pixel values loaded through imageJ and check again through loading it in matlab and the right is the result of viewing the image through a method in eclipse (also checked in other ide's though).
JFileChooser chooser = new JFileChooser();
int returnValue = chooser.showOpenDialog(null);
File file = null;
if (returnValue == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
}
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JLabel(new ImageIcon(image)));
frame.pack();
frame.setVisible(true);
The intensity values are very important so any help would be appreciated!
image is successfully received at the server side and I can display it on label
but my Problem is how to save that image
I have used
JFileChooser.showSaveDialog()
I tried printstream. I can save the file but whenever I opened the file in image viewer it is showing as this type of file is cant be opened
BufferedImage img=ImageIO.read(ImageIO.createImageInputStream(sock.getInputStream()));
System.out.println("Image received!!!!");
JFileChooser fc = new JFileChooser();
int i=fc.showSaveDialog(null);
if( i == JFileChooser.APPROVE_OPTION ) {
PrintStream ps = new PrintStream(fc.getSelectedFile());
// ImageIO.write(bimg,"JPG",fc.getInputStream());
ps.print( img);
ps.close();
lblNewLabel.setIcon(new ImageIcon(img)); //image is successfully displaying on the label
}
You're writing the "object" representation of the image, only if your load it through PrintStream would you have a chance of seeing it again.
Try using something like...
ImageIO.write(img,"JPG",fc.getSelectedFile());
instead
I would like to print the content of a JFrame/JPanel by printer how to do it? Could sb give me an example of code? I do not understand official tutorial.
EDIT:
I thought it is clear. I do not want to print it to file then print it manually. I want to use java to do this.
BufferedImage image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
this.paint(g);
g.dispose();
// File to save output Image
File imageOut = new File("screenshot.png");
try {
ImageIO.write(image, "png", imageOut);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
with this as a JFrame
I have a an Image Icon generated in my code which i place it as an icon on a label as per the following code:
ImageIcon icon = new ImageIcon(barcode.drawBarcode());
jLabel36.setIcon(icon);
Now my problem is that how can i change the ImageIcon type to Image and save it on the hard disk. when i try to type cast ImageIcon to Image i get the following error :
java.lang.ClassCastException: javax.swing.ImageIcon cannot be cast to java.awt.Image
Can anyone suggest me how can i achieve this task both type casting and saving the image.
Just use getImage():
// get image from imageicon
Image image = icon.getImage();
// cast it to bufferedimage
BufferedImage buffered = (BufferedImage) image;
try {
// save to file
File outputfile = new File("saved.png");
ImageIO.write(buffered, "png", outputfile);
} catch (IOException e) {
e.printStackTrace();
}
I am using an Applet to save image from clipboard. The image is saved but something happened with its format. It is darken and lost colors.
here's how I am doing it:
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
try {
//create clipboard object
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//Get data from clipboard and assign it to an image.
//clipboard.getData() returns an object, so we need to cast it to a BufferdImage.
BufferedImage image = (BufferedImage) clipboard.getData(DataFlavor.imageFlavor);
//file that we'll save to disk.
File file = new File("/tmp/clipboard.jpg");
//class to write image to disk. You specify the image to be saved, its type,
// and then the file in which to write the image data.
ImageIO.write(image, "jpg", file);
//getData throws this.
} catch (UnsupportedFlavorException ufe) {
ufe.printStackTrace();
return "Não tem imagem na área de transferência";
} catch (Exception ioe){
ioe.printStackTrace();
}
return null;
}
}
);
I read that Mac uses a different image format but I did not find how to convert it to a format I could save. I imagined that java should have taken care of that.
So, how can I convert the image from clipboard to jpg?
PS. I tried using png instead of jpg, got a worse result: black image
To solve the issue on Mac, I used the solution proposed on The nightmares of getting images from the Mac OS X clipboard using Java.
I pass the retrieved BufferedImage to method that redraws it to new BufferedImage, returning a valid image. Here follows the code from that page:
public static BufferedImage getBufferedImage(Image img) {
if (img == null) return null;
int w = img.getWidth(null);
int h = img.getHeight(null);
// draw original image to thumbnail image object and
// scale it to the new size on-the-fly
BufferedImage bufimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bufimg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(img, 0, 0, w, h, null);
g2.dispose();
return bufimg;
}
And how I use it:
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//Get data from clipboard and assign it to an image.
//clipboard.getData() returns an object, so we need to cast it to a BufferdImage.
BufferedImage image = (BufferedImage) clipboard.getData(DataFlavor.imageFlavor);
if (isMac()) {
image = getBufferedImage(image);
}
PNG is the preferred image format for Macs. You might want to try saving as that and then converting to a JPG if needed afterwards.