java - 如何在 JavaFX 中保存对 ImageView 所做的更改?

标签 java image javafx

我在ImageView的帮助下向显示的图像添加了效果和颜色调整。 .

现在我想将这些更改保存到另一个文件中。我该如何执行此操作?

最佳答案

您需要snapshotfromFXImage 配对的 Node 类的功能SwingFXUtils 的方法。

Takes a snapshot of this node and returns the rendered image when it is ready. CSS and layout processing will be done for the node, and any of its children, prior to rendering it. The entire destination image is cleared to the fill Paint specified by the SnapshotParameters.

示例:

ImageView imageViewAdjusted = new ImageView(new Image(getClass().getResource("thinking-man.jpg").toExternalForm(), 250, 250, true, true));

ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setContrast(0.9);

imageViewAdjusted.setEffect(colorAdjust);
imageViewAdjusted.setCache(true);
imageViewAdjusted.setCacheHint(CacheHint.SPEED);

Button btnSave = new Button("Save to File");
btnSave.setOnAction(new EventHandler<ActionEvent>() {

    @Override
    public void handle(ActionEvent event) {
        File outputFile = new File("D:/formattedPicture.png");
        BufferedImage bImage = SwingFXUtils.fromFXImage(imageViewAdjusted.snapshot(null, null), null);
        try {
            ImageIO.write(bImage, "png", outputFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
});

关于java - 如何在 JavaFX 中保存对 ImageView 所做的更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37608808/

相关文章:

JavaFX 图表未正确分层

java - 如何在java正则表达式中对两个字符进行分组?

java - 使用 CriteriaBuilder 的动态查询 JPA 2 规范

java - Android OCR(光学字符识别)

image - Flutter 图像未加载(异常 : resolving an image codec)

java - 将图像导入 Eclipse

java - Nutch 1.4 与 Solr 3.5 http.agent.name 属性的集成

php - Laravel 404 Not Found,但图片路径正确

java - 处理表格 View 中的按钮单击事件

string - 如何从 JavaFX 中的字符串加载 FXML