加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 02:24:52 所属栏目:Java 来源:网络整理
导读:我有一些我操纵的图像,在这些图像中我总是有两个点(x1,y1)和(x2,y2).像这样: |----------------|| || . || || . || ||----------------| 我需要编写一个algorythm代码来对齐图像 |----------------|| || || . . || || ||----------------| 我已经阅读了this
我有一些我操纵的图像,在这些图像中我总是有两个点(x1,y1)和(x2,y2).像这样:

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

我需要编写一个algorythm代码来对齐图像

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

我已经阅读了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,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返回的弧度值转换为度数,旋转效果很好.

大家好

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读