简体   繁体   English

如何在 JavaFX 中显示 PGM 图片?

[英]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.所以我试图打开一个 PGM 文件以在我的 JavaFX 场景中的 ImageView 小部件中显示它,但它不起作用。 Any idea how can I display the PGM file?知道如何显示 PGM 文件吗? Is there any way to convert it to a JPG/PNG file and then display it?有没有办法将其转换为 JPG/PNG 文件然后显示? 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();
}

You can use ImageJ to convert a PGM file to a BufferedImage that can easily be converted to JavaFX Image:您可以使用ImageJ将 PGM 文件转换为可轻松转换为 JavaFX 图像的BufferedImage

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

ImageJ Maven dependency: ImageJ Maven 依赖:

<!-- 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注意:您可以参考答案以获取有关 JavaFX 和 ImageJ 中支持的图像类型的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM