【问题标题】:Trying two different methods to transform a picture - JavaFX尝试两种不同的方法来转换图片 - JavaFX
【发布时间】:2017-05-25 15:22:52
【问题描述】:

所以我还在学习 Java。现在正在学习JavaFX。

我有一张树的照片。我想尝试两种不同的方法。我使用的第一种方法是使用一元运算符将图像颜色变为灰色。

现在我想尝试第二种方法,使用我制作的ColourTransformer 接口,以获得一个 10 像素宽的灰色框来替换图像边框上的像素。

这就是我所做的。对于第二种方法,我不太确定如何指定像素。有什么建议吗?

这就是我所做的

public class ColourFilter extends Application {

   //Using Unary Operator to transform image to grayscale - Method 1
   public static Image transform(Image in, UnaryOperator<Color> f) {
      int width = (int) in.getWidth();
      int height = (int) in.getHeight();
      WritableImage out = new WritableImage(
              width, height);
      for (int x = 0; x < width; x++)
         for (int y = 0; y < height; y++)
            out.getPixelWriter().setColor(x, y,
                    f.apply(in.getPixelReader().getColor(x, y)));
      return out;
   }
   public static <T> UnaryOperator<T> compose(UnaryOperator<T> op1, UnaryOperator<T> op2) {
      return t -> op2.apply(op1.apply(t));
   }

   //Using ColourTransformer interface to get 10 pixel wide gray frame replacing the pixels on the border of an image - Method 2
   public static Image transform(Image in, ColourTransformer f) {
      int width = (int) in.getWidth();
      int height = (int) in.getHeight();
      WritableImage out = new WritableImage(
              width, height);
      for (int x = 0; x < width; x++)
         for (int y = 0; y < height; y++)
            out.getPixelWriter().setColor(x, y, f.apply(x, y, in.getPixelReader().getColor(x, y)));
      return out;
   }
   @FunctionalInterface
   interface ColourTransformer {
      Color apply(int x, int y, Color colorAtXY);
   }

   public void start(Stage stage) {
      Image image = new Image("amazing-trees.jpg");
      Image image2 = transform(image, Color::brighter);
      Image image3 = transform(image2, Color::grayscale);
      // alternative to two previous image transforms -- composition
      //Image image3 = transform(image, compose(Color::brighter, Color::grayscale));
      stage.setScene(new Scene(new VBox(
              new ImageView(image),
              // new ImageView(image2),
              new ImageView(image3))));
      stage.show();
   }
}

【问题讨论】:

  • 那不会编译,是吗?在transform(Image, ColourTransformer) 中你不需要f.apply(x, y, in.getPixelReader().getColor(x, y)) 吗?你能澄清一下问题是什么吗?
  • 当我添加第二种方法时它没有编译。我制作的第一种方法工作正常。所以第二种方法的目的是使用ColourTransformer接口得到10像素宽的灰框替换图像边框上的像素。
  • 编译错误是什么?我上面建议的更改不是解决了吗? (顺便说一句,您不认为包含诸如“它无法编译”之类的信息以及您遇到的编译错误会使某人更有可能回答这个问题吗……?)跨度>
  • 哦,是的,注意到了。嗯,您提出的建议在第二种方法中一直存在。我会更新错误消息。
  • 不,我提出的建议不存在。你有f.apply(in.getPixelReader().getColor(x, y))。我认为你需要f.apply(x, y, in.getPixelReader().getColor(x, y))。 (这正是错误消息告诉你的。)

标签: java javafx transform


【解决方案1】:

正如编译错误消息所说,您正在尝试调用

f.apply(Color);

其中fColourTransformer:但是您在ColorTransformer 中使用三个参数定义了apply 方法:apply(int, int, Color)

你需要更换

f.apply(in.getPixelReader().getColor(x, y))

f.apply(x, y, in.getPixelReader().getColor(x, y))

public static Image transform(Image in, ColourTransformer f) {
    int width = (int) in.getWidth();
    int height = (int) in.getHeight();
    WritableImage out = new WritableImage(
          width, height);
    for (int x = 0; x < width; x++)
        for (int y = 0; y < height; y++)
            out.getPixelWriter().setColor(x, y, f.apply(x, y, in.getPixelReader().getColor(x, y)));
    return out;

}

您可以通过以下方式获得 10 像素的灰色边框:

Image image = new Image("amazing-trees.jpg");
int width = (int) image.getWidth();
int height = (int) image.getHeight();
Image framedImage = transform(image, (x, y, color) -> {
    if (x < 10 || y < 10 || width - x < 10 || height - y < 10) {
        return Color.GRAY ;
    } else return color ;
});

【讨论】:

  • 好的,那么我该如何指定它来获得一个 10 像素宽的灰色框来替换图像边框上的像素?
猜你喜欢
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 2020-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多