英文:
How can I display a PGM picture in a JavaFX?
问题
if (file != null) {
Image image1 = new Image(file.toURI().toString());
avatar.setImage(image1); //avatar是我JavaFX界面中的ImageView小部件
adresse = file.getPath();
}
英文:
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();
}
答案1
得分: 2
你可以使用 ImageJ 来将 PGM 文件转换为可以轻松转换为 JavaFX 图像的 BufferedImage
:
ImagePlus imagePlus = new ImagePlus("image.pgm");
WritableImage fxImage = SwingFXUtils.toFXImage(imagePlus.getBufferedImage(), null);
ImageView imageView = new ImageView(fxImage);
ImageJ Maven 依赖:
<!-- https://mvnrepository.com/artifact/net.imagej/ij -->
<dependency>
<groupId>net.imagej</groupId>
<artifactId>ij</artifactId>
<version>1.52u</version>
</dependency>
注意:你可以参考 这个 答案以获取关于 JavaFX 和 ImageJ 支持的图像类型的更多信息。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论