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

时间:2018-06-12 02:41:35

标签: java rotation bufferedimage

我正在尝试使用此处的帮助在其中心周围旋转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度时,图像位于其中心左侧较高位置:

enter image description here

如果我使用此代码:

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);

图像旋转正确,但图像本身的边缘会非常像素化。

enter image description here

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

这是整个方法:

@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);
    }

}

非常感谢!

1 个答案:

答案 0 :(得分:3)

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

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

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