java - 平滑 BufferImage 边缘

标签 java bufferedimage java-2d

有没有办法平滑转换(平移和旋转)的 BufferedImage 的锯齿状边缘?

测试图像的放大 View :

Zoomed in view of the jagged edges (请注意,这不是将要使用的实际 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();
}

关于java - 平滑 BufferImage 边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29105154/

相关文章:

java - 如何在 Spring 中显式包含要 Autowiring 的组件?

java - BufferedImage 的 Changellistener

java - 带有图像的 fillRect 方法

java - JPanel 和 BufferedImage

java - 覆盖所有其他组件(Swing、Java)

java - 使用 Hibernate 自定义 double[] UserType

java - 将 int[] 转换为 bytes[] Java

java - 循环中数组的使用

java - 将 Swing 屏幕传递给另一个 Java 应用程序。最小化时 Swing 会更新屏幕吗?

java - 将 JavaFX 图像转换为 BufferedImage