【发布时间】:2011-04-07 14:31:55
【问题描述】:
我处理了一些图像,在这些图像中我总是有两个点 (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 实现?
【问题讨论】:
-
当你说不工作时,到底发生了什么。我使用完全相同的代码(忘记了它来自哪里),非常成功。
-
我的意思是,旋转没有正确对齐图像。我的一张图片需要旋转 19 度,但 Math.Atan2(pointB.Y - pointA.Y, pointB.X - pointA.X);返回类似 2 的东西
标签: java image image-rotation