Java: 缓冲图像围绕其中心的旋转是倾斜的

Java: Rotation of a buffered image around its centre is skewed

我正在尝试使用此处的帮助围绕其中心旋转 Java 中的缓冲图像(地图上的平面图标): Rotating BufferedImage instances

当我使用这段代码时:

AffineTransform at = new AffineTransform();

at.rotate(Math.toRadians(planeHeading),origImage.getWidth() / 2, origImage.getHeight() / 2);

AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);

origImage = op.filter(origImage, null);

g.drawImage(origImage, x-origImage.getWidth() / 2, y-origImage.getHeight() / 2, null);

旋转 180-270 度时,图片位置偏高,偏左一点:

如果我使用此代码:

AffineTransform at = new AffineTransform();

at.translate(x, y);

at.rotate(Math.toRadians(planeHeading));

at.translate(-origImage.getWidth()/2, -origImage.getHeight()/2);

g.drawImage(origImage, at, null);

图像已正确旋转,但图像本身的边缘像素化程度很高。

有人可以帮忙找到罪魁祸首吗?

这是整个方法:

@Override
public void paintWaypoint(Graphics2D g, JXMapViewer viewer, MapPlane w)
{
    g = (Graphics2D)g.create();

    try
    {
        origImage = ImageIO.read(getClass().getResource("/images/map/mapPLANE.png"));

        Point2D point = viewer.getTileFactory().geoToPixel(w.getPosition(), viewer.getZoom());

        // Center coordinates
        int x = (int)point.getX();
        int y = (int)point.getY();

        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // Get heading of the plane and rotate the image
        String planeHeadingStr = w.getHeading();

        try
        {
            double planeHeading = Double.parseDouble(planeHeadingStr);

            AffineTransform at = new AffineTransform();

            //Do the actual rotation
            at.rotate(Math.toRadians(planeHeading),origImage.getWidth() / 2, origImage.getHeight() / 2);
            AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
            origImage = op.filter(origImage, null);

            // Draw the image
            g.drawImage(origImage, x-origImage.getWidth() / 2, y-origImage.getHeight() / 2, null);

        }
        catch(NumberFormatException e)
        {

        }


        g.dispose();

    }
    catch (Exception ex)
    {
        log.warn("couldn't read mapPLANE.png", ex);
    }

}

非常感谢!

要在第二种情况下使用 AffineTransform 直接绘制,要实现与 AffineTransformOp 相同的双线性插值,您应该设置另一个 RenderingHint:

g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
                   RenderingHints.VALUE_INTERPOLATION_BILINEAR);

否则,在您的情况下,它默认为 NEAREST_NEIGHBOUR 插值。