如何在JavaFX中显示PGM图片?

huangapple go评论79阅读模式
英文:

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(&quot;image.pgm&quot;);
WritableImage fxImage = SwingFXUtils.toFXImage(imagePlus.getBufferedImage(), null);
ImageView imageView = new ImageView(fxImage);

ImageJ Maven dependency:

&lt;!-- https://mvnrepository.com/artifact/net.imagej/ij --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;net.imagej&lt;/groupId&gt;
    &lt;artifactId&gt;ij&lt;/artifactId&gt;
    &lt;version&gt;1.52u&lt;/version&gt;
&lt;/dependency&gt;

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

huangapple
  • 本文由 发表于 2020年4月5日 00:21:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61031098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定