【问题标题】:Scaled "straight line" image jumps out of line缩放的“直线”图像跳出线
【发布时间】:2019-11-16 16:39:32
【问题描述】:

a 在放大图像时遇到问题。我有一个看起来像这样的 png 文件: raw png

图片为 8px * 8px,上面有一些红色的直线。 但是当我用 java 绘制这个图像并放大它时,会发生这种情况:java image

您几乎可以看到,这条线并不完全笔直。它总是关闭一个像素,并形成这种波浪形。如果图像在框架上的其他地方绘制,则“波浪”在其他地方。图像旋转了 90°,但我在没有旋转的情况下对其进行了测试,它仍然存在。除此之外,我确实需要旋转图像。

有没有办法解决这个问题?我在同一个 Graphics2D 对象中启用了文本抗锯齿功能。是否还有某种反锯齿功能?

代码

private void loadImage(String path, int field, int imageNumber) {
    BufferedImage image;
    image = new Resource().readImg(path);
    images[field][imageNumber][0] = image;
    images[field][imageNumber][1] = rotateClockwise90(image);
    images[field][imageNumber][2] = rotateClockwise90(rotateClockwise90(image));
    images[field][imageNumber][3] = rotateClockwise90(rotateClockwise90(rotateClockwise90(image)));
}

private BufferedImage rotateClockwise90(BufferedImage src) {
    int width = src.getWidth();
    int height = src.getHeight();

    BufferedImage dest = new BufferedImage(height, width, src.getType());

    Graphics2D graphics2D = dest.createGraphics();
    graphics2D.translate((height - width) / 2, (height - width) / 2);
    graphics2D.rotate(Math.PI / 2, height / 2, width / 2);
    graphics2D.drawRenderedImage(src, null);

    return dest;
}

当程序启动时,我会在所有 4 个方向上旋转图像,因此我不必在程序运行时一遍又一遍地执行此操作。

public BufferedImage getTile(int type, int part, int rotation) {
    return images[type][part][rotation];
}

然后我要做的就是调用这个get方法并绘制图像:

g2d.drawImage(tiles.getShipTile(type, part, rotation), x, y, null);

【问题讨论】:

  • 我不知道 png 图像、渲染形状和线条等的任何抗锯齿功能,是的,但不适用于图像。
  • 也许你可以给我们你的代码,这样我们就可以看看你到底做了什么。

标签: java image rendering antialiasing


【解决方案1】:

我实际上找到了一种方法来避免这些奇怪的像素,但是这种方法会使图像有点模糊。 而不是使用

g2d.drawImage(img, x, y, width, height, null);

你可以简单地使用

g2d.drawImage(img.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING), x, y, null);

它基本上做同样的事情,但我放大它使用这个平滑制作键。

我试过了,发现它不是很舒服,因为它滞后很多。

所以我只是在开始旋转图像时将其放大。

正如我所说,这种方法有点模糊,但如果没有其他方法可以避免这个问题,我必须使用它。你几乎看不到模糊,所以这对我来说是一个选择。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-20
    • 2023-03-05
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 2023-04-02
    相关资源
    最近更新 更多