Java保存图像

时间:2018-11-24 11:47:16

标签: java swing javafx java-8

我写了一个程序,可以为我生成图像。 到目前为止,一切正常,请保存。

在我要保存的方法中,我有一个WritabelImage 我用BuffertImage做一个SwingFXUtils.fromFXImage ()。在Intellij的调试器中,我也可以查看它,并且它实际上已经存在。

然后,我将输出路径创建为URI,并将其传递给Fil对象。

我想结合ImageIO.write ()并保存两者。 那是行不通的。 路径退出。 我尝试使用直接URI以及uri.getPath ()

我只是不知道如何继续。 你能帮我吗?

try {
        String path;
        URI    uri;
        uri = this.getClass().getResource("").toURI();
        path = uri.toString().replace("foo/bar/foobar/generate/", "generate/out/" + name + ".png");
        uri = URI.create(path);
        File          outputFile = new File(uri.getPath());
        BufferedImage bImage     = SwingFXUtils.fromFXImage(writableImage, null);
        System.out.println("Saved");
        ImageIO.write(bImage, ".png", outputFile);

    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:2)

格式名称参数不应包含点。简单尝试"png"