操作する画像がいくつかありますが、これらの画像には常に2つのポイント(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の実装を手伝ってくれる人はいますか?