java - 如何基于两点旋转图像

标签 java image image-rotation

我处理了一些图像,在这些图像中我总是有两个点 (x1, y1) 和 (x2, y2)。像这样:

|----------------|
|                |
|    .           |
|                |
|          .     |
|                |
|----------------|

我需要编写一个算法来像这样对齐图像

|----------------|
|                |
|                |
|    .     .     |
|                |
|                |
|----------------|

我已经阅读了this问题但是得到的角度

double angle = Math.Atan2(pointB.Y - pointA.Y, pointB.X - pointA.X);

当我在 java 中使用这个旋转代码时不工作:

public static BufferedImage tilt(BufferedImage image, double angle) {
    double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
    int w = image.getWidth(), h = image.getHeight();
    int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math
            .floor(h * cos + w * sin);
    GraphicsConfiguration gc = getDefaultConfiguration();
    BufferedImage result = gc.createCompatibleImage(neww, newh,
            Transparency.OPAQUE);
    Graphics2D g = result.createGraphics();

    g.setColor(Color.white);
    g.setBackground(Color.white);
    g.fillRect(0, 0, neww, newh);

    g.translate((neww - w) / 2, (newh - h) / 2);

    g.rotate(angle, w / 2, h / 2);
    g.drawRenderedImage(image, null);
    g.dispose();
    return result;
}

在他们之前提到的帖子中,他们使用了像这样的c#代码

myImage.TranslateTransform(-pointA.X, -pointA.Y);
myImage.RotateTransform((float) angle, MatrixOrder.Append);
myImage.TranslateTransform(pointA.X, pointA.Y, MatrixOrder.Append);

有没有人可以帮助实现这种情况的 java 实现?

最佳答案

好的...为了解决这个问题,我只是将 Math.atan2 返回的弧度值转换为度数,旋转效果很好。

谢谢大家

关于java - 如何基于两点旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5582755/

相关文章:

javascript - 请解释Javascript图像旋转代码

java - 在 Android UI 中旋转和显示图像的最有效方法是什么?

javascript - 旋转后平移缓冲区

java - 如何实现通用接口(interface)列表

java - VisualVM 无​​法在 Windows 10 上运行 : jpswin2

javascript - 使用 Javascript 和 Jquery 正确的 Img Src 路径

java - 在android上实现自动颜色(photoshop工具)

Java Reflect 2 对象

java - AChartEngine getIntent() 显示点值

python - Web2py:下载文件/显示图像