How can I display a PGM picture in a JavaFX?

So I tried to open a PGM file to display it in an ImageView widget in my JavaFX scene, but it's not working. Any idea how can I display the PGM file? Is there any way to convert it to a JPG/PNG file and then display it? thanks!

if (file != null) {
    Image image1 = new Image(file.toURI().toString());
    avatar.setImage(image1); //avatar is an ImageView widget in my JavaFX interface
    adresse = file.getPath();
}

Upvotes: 0

Views: 159

Answers (1)

Miss Chanandler Bong
Miss Chanandler Bong

Reputation: 4258

You can use ImageJ to convert a PGM file to a BufferedImage that can easily be converted to JavaFX Image:

ImagePlus imagePlus = new ImagePlus("image.pgm");
WritableImage fxImage = SwingFXUtils.toFXImage(imagePlus.getBufferedImage(), null);
ImageView imageView = new ImageView(fxImage);

ImageJ Maven dependency:

<!-- https://mvnrepository.com/artifact/net.imagej/ij -->
<dependency>
    <groupId>net.imagej</groupId>
    <artifactId>ij</artifactId>
    <version>1.52u</version>
</dependency>

Note: you can refer to this answer for more information about the supported image types in JavaFX and ImageJ

Upvotes: 2

Related Questions