How can i export an image to a jpg file? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can i export an image to a jpg file?

I have converted a canvas into an image, but I can't find how to export it, I use java fx

27th Feb 2022, 6:10 AM
Joel Alejandro Aguilar Quiñónez
Joel Alejandro Aguilar Quiñónez - avatar
1 Answer
+ 2
Try this: import javax.imageio.ImageIO; import javafx.scene.image.WritableImage; import javafx.embed.swing.SwingFXUtils; import java.io.File; .... File file = new File(directoryPath+"test.jpg"); WritableImage writableimage = yourCanvas.snapshot(null); /* need to remove the alpha channel from the image. It's a bug. Instead of solving it, the library refuses to create a image without the alpha channel. Such as an jpg/JPEG file. BufferedImage jpgImage = new BufferedImage((int)writableimage.getWidth(), (int)writableimage.getHeight(), BufferedImage.TYPE_INT_RGB); ImageIO.write(SwingFXUtils.fromFXImage(jpgImage, null), "jpg", file);
27th Feb 2022, 8:54 AM
Mustafa A
Mustafa A - avatar