如何在没有模糊/别名的情况下将图像绘制到JavaFX Canvas中? [英] How can I draw images into JavaFX Canvas without blur/aliasing in a given area?

查看:309
本文介绍了如何在没有模糊/别名的情况下将图像绘制到JavaFX Canvas中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JavaFX Canvas 中绘制图像。但是, GraphicsContext 的传统 drawImage()方法似乎会产生某种模糊或混淆的结果。可能是因为我使用的是Retina MacBook Pro。

I am trying to draw an image in a JavaFX Canvas. However, the conventional drawImage() method of GraphicsContext seems to produce some sort of blurred or aliased results. Probably because I am using a Retina MacBook Pro.

我在这里找到了一个解决方案: https://stackoverflow.com/a/26706028/555690

I found a solution here: https://stackoverflow.com/a/26706028/555690

public class ImageRenderer {
   public void render(GraphicsContext context, Image image, int sx, int sy, int sw, int sh, int tx, int ty) {
      PixelReader reader = image.getPixelReader();
      PixelWriter writer = context.getPixelWriter();
      for (int x = 0; x < sw; x++) {
         for (int y = 0; y < sh; y++) {
            Color color = reader.getColor(sx + x, sy + y);
            if (color.isOpaque()) {
              writer.setColor(tx + x, ty + y, color);
            }
         }
      }
   }
}

它有效,但有时我需要绘制具有特定大小的目标rect(所以我需要为 int tw 添加参数int ),但我不知道如何调整上述方法的逻辑来使用它们。

It works, but sometimes I need to draw the target rect with a particular size (so I need to add parameters for int tw and int th), but I don't know how to adjust the logic of the above method to make use of them.

我怎么能将图像绘制到JavaFX Canvas ,在给定区域内没有模糊/锯齿?

How can I draw images into JavaFX Canvas without blur/aliasing in a given area?

推荐答案

<我也有一台MacBook Pro Retina,我遇到了同样的问题,但如果你做得对,你在画布上绘制的图像并不模糊。

I also have a MacBook Pro Retina and I had the same problem but if you do it right, the images that you draw on a canvas are not blurred.

什么你必须要记住的是Retina Mac的像素比例因子为2.因此,如果你要求画布说1000x1000,你会得到一个尺寸为1000x1000光栅单位或虚拟像素的画布,但在内部它支持通过2000x2000像素的图像。现在假设您有一个大小为400x400像素的图像,并且您希望在画布的左上角显示该图像而不会模糊。然后,您必须按以下方式使用drawImage函数:

What you have to keep in mind is that the Retina Mac has a pixel scale factor of 2. So if you ask for a canvas of say 1000x1000 you will get a canvas which has a size of 1000x1000 raster units or virtual pixels but internally it is backed by an image of 2000x2000 pixels. Now assume you have an image with a size of 400x400 pixels and you want to display that in the upper left corner of your canvas without blurring. You then have to use the drawImage function in the following way:

gc.drawImage(img, 0, 0, 400, 400, 0, 0, 200, 200);

另一种情况可能是你想用画面填充画布的上四分之一面积为500x500光栅单位。这意味着您必须提供1000x1000像素的图像并将其放置在

Another scenario might be that you want to fill the upper quarter of the canvas with an image which is an area of 500x500 raster units. That means you will have to provide an image with 1000x1000 pixels and place it at

gc.drawImage(img, 0, 0, 1000, 1000, 0, 0, 500, 500);

我希望这有助于理解这个问题。

I hope this helps to understand the issue.

这篇关于如何在没有模糊/别名的情况下将图像绘制到JavaFX Canvas中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆