java - 在 JavaFX 中的图像上使用 ColorPicker

标签 java image javafx imageview color-picker

我在 ImageView 中有一张图像。我想做的就是给它添加颜色。基本上它就像图像上的一层颜色。所以我尝试使用 ColorPicker 为 T 恤图像添加颜色,但我不知道如何做到这一点。我已经尝试过将其与 colorAdjust 类一起使用,但它在 T 恤上提供的颜色与我从 ColorPicker 中选择的颜色不同。

顺便说一句,我正在使用 SceneBuilder。

图形用户界面

GUI

T恤

T-shirt

@FXML
private void changecolor(ActionEvent event) {      
    Color mycolor=mycolorpicker.getValue();
    label.setBackground(new Background(new BackgroundFill(mycolor,null,null)));
    Image img=new Image(getClass().getResourceAsStream("tshirt7.PNG"));
    imageV.setImage(img);
    ColorAdjust colorAdjust = new ColorAdjust();
    colorAdjust.setHue((mycolor.getHue()/360));
    colorAdjust.setSaturation(mycolor.getSaturation());
    colorAdjust.setBrightness(mycolor.getBrightness()-1);
   
    imageV.setEffect(colorAdjust);
}

我期望图像上的颜色与我从 ColorPicker 中选择的颜色相同。但 T 恤上的颜色不准确。

最佳答案

如何从颜色选择器为图像着色。

更多信息请参见:

示例

将使用的图像(我使用了您问题中提供的图像)放置在与放置示例代码的包具有相同目录结构的资源文件夹中。

purple

orange

import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.effect.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.util.Objects;

public class TShirtDesigner extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        ColorPicker colorPicker = new ColorPicker();

        Image tshirtImage = new Image(
                Objects.requireNonNull(
                        TShirtDesigner.class.getResource(
                                "tshirt.png"
                        )
                ).toExternalForm()
        );

        ImageView tshirt = new ImageView(tshirtImage);
        tshirt.setClip(new ImageView(tshirtImage));

        bindImageColor(
                tshirt,
                colorPicker.valueProperty()
        );

        VBox layout = new VBox(
                10,
                colorPicker,
                tshirt
        );
        layout.setAlignment(Pos.CENTER);
        layout.setPadding(new Insets(10));

        Scene scene = new Scene(layout);
        stage.setScene(scene);
        stage.show();
    }

    private static void bindImageColor(
            ImageView imageView,
            ObjectProperty<Color> colorProperty
    ) {
        ColorInput colorInput = new ColorInput(
                0,
                0,
                imageView.getImage().getWidth(),
                imageView.getImage().getHeight(),
                colorProperty.get()
        );
        colorInput.paintProperty().bind(
                colorProperty
        );

        ColorAdjust monochrome = new ColorAdjust();
        monochrome.setSaturation(-1.0);

        Blend colorBlend = new Blend(
                BlendMode.MULTIPLY,
                monochrome,
                colorInput
        );

        imageView.setEffect(colorBlend);
    }

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

关于java - 在 JavaFX 中的图像上使用 ColorPicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76626020/

相关文章:

css - 如何告诉图像保持居中 - CSS

php - 数据 url 图片另存为 png

java - 查看fxml里面的逻辑(特别是迭代)

javafx-2 - 如何在 JavaFX 中列出所有颜色

java - 如何更改javafx图表的 Axis 颜色?

java - 如何将数组列表值返回到单独的数组中?

java - 将数据从选项卡式 Activity 中的 fragment 发送到 fragment

java - 在 Spring RestTemplate 和 ResponseEntity 中设置状态码 301 或 303

java - TestFX中有测试选择框选择的功能吗?

php - Facebook API 发布 : How to get larger thumbnail?