JavaFX |颜色调整类
ColorAdjust 类是 JavaFX 的一部分。 ColorAdjust 类允许对色调、饱和度、亮度和对比度进行逐像素调整。 ColorAdjust 类继承 Effect 类。
类的构造函数:
- ColorAdjust() :创建 ColorAdjust 类的新对象
- ColorAdjust(双色调,双饱和度,双亮度,双对比度) :使用指定的色调,饱和度,亮度和对比度值创建 ColorAdjust 类的新对象。
常用方法:
Methods Explanation getBrightness() Returns the value of brightness of ColorAdjust effect setBrightness(double v) Sets the value of brightness of ColorAdjust effect getHue() Returns the value of Hue of ColorAdjust effect setHue(double v) Sets the value of Hue of ColorAdjust effect getContrast() Returns the value of Contrast of ColorAdjust effect setContrast(double v) Sets the value of Contrast of ColorAdjust effect getSaturation() Returns the value of Saturation of ColorAdjust effect setSaturation(double v) Sets the value of Saturation of ColorAdjust effect getInput() Returns the value of property input setInput(Effect v) Sets the value of property input
下面的程序说明了 ColorAdjust 类的使用:
- Java程序将颜色调整效果应用于具有指定色调、亮度、对比度和饱和度的图像:在该程序中,创建FileInputStream并将图像作为文件的输入。名为image的图像是使用来自文件输入流的输入创建的。从图像中创建图像视图对象并将其添加到VBox 。然后将VBox添加到场景中,并将场景添加到舞台上。创建一个ColorAdjust效果,并使用函数setHue() 、 setBrightness() 、 setSaturation() 、 setContrast()设置色调、饱和度、对比度和亮度的值,并使用setEffect()函数将效果设置为图像视图。
Java
// Java Program to apply color Adjust effect
// to a image with specified hue, brightness,
// contrast and Saturation
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.image.*;
import javafx.scene.effect.*;
import java.io.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
public class ColorAdjust_1 extends Application {
// launch the application
public void start(Stage stage) throws Exception
{
// set title for the stage
stage.setTitle("ColorAdjust example");
// create a input stream
FileInputStream input = new FileInputStream("f:\\gfg.png");
// create a image
Image image = new Image(input);
// create a image View
ImageView imageview = new ImageView(image);
// create a ColorAdjust effect
ColorAdjust color_adjust = new ColorAdjust();
// set hue, saturation, brightness, and contrast
color_adjust.setHue(0.4);
color_adjust.setBrightness(0.6);
color_adjust.setContrast(0.8);
color_adjust.setSaturation(0.1);
// set effect
imageview.setEffect(color_adjust);
// create a VBox
VBox vbox = new VBox(imageview);
// create a scene
Scene scene = new Scene(vbox, 200, 200);
// set the scene
stage.setScene(scene);
stage.show();
}
// Main Method
public static void main(String args[])
{
// launch the application
launch(args);
}
}
Java
// Java Program to apply color Adjust effect
// to a image with hue, brightness, contrast
// and Saturation taken as input from from user
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.image.*;
import javafx.scene.effect.*;
import java.io.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
public class ColorAdjust_2 extends Application {
// launch the application
public void start(Stage stage) throws Exception
{
// set title for the stage
stage.setTitle("ColorAdjust example");
// textfields
TextField hue, saturation, brightness, contrast;
// create the textFields
hue = new TextField("Hue");
saturation = new TextField("Saturation");
brightness = new TextField("Brightness");
contrast = new TextField("Contrast");
// create a input stream
FileInputStream input = new FileInputStream("f:\\gfg.png");
// create a image
Image image = new Image(input);
// create a image View
ImageView imageview = new ImageView(image);
// create a ColorAdjust effect
ColorAdjust color_adjust = new ColorAdjust();
// create a button
Button button = new Button("apply");
// action event
EventHandler event = new EventHandler() {
public void handle(ActionEvent e)
{
// set the hue, brightness, contrast and saturation
color_adjust.setHue(Double.parseDouble(hue.getText()));
color_adjust.setBrightness(Double.parseDouble(
brightness.getText()));
color_adjust.setContrast(Double.parseDouble(
contrast.getText()));
color_adjust.setSaturation(Double.parseDouble(
saturation.getText()));
}
};
// set on action of button
button.setOnAction(event);
// set effect
imageview.setEffect(color_adjust);
// create a VBox
VBox vbox = new VBox(imageview, hue, saturation,
brightness, contrast, button);
// create a scene
Scene scene = new Scene(vbox, 200, 400);
// set the scene
stage.setScene(scene);
stage.show();
}
// Main Method
public static void main(String args[])
{
// launch the application
launch(args);
}
}
- 输入图像:
- 输出:
- Java程序将颜色调整效果应用于具有色相、亮度、对比度和饱和度的图像作为用户的输入(使用文本字段):在该程序中,创建FileInputStream并将图像作为文件的输入。名为image的图像是使用来自文件输入流的输入创建的。从图像中,创建图像视图对象并将其添加到VBox 。然后将VBox添加到场景中,并将场景添加到舞台上。创建一个ColorAdjust效果,并使用函数setHue() 、 setBrightness() 、 setSaturation() 、 setContrast()设置色调、饱和度、对比度和亮度的值,并使用setEffect()函数将效果设置为图像视图。我们将创建四个文本字段(色调、饱和度、对比度和亮度)和一个按钮按钮。用户将给出必要的色调、饱和度、对比度和亮度值,当按下按钮时,这些值将应用于图像。创建一个EventHandler来处理按钮事件。
Java
// Java Program to apply color Adjust effect
// to a image with hue, brightness, contrast
// and Saturation taken as input from from user
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.image.*;
import javafx.scene.effect.*;
import java.io.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
public class ColorAdjust_2 extends Application {
// launch the application
public void start(Stage stage) throws Exception
{
// set title for the stage
stage.setTitle("ColorAdjust example");
// textfields
TextField hue, saturation, brightness, contrast;
// create the textFields
hue = new TextField("Hue");
saturation = new TextField("Saturation");
brightness = new TextField("Brightness");
contrast = new TextField("Contrast");
// create a input stream
FileInputStream input = new FileInputStream("f:\\gfg.png");
// create a image
Image image = new Image(input);
// create a image View
ImageView imageview = new ImageView(image);
// create a ColorAdjust effect
ColorAdjust color_adjust = new ColorAdjust();
// create a button
Button button = new Button("apply");
// action event
EventHandler event = new EventHandler() {
public void handle(ActionEvent e)
{
// set the hue, brightness, contrast and saturation
color_adjust.setHue(Double.parseDouble(hue.getText()));
color_adjust.setBrightness(Double.parseDouble(
brightness.getText()));
color_adjust.setContrast(Double.parseDouble(
contrast.getText()));
color_adjust.setSaturation(Double.parseDouble(
saturation.getText()));
}
};
// set on action of button
button.setOnAction(event);
// set effect
imageview.setEffect(color_adjust);
// create a VBox
VBox vbox = new VBox(imageview, hue, saturation,
brightness, contrast, button);
// create a scene
Scene scene = new Scene(vbox, 200, 400);
// set the scene
stage.setScene(scene);
stage.show();
}
// Main Method
public static void main(String args[])
{
// launch the application
launch(args);
}
}
- 输入图像:
- 输出:
注意:上述程序可能无法在在线 IDE 中运行。请使用离线编译器。
参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/effect/ColorAdjust.html