Image processing 如何在JavaFX中更改图像的颜色

Image processing 如何在JavaFX中更改图像的颜色,image-processing,javafx-2,Image Processing,Javafx 2,我有一个PNG图像,如下所示: 我想将图像更改为以下内容: 在JavaFX中如何实现这一点?因为您不关心它是矢量形状还是位图,所以我将在这里使用位图概述解决方案。如果你真的想要一个向量形状,我相信你需要使用向量输入来获得一个好的结果 在亮度设置为最小值(-1)的情况下使用颜色调整效果。 缓存结果以提高速度 以下是创建图像阴影轮廓的示例: import javafx.application.Application; import javafx.scene.*; import javafx.

我有一个PNG图像,如下所示:

我想将图像更改为以下内容:


在JavaFX中如何实现这一点?

因为您不关心它是矢量形状还是位图,所以我将在这里使用位图概述解决方案。如果你真的想要一个向量形状,我相信你需要使用向量输入来获得一个好的结果


在亮度设置为最小值(-1)的情况下使用颜色调整效果。 缓存结果以提高速度

以下是创建图像阴影轮廓的示例:

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.*;
import javafx.stage.Stage;

public class Shadow extends Application {
    @Override 
    public void start(Stage stage) throws Exception {
        ImageView imageView = new ImageView(
            new Image(
                "http://i.stack.imgur.com/jbT1H.png"
            )
        );

        ColorAdjust blackout = new ColorAdjust();
        blackout.setBrightness(-1.0);

        imageView.setEffect(blackout);
        imageView.setCache(true);
        imageView.setCacheHint(CacheHint.SPEED);

        stage.setScene(new Scene(new Group(imageView)));
        stage.show();
    }

    public static void main(String[] args) {
        Application.launch();
    }
}
这是另一个调整图像颜色的示例,将鼠标悬停在smurfette上使她脸红


下面的代码将用另一种像素颜色替换一种像素颜色。如果在原始图像上多次运行该选项,则应将灰度值替换为颜色值。 为了节省内存,您可能需要修改代码,以便为每个循环重用可写映像

/**
   * reColor the given InputImage to the given color
   * inspired by https://stackoverflow.com/a/12945629/1497139
   * @param inputImage
   * @param oldColor
   * @param newColor 
   * @return reColored Image
   * 
   */
  public static Image reColor(Image inputImage, Color oldColor, Color newColor) {
    int W = (int) inputImage.getWidth();
    int H = (int) inputImage.getHeight();
    WritableImage outputImage = new WritableImage(W, H);
    PixelReader reader = inputImage.getPixelReader();
    PixelWriter writer = outputImage.getPixelWriter();
    int ob=(int) oldColor.getBlue()*255;
    int or=(int) oldColor.getRed()*255;
    int og=(int) oldColor.getGreen()*255;
    int nb=(int) newColor.getBlue()*255;
    int nr=(int) newColor.getRed()*255;
    int ng=(int) newColor.getGreen()*255;
    for (int y = 0; y < H; y++) {
      for (int x = 0; x < W; x++) {
        int argb = reader.getArgb(x, y);
        int a = (argb >> 24) & 0xFF;
        int r = (argb >> 16) & 0xFF;
        int g = (argb >>  8) & 0xFF;
        int b =  argb        & 0xFF;
        if (g==og && r==or && b==ob) {
          r=nr;
          g=ng;
          b=nb;
        }

        argb = (a << 24) | (r << 16) | (g << 8) | b;
        writer.setArgb(x, y, argb);
      }
    }
    return outputImage;
  }
/**
*将给定的输入图像恢复为给定的颜色
*灵感来自https://stackoverflow.com/a/12945629/1497139
*@param-inputImage
*@param oldColor
*@param newColor
*@return重着色图像
* 
*/
公共静态图像复原器(图像输入图像、颜色oldColor、颜色newColor){
int W=(int)inputImage.getWidth();
inth=(int)inputImage.getHeight();
WritableImage outputImage=新的可写映像(W,H);
PixelReader=inputImage.getPixelReader();
PixelWriter writer=outputImage.getPixelWriter();
int ob=(int)oldColor.getBlue()*255;
int或=(int)oldColor.getRed()*255;
int og=(int)oldColor.getGreen()*255;
int nb=(int)newColor.getBlue()*255;
int nr=(int)newColor.getRed()*255;
int ng=(int)newColor.getGreen()*255;
对于(int y=0;y>24)&0xFF;
int r=(argb>>16)和0xFF;
int g=(argb>>8)&0xFF;
int b=argb&0xFF;
如果(g==og&&r==或&&b==ob){
r=nr;
g=ng;
b=铌;
}

argb=(a你想要a还是a?好吧,任何更快的人,因为我想在我的游戏引擎中使用此功能。我想在运行时从图像创建形状。因此它需要快速方法。谢谢你的代码,但这并不完全是我想要的。我需要一个形状,然后我可以更改形状的颜色。我已经更新了我的问题。更新了答案包括颜色更改。提问时请更具体,谢谢:-)谢谢你的回答,我的主要问题是如何从图像创建形状。这是一个很好的解决方法,但我的主要问题仍然没有答案。我试图用这个解决方法解决我的问题。谢谢@Wolfgang Fahl,这满足了我的要求。+1谢谢-如果你也比较a参数,你也可以用这种方式更改不透明度。谢谢谢谢你让我知道。
/**
   * reColor the given InputImage to the given color
   * inspired by https://stackoverflow.com/a/12945629/1497139
   * @param inputImage
   * @param oldColor
   * @param newColor 
   * @return reColored Image
   * 
   */
  public static Image reColor(Image inputImage, Color oldColor, Color newColor) {
    int W = (int) inputImage.getWidth();
    int H = (int) inputImage.getHeight();
    WritableImage outputImage = new WritableImage(W, H);
    PixelReader reader = inputImage.getPixelReader();
    PixelWriter writer = outputImage.getPixelWriter();
    int ob=(int) oldColor.getBlue()*255;
    int or=(int) oldColor.getRed()*255;
    int og=(int) oldColor.getGreen()*255;
    int nb=(int) newColor.getBlue()*255;
    int nr=(int) newColor.getRed()*255;
    int ng=(int) newColor.getGreen()*255;
    for (int y = 0; y < H; y++) {
      for (int x = 0; x < W; x++) {
        int argb = reader.getArgb(x, y);
        int a = (argb >> 24) & 0xFF;
        int r = (argb >> 16) & 0xFF;
        int g = (argb >>  8) & 0xFF;
        int b =  argb        & 0xFF;
        if (g==og && r==or && b==ob) {
          r=nr;
          g=ng;
          b=nb;
        }

        argb = (a << 24) | (r << 16) | (g << 8) | b;
        writer.setArgb(x, y, argb);
      }
    }
    return outputImage;
  }