3

我只是做了一个类,让我可以使用 ColorAjust 类修改饱和度亮度、对比度和色调等图像。

但我不知道如何在进行这些修改后保存该图像。

这是代码:

final Stage imageProcessStage = new Stage();
        imageProcessStage.initModality(Modality.APPLICATION_MODAL);
        imageImageProcess = new Image(ImagePathImageProcess);
        imageViewImageProcess = ImageViewBuilder.create().image(imageImageProcess).build();
        ColorAdjust colorAdjust = ColorAdjustBuilder.create().build();
        imageViewImageProcess.setEffect(colorAdjust);
        // 
        Label saturationLabel = LabelBuilder.create().text("Saturation").build();
        GridPane.setConstraints(saturationLabel, 0, 0);
        Slider saturationSlider = SliderBuilder.create().value(50).build();
        colorAdjust.saturationProperty().bind(saturationSlider.valueProperty().divide(50).subtract(1));
        GridPane.setConstraints(saturationSlider, 1, 0);
        GridPane.setHgrow(saturationSlider, Priority.ALWAYS);
        Label saturationValueLabel = LabelBuilder.create().minWidth(75).maxWidth(75).build();
        saturationValueLabel.textProperty().bind(colorAdjust.saturationProperty().multiply(100).asString("%.2f%%"));
        GridPane.setConstraints(saturationValueLabel, 2, 0);
        // 
        Label hueLabel = LabelBuilder.create().text("Hue").build();
        GridPane.setConstraints(hueLabel, 0, 1);
        Slider hueSlider = SliderBuilder.create().value(50).build();
        colorAdjust.hueProperty().bind(hueSlider.valueProperty().divide(50).subtract(1));
        GridPane.setConstraints(hueSlider, 1, 1);
        GridPane.setHgrow(hueSlider, Priority.ALWAYS);
        Label hueValueLabel = LabelBuilder.create().minWidth(75).maxWidth(75).build();
        hueValueLabel.textProperty().bind(colorAdjust.hueProperty().multiply(100).asString("%.2f%%"));
        GridPane.setConstraints(hueValueLabel, 2, 1);
        // 
        Label brightnessLabel = LabelBuilder.create().text("Brightness").build();
        GridPane.setConstraints(brightnessLabel, 0, 2);
        Slider brightnessSlider = SliderBuilder.create().value(50).build();
        colorAdjust.brightnessProperty().bind(brightnessSlider.valueProperty().divide(50).subtract(1));
        GridPane.setConstraints(brightnessSlider, 1, 2);
        GridPane.setHgrow(brightnessSlider, Priority.ALWAYS);
        Label brightnessValueLabel = LabelBuilder.create().minWidth(75).maxWidth(75).build();
        brightnessValueLabel.textProperty().bind(colorAdjust.brightnessProperty().multiply(100).asString("%.2f%%"));
        GridPane.setConstraints(brightnessValueLabel, 2, 2);
        // 
        Label contrastLabel = LabelBuilder.create().text("Contrast").build();
        GridPane.setConstraints(contrastLabel, 0, 3);
        Slider contrastSlider = SliderBuilder.create().value(50).build();
        colorAdjust.contrastProperty().bind(contrastSlider.valueProperty().divide(50).subtract(1));
        GridPane.setConstraints(contrastSlider, 1, 3);
        GridPane.setHgrow(contrastSlider, Priority.ALWAYS);
        Label contrastValueLabel = LabelBuilder.create().minWidth(75).maxWidth(75).build();
        contrastValueLabel.textProperty().bind(colorAdjust.contrastProperty().multiply(100).asString("%.2f%%"));
        GridPane.setConstraints(contrastValueLabel, 2, 3);

        //Validate Button
        Button btnValider = new Button("Valider");
        btnValider.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event) {   

              // SAVE IMAGE HERE

            }
        });
4

2 回答 2

2

升级到JavaFX 2.2并在您的按钮事件处理程序中使用以下代码。

ImageIO.write(
  SwingFXUtils.fromFXImage(
   imageViewImageProcess.snapshot(null, null), null
  ),
  "png",
  new File("valider.png")
);

请注意,2.2 目前处于开发者预览版而非 GA 产品中,因此您可能会遇到一些问题和错误,直到新的 2.2 方法经过彻底的 QAed。

这是一个完整的可执行示例:https ://gist.github.com/2870355

于 2012-06-04T19:14:33.810 回答
2

通用且更简单的代码版本:

private void saveImage(
    Image i, 
    String extension, 
    String pathname) 
throws IOException {

ImageIO.write(
    SwingFXUtils.fromFXImage(
        image, 
        null), 
    extension, 
    new File(
        pathname));
}
于 2013-10-19T16:49:35.060 回答