平滑 BufferImage 边缘
Smooth BufferImage edges
有没有办法平滑转换(平移和旋转)BufferedImage 的锯齿状边缘?
测试图像的放大视图:
(注意这不是实际使用的BufferedImage,这里只是为了演示)
已使用双线性插值、质量渲染和抗锯齿 RenderHints,但抗锯齿似乎仅适用于 Java 绘制的形状。很明显,白色背景和形状的黑色边缘并没有像灰色和黑色的插值那样混合在一起。
我想要的可以通过图像周围的 1px 透明边框实现,并让插值完成工作,但这感觉多余。没有更好的方法吗?
是的,这是一个已知问题,但可以使用我曾经在 a blog post by Chris Campbell:
中找到的一个巧妙技巧来解决这个问题
It is true that Sun's Java 2D implementation does not automatically antialias image edges when rendering with Graphics.drawImage(). However, there is a simple workaround: use TexturePaint and render a transformed/antialiased fillRect().
这是我使用的代码,改编自他博客中的代码:
// For multiples of 90 degrees, use the much faster drawImage approach
boolean fast = ((Math.abs(Math.toDegrees(angle)) % 90) == 0.0);
int w = source.getWidth();
int h = source.getHeight();
// Compute new width and height
double sin = Math.abs(Math.sin(angle));
double cos = Math.abs(Math.cos(angle));
int newW = (int) Math.floor(w * cos + h * sin);
int newH = (int) Math.floor(h * cos + w * sin);
// Create destination image for painting onto
BufferedImage dest = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
// Set up transformation around center
AffineTransform transform = AffineTransform.getTranslateInstance((newW - w) / 2.0, (newH - h) / 2.0);
transform.rotate(angle, w / 2.0, h / 2.0);
Graphics2D g = dest.createGraphics();
try {
g.transform(transform);
if (!fast) {
// Max quality
g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Here's the trick:
g.setPaint(new TexturePaint(source,
new Rectangle2D.Float(0, 0, source.getWidth(), source.getHeight())));
g.fillRect(0, 0, source.getWidth(), source.getHeight());
}
else {
// Multiple of 90 degrees:
g.drawImage(source, 0, 0, null);
}
}
finally {
g.dispose();
}
有没有办法平滑转换(平移和旋转)BufferedImage 的锯齿状边缘?
测试图像的放大视图:
已使用双线性插值、质量渲染和抗锯齿 RenderHints,但抗锯齿似乎仅适用于 Java 绘制的形状。很明显,白色背景和形状的黑色边缘并没有像灰色和黑色的插值那样混合在一起。
我想要的可以通过图像周围的 1px 透明边框实现,并让插值完成工作,但这感觉多余。没有更好的方法吗?
是的,这是一个已知问题,但可以使用我曾经在 a blog post by Chris Campbell:
中找到的一个巧妙技巧来解决这个问题It is true that Sun's Java 2D implementation does not automatically antialias image edges when rendering with Graphics.drawImage(). However, there is a simple workaround: use TexturePaint and render a transformed/antialiased fillRect().
这是我使用的代码,改编自他博客中的代码:
// For multiples of 90 degrees, use the much faster drawImage approach
boolean fast = ((Math.abs(Math.toDegrees(angle)) % 90) == 0.0);
int w = source.getWidth();
int h = source.getHeight();
// Compute new width and height
double sin = Math.abs(Math.sin(angle));
double cos = Math.abs(Math.cos(angle));
int newW = (int) Math.floor(w * cos + h * sin);
int newH = (int) Math.floor(h * cos + w * sin);
// Create destination image for painting onto
BufferedImage dest = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
// Set up transformation around center
AffineTransform transform = AffineTransform.getTranslateInstance((newW - w) / 2.0, (newH - h) / 2.0);
transform.rotate(angle, w / 2.0, h / 2.0);
Graphics2D g = dest.createGraphics();
try {
g.transform(transform);
if (!fast) {
// Max quality
g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Here's the trick:
g.setPaint(new TexturePaint(source,
new Rectangle2D.Float(0, 0, source.getWidth(), source.getHeight())));
g.fillRect(0, 0, source.getWidth(), source.getHeight());
}
else {
// Multiple of 90 degrees:
g.drawImage(source, 0, 0, null);
}
}
finally {
g.dispose();
}