gpt4 book ai didi

javafx - 如何在javafx中禁用 Canvas 上drawImage的线性过滤

转载 作者:行者123 更新时间:2023-12-04 14:25:55 26 4
gpt4 key购买 nike

我正在尝试在 javafx 的 Canvas 上绘制缩放图像。使用此代码:

Image image = ...;
canvas.setWidth(scale * width);
canvas.setHeight(scale * height);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.drawImage(image, 0, 0, scale * width, scale * height);
// this gives same result
// gc.scale(scale, scale);
// gc.drawImage(editableImage, 0, 0, width, height);

它的工作速度非常快,但会产生像这样的模糊图像:

enter image description here

这不是我想看到的。相反,我想得到这张照片:

enter image description here

可以通过使用以下代码手动设置每个像素颜色来绘制:
PixelReader reader = image.getPixelReader();
PixelWriter writer = gc.getPixelWriter();
for (int y = 0; y < scale * height; ++y) {
for (int x = 0; x < scale * width; ++x) {
writer.setArgb(x, y, reader.getArgb(x / scale, y / scale));
}
}

但是我不能使用这种方法,因为它太慢了。绘制 8 倍缩放的 1Kb 图像需要几秒钟。所以我问是否有任何方法可以禁用在 Canvas 上绘制的这种模糊效果?

最佳答案

UPD 10/07/2019:
看起来问题是 fixed !现在 GraphicsContext 应该有属性 "image smoothing"控制这种行为。
初始答复
我想我已经找到了我的问题的答案。如 this issue说没有办法在图形上下文中指定过滤选项。
描述:

When drawing an image in a GraphicsContext using the drawImage()method to enlarge a small image to a larger canvas, the image is beinginterpolated (possibly using a bilinear or bicubic algorithm). Butthere are times like when rendering color maps (temperature,zooplancton, salinity, etc.) or some geographical data (populationconcentration, etc.) where we want to have no interpolation at all(ie: use the nearest neighbor algorithm instead) in order to representaccurate data and shapes.

In Java2D, this is possible by setting the appropriateRenderingHints.KEY_RENDERING on the Graphics2D at hand. Currently onJavaFX's GraphicsContext there is no such way to specify how the imageis to be interpolated.

The same applies when shrinking images too.

This could be expanded to support a better form of smoothing for the"smooth" value that is available in both Image and ImageView and thatdoes not seem to work very well currently (at least on Windows).


这个问题是在 2013 年创建的,但它仍然没有受到影响,所以不太可能很快得到解决。

关于javafx - 如何在javafx中禁用 Canvas 上drawImage的线性过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44445072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com