JavaFX:禁用Canvas对象上的图像平滑 [英] JavaFX: Disable image smoothing on Canvas object
问题描述
我使用JavaFX在桌面上使用sprite编辑器。
我试图实现缩放功能,但我遇到了一个问题:我无法弄清楚如何在 Canvas
对象上禁用图片平滑。
我每个教程实现<$ c>调用 Canvas.setScaleX()
和 Canvas.setScaleY()
$ c> Canvas 缩放。但是放大后,我的图片模糊。
我有一些测试代码,并且有一个隐藏的 Canvas
a href =http://stackoverflow.com/questions/16209512/is-there-any-javafx-image-editor>(这里是一个相关问题的链接,但没有解决我的特定问题)
我对模糊问题也有同样的问题。
在我的情况下,我的电脑有Retina显示。视网膜显示使得用子像素呈现像素。当将图像绘制到画布时,将对子像素使用抗锯齿绘制图像。我还没有找到一种方法来防止这种抗锯齿的发生(虽然可能与其他画布技术,如 HTML5's Canvas )
在此期间,我有一个解决方案(虽然我关心性能):
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 color color = reader.getColor + x,sy + y);
if(color.isOpaque()){
writer.setColor(tx + x,ty + y,color);
}
}
}
}
}
$ b b
PixelWriter绕过在绘制图片时发生的消除锯齿。
I'm making a sprite editor using JavaFX for use on desktops.
I'm attempting to implement zooming functionality, but I've run into a problem: I can't figure out how to disable image smoothing on a Canvas
object.
I'm calling Canvas.setScaleX()
and Canvas.setScaleY()
as per every tutorial implementing Canvas
zooming. But my image appears blurred when zoomed in.
I have some test code here to demonstrate.
As this is a sprite editor, it's important for me to have crisp edges to work with. The alternative to fixing image smoothing on the Canvas
is to have a non-smoothing ImageView
, and have a hidden Canvas
to draw on, which I would rather avoid.
Help is appreciated.
(here's a link to a related question, but doesn't address my particular problem)
I was having the same issue with the blurring.
In my case, my computer has Retina Display. Retina Display causes a pixel to be rendered with sub-pixels. When drawing images to the canvas, the image would be drawn with antialiasing for the sub-pixels. I have not found a way to prevent this antialiasing from occurring (although it is possible with other canvas technologies such as HTML5's Canvas)
In the meantime, I have a work-around (albeit I'm concerned about performance):
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);
}
}
}
}
}
The PixelWriter bypasses the anti-aliasing that occurs when drawing the image.
这篇关于JavaFX:禁用Canvas对象上的图像平滑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!