java - 如何在 Mac 上使用 JavaFX 将图像保存到文件系统?

标签 java image save javafx javafx-2

我正在尝试将图像保存到我的文件系统,如 javafx 图像操作页面上所述:http://docs.oracle.com/javafx/2/image_ops/jfxpub-image_ops.htm但我不断收到此异常错误:

java[80783:707] [JRSAppKitAWT markAppIsDaemon]: Process manager already initialized: can't fully enable headless mode.

这个 stackoverflow 问题有点帮助 JavaFX screencapture headless exception on OSX但我仍然坚持保存图像。

有什么办法可以保存图像吗?

这就是我目前正在做的事情:

File outFileImage = new File(imageUrl);

Toolkit tk = java.awt.Toolkit.getDefaultToolkit().getDefaultToolkit();
try {
    ImageIO.write(SwingFXUtils.fromFXImage(image, null), ".png", outFileImage);
} catch (Exception e) {
    log.log(Level.WARNING, "Image not saved to disk", e);
}


编辑

我尝试运行 Java 8 以查看问题是否能得到解决,但我不断收到 JavaFX FXML 错误:

javafx.fxml.LoadException: Root hasn't been set. Use method setRoot() before load.
/Users/jonathan/Projects/Dominion/target/classes/dominion/application/controller/main_overview_tab.fxml:13

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2613)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:104)
at javafx.fxml.FXMLLoader$RootElement.constructValue(FXMLLoader.java:1320)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:740)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2723)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at dominion.application.controller.MainOverviewTab.initView(MainOverviewTab.java:64)


编辑

我已将此 JavaFX FXML 加载问题发布到此处找到的另一个问题中:Root hasn't been set Error with Java 8 Eclipse

最佳答案

RT-20784 Mac: Headless environment issue, MacOSX 的评论中,这与您粘贴到问题中的警告消息相关,开发人员指出:

This indeed requires fixes in JDK. We don't have plans to back port these specific changes to 7uX releases because they are somewhat risky, and we don't want to destabilize the 7uX branch. As Steve suggests, please move to 8.

AFAIK, apart from the fact that FX forces the AWT to run in the headless mode, nothing else changes or breaks because of this. So you may assume that the warning printed out to the console is the only side-effect of the missing changes. As long as you don't need to use AWT/Swing in your application, you should be safe.

所以也许你最好的做法是尝试 Java 8看看您是否还有任何问题。

根据有关 setRoot() 的更新问题进行更新

您在 Java 8 的哪个版本中遇到 FXML 错误?据此Bug Report ,这个特定的“根尚未设置”FXML 加载错误可能已在 JavaFX 8b121 中修复。可能会看到related StackOverflow question on setRoot errors 。 FXML 加载问题与将图像保存到文件系统的原始问题无关,通常不相关的问题最好作为新问题提出。

关于java - 如何在 Mac 上使用 JavaFX 将图像保存到文件系统?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21178646/

相关文章:

java - 如何获取不是整数而是字符的微调器值

java - NDEF_DISCOVERED 的多个 Activity

java - 推特认证 API 问题

java - JFileChooser 过滤器

html - R 中 htmlwidget 的 savewidget,无法将 html 文件保存在另一个文件夹中

php - 如何使用php保存图片?

java - 如何解决Play Framework中的空指针异常?

android - Android 上的 NV21 转位图,图像非常暗、灰度还是黄色?

ajax - 通过ajax请求将字符串发送到php gd

python - 是否有 numpy.save(file, arr) 的替代方法可以与 pypy 一起使用?