【问题标题】:Java: Rotation of a buffered image around its centre is skewedJava:缓冲图像围绕其中心的旋转倾斜
【发布时间】:2018-06-12 02:41:34
【问题描述】:

我正在尝试使用此处的帮助围绕其中心旋转 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);
    }

}

非常感谢!

【问题讨论】:

    标签: java rotation bufferedimage


    【解决方案1】:

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

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

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

    【讨论】:

    • 这很棒。谢谢!所以我明白了,第一种情况有什么问题?最好的!
    • 很难理解这一点(双关语)。我假设图像是正方形的是否正确?
    • 是的。 40 像素 x 40 像素。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 2018-04-26
    相关资源
    最近更新 更多