Save the background image of a rectangle using file dialog in javafx - java

I have two rectangles in my scene .Then I set a background in those rectangles using setFill() and make some changes.How do I save those images to disk using saveDialog ? Here is my save function -
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(new ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"));
fileChooser.setTitle("Save Image");
File file = fileChooser.showSaveDialog(window);
rect2.getFill();
SaveFile(file);

You can create a javafx.scene.image.Image from any node by calling snapshot(...) on the node:
Image img = rect2.snapshot(null, null);
The javax.imageio.ImageIO class has methods for saving images in standard formats, but only works with AWT images, so you need to convert it first:
BufferedImage bImg = SwingFXUtils.fromFXImage(img, null);
String format = file.getName().substring(file.getName().lastIndexOf(".")+1);
ImageIO.write(bImg, format, file);

Related

Writing a GIF image over PNG image

I wonder if there is a way in java to put a gif image over png image at particular location (say at particular value of x,y). If so please help me through this.
This is the case :
I have a base Image which is of png type. and I have gif images of size 62*62. I wanted to put several such gif images on png image and I need to render the png image on front end at every 5 seconds..
To extract image from GIF file.. This save the first image into png file from GIF.
try {
ImageReader reader = ImageIO.getImageReadersByFormatName("gif").next();
ImageInputStream stream = ImageIO.createImageInputStream(new File("c:/aaa.gif");
reader.setInput(stream);
int count = reader.getNumImages(true);
if(count>0){
BufferedImage frame = reader.read(0);
ImageIO.write(frame, "png", new File(filePath+fileName+".png"));
System.out.println("Donesss");
}
} catch (IOException ex) {
}

How to save an image received through network in java

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

Drawing a resource image on to a buffered image

Okay, I want to create a copy of an image I have in my resource folder, and put it onto the desktop pretty much. Example: My project has a resource folder with an image called apple.png. Since when I export my jar file it can't find it, I want to copy it to the desktop so it can find it from there. Here is what I tried doing:
try {
// retrieve image
BufferedImage bi = new BufferedImage(256, 256,
BufferedImage.TYPE_INT_RGB);
File outputfile = new File(
"C:/Users/Owner/Desktop/saved.png");
ImageIO.write(bi, "png", outputfile);
} catch (IOException e) {
}
}
This just created the buffered Image for me on my desktop. How do I take my res Image and copy it to it.
Any reason for loading it as an image? If you just want to copy resource to desktop without changing it:
InputStream resStream = getClass().getResourceAsStream("/image.png"));
//Improved creation of output path:
File path = new File(new File(System.getProperty("user.home")), "Desktop");
File outputFile = new File(path, "saved.png");
//now write it
Files.copy(resStream, outputFile);
You need to load the BufferedImage as the image file.
BufferedImage bi = ImageIO.read(new File(getClass().getResource("/apple.png"));));
All the other steps are the same.

BufferedImage to File with black background

I'm trying to save a BufferedImage (came from byte[]) to a File, but it's producing a black background without the image. I'm using the photoCam from primefaces.
This is my ManagedBean method:
public void webcamCapture(CaptureEvent captureEvent) {
try {
byte[] data = captureEvent.getData();
InputStream in = new ByteArrayInputStream(data);
BufferedImage fotoBuffered = ImageIO.read(in);
String idImagem = ImagemHelper.getInstance().salvarImagemFromImageObject(fotoBuffered);
paciente.getPessoaFisica().setFoto(idImagem);
} catch (Exception e) {
addErrorMessage("Erro ao capturar imagem da webcam");
FacesContext.getCurrentInstance().validationFailed();
}
}
The method "salvarImagemFromImageObject" simple make a "ImageIO.write(image,"jpg",destFile)" to save a file, but this file don't have nothing, just a black background.
Primefaces PhotoCam component renders PNG images. PNG format is by design. If you want to work with another file format, you'll need to post-process the PNG image rendered by the PF component.
Refactor your salvarImagemFromImageObject function with a .png destFile:
ImageIO.write(fotoBuffered, "png", destFile);
EDIT
Writes the resulting png data to jpeg format:
//Converts PNG image to plain RGB format
BufferedImage newBufferedImage = new BufferedImage(fotoBuffered.getWidth(), fotoBuffered.getHeight(), BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(fotoBuffered , 0, 0, Color.WHITE, null);
//Then, writes to jpeg file
ImageIO.write(newBufferedImage, "jpg", destFile);

Saving png image with Java using JFileChooser

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!");
}
}

Categories

Resources