Tuesday, January 22, 2013

Set effect on ImageView with ColorAdjust

javafx.scene.effect.ColorAdjust is effect that allows for per-pixel adjustments of hue, saturation, brightness, and contrast.

Set effect on ImageView with ColorAdjust
Set effect on ImageView with ColorAdjust

package javafx_imageprocessing;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.control.SliderBuilder;
import javafx.scene.control.Tooltip;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_ImageProcessing extends Application {

ImageView imageView_Source, imageView_Target;
Slider sliderContrast, sliderHue, sliderBrightness, sliderSaturation;

@Override
public void start(Stage primaryStage) {

Image image = new Image("http://goo.gl/kYEQl");
imageView_Source = new ImageView();
imageView_Source.setImage(image);

imageView_Target = new ImageView();
imageView_Target.setImage(image);

HBox hBoxImage = new HBox();
hBoxImage.getChildren().addAll(imageView_Source, imageView_Target);

sliderContrast = SliderBuilder.create()
.prefWidth(300)
.min(-1)
.max(1)
.majorTickUnit(0.2)
.showTickMarks(true)
.showTickLabels(true)
.value(0)
.tooltip(new Tooltip("Contrast"))
.build();

sliderHue = SliderBuilder.create()
.prefWidth(300)
.min(-1)
.max(1)
.majorTickUnit(0.2)
.showTickMarks(true)
.showTickLabels(true)
.value(0)
.tooltip(new Tooltip("Hue"))
.build();

sliderBrightness = SliderBuilder.create()
.prefWidth(300)
.min(-1)
.max(1)
.majorTickUnit(0.2)
.showTickMarks(true)
.showTickLabels(true)
.value(0)
.tooltip(new Tooltip("Brightness"))
.build();

sliderSaturation = SliderBuilder.create()
.prefWidth(300)
.min(-1)
.max(1)
.majorTickUnit(0.2)
.showTickMarks(true)
.showTickLabels(true)
.value(0)
.tooltip(new Tooltip("Saturation"))
.build();

Button btnProcess = new Button("Process...");
btnProcess.setOnAction(btnProcessEventListener);

VBox vBox = new VBox();
vBox.getChildren().addAll(hBoxImage,
sliderContrast,
sliderHue,
sliderBrightness,
sliderSaturation,
btnProcess);

StackPane root = new StackPane();
root.getChildren().add(vBox);
Scene scene = new Scene(root, 350, 330);
primaryStage.setTitle("java-buddy.blogspot.com");
primaryStage.setScene(scene);
primaryStage.show();

updateEffect();

}

public static void main(String[] args) {
launch(args);
}

EventHandler<ActionEvent> btnProcessEventListener
= new EventHandler<ActionEvent>(){

@Override
public void handle(ActionEvent t) {
updateEffect();
}
};

private void updateEffect(){
Double valueContrast = sliderContrast.valueProperty().doubleValue();
Double valueHue = sliderHue.valueProperty().doubleValue();
Double valueBrightness = sliderBrightness.valueProperty().doubleValue();
Double valueSaturation = sliderSaturation.valueProperty().doubleValue();

ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setContrast(valueContrast);
colorAdjust.setHue(valueHue);
colorAdjust.setBrightness(valueBrightness);
colorAdjust.setSaturation(valueSaturation);

imageView_Target.setEffect(colorAdjust);
}
}


Related: Create and adjust Color using hue, saturation, brightness


No comments:

Post a Comment