在JavaFX中使用ColorPicker处理图像。

huangapple go评论65阅读模式
英文:

Use of ColorPicker on an image in JavaFX

问题

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

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

GUI

在JavaFX中使用ColorPicker处理图像。

T恤

在JavaFX中使用ColorPicker处理图像。

@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恤上的颜色不准确。


<details>
<summary>英文:</summary>

I have an image in the imageview. What I want to do is add color to it. Basically it&#39;s like a layer of color on the image. So I am trying to add color to a t-shirt image using the ColorPicker and I don&#39;t know how I can do that. I&#39;ve tried it along with the colorAdjust class but its giving me a different color on the tshirt than the one I&#39;ve chosen from the ColorPicker. 

I am using SceneBuilder, btw.


GUI

[![GUI][1]][1]

T-shirt

[![T-shirt][2]][2]

    @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(&quot;tshirt7.PNG&quot;));
        imageV.setImage(img);
        ColorAdjust colorAdjust = new ColorAdjust();
        colorAdjust.setHue((mycolor.getHue()/360));
        colorAdjust.setSaturation(mycolor.getSaturation());
        colorAdjust.setBrightness(mycolor.getBrightness()-1);
       
        imageV.setEffect(colorAdjust);
    }

I was expecting the same color on the image as the one I&#39;ve chosen from ColorPicker. But the color on the tshirt is not accurate.


  [1]: https://i.stack.imgur.com/yUNnY.png
  [2]: https://i.stack.imgur.com/UaZEl.png

</details>


# 答案1
**得分**: 2

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

* 确保您的`png`输入图像具有适当的透明颜色区域(您的示例T恤图像具有)。
* 定义一个[ColorPicker][1]来选择您的颜色。
* 将[选择的颜色][2]绑定到一个[多重混合][3]的[ColorInput][5]效果上,该效果作用于一个[去饱和][6]的单色[调整][7]或[灰度化][8]图像,图像由图像定义的剪辑所裁剪。

更多信息请参见:

* https://stackoverflow.com/questions/18124364/how-to-change-color-of-image-in-javafx/18124868#18124868

**示例**

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

![紫色][10]

![橙色][11]

```java
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);
    }
}
英文:

How to color an image from a color picker.

For more info see:

Example

Place the image used (I used the image provided in your question) in a resource folder with the same directory structure as the package you place the sample code in.

在JavaFX中使用ColorPicker处理图像。

在JavaFX中使用ColorPicker处理图像。

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(
                                &quot;tshirt.png&quot;
                        )
                ).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&lt;Color&gt; 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);
    }
}

huangapple
  • 本文由 发表于 2023年7月6日 14:21:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定