使用以下方法:

  @FXML
  public void exportSideImage() {
    File imageLocation = ImageExport.display();
    WritableImage image = SideView.getInstance().snapshot(null, null);
    BufferedImage bImage = SwingFXUtils.fromFXImage(image, null);
    try {
      ImageIO.write(bImage, getFileExtension(imageLocation), imageLocation);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  private String getFileExtension(File file) {
    String name = file.getName();
    int lastIndexOf = name.lastIndexOf(".");
    return name.substring(lastIndexOf + 1);
  }



如果指定了“ png”图像,我的程序将仅写入图像。我已经在多个平台(Linux,Windows)上对此进行了测试,但是问题仍然存在。有人知道这是怎么回事吗?

最佳答案

我建议您检查ImageIO.write =>中的结果代码,它可以报告为false,还可以打印文件扩展名以确认它是其中之一:

ImageIO.getWriterFileSuffixes()

关于java - ImageIO只能写PNG,其他格式失败(没有错误报告),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61749751/

10-10 16:29