我有一些我处理的图像,在这些图像中我总是有两个点(x1,y1)和(x2,y2)。像这样:
|----------------|
| |
| . |
| |
| . |
| |
|----------------|
我需要编写一个算法来像这样对齐图像
|----------------|
| |
| |
| . . |
| |
| |
|----------------|
我已经读过这个问题,但获得的角度
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实现?