【问题标题】:Smooth BufferImage edges平滑 BufferImage 边缘
【发布时间】:2015-03-17 16:56:47
【问题描述】:

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

测试图像的放大视图:

(请注意,这不是实际使用的 BufferedImage,仅用于演示)。

已使用双线性插值、质量渲染和抗锯齿 RenderHints,但抗锯齿似乎仅适用于 Java 绘制的形状。显然,白色背景和形状的黑色边缘并没有像灰色和黑色的插值那样混合在一起。

我想要的可以通过图像周围的 1px 透明边框来实现,并让插值来完成工作,但这感觉是多余的。没有更好的方法吗?

【问题讨论】:

  • 其实,我也喜欢你的 1 像素透明边框技巧。 :-) 我相信大图像需要相当多的内存((w+2) * (h+2) * pixelSize字节)。 Chris Campbell 的方法也可能更快,但我还没有测试过。

标签: java bufferedimage java-2d


【解决方案1】:

是的,这是一个已知问题,但可以使用我曾经在 a blog post by Chris Campbell 中发现的一个巧妙技巧来解决这个问题:

确实,在使用 Graphics.drawImage() 进行渲染时,Sun 的 Java 2D 实现不会自动消除图像边缘的锯齿。但是,有一个简单的解决方法:使用 TexturePaint 并渲染一个转换/抗锯齿的 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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多