I have some images that I control, and on these images I always have two points (x1, y1) and (x2, y2). eg:
|----------------| | | | . | | | | . | | | |----------------|
I need to code an algorithm to align the image as follows
|----------------| | | | | | . . | | | | | |----------------|
I already read this question, but the angle obtained using
double angle = Math.Atan2(pointB.Y - pointA.Y, pointB.X - pointA.X);
Doesn't work when I use this rotation code in 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; }
In the message mentioned above, they use C # code, for example
myImage.TranslateTransform(-pointA.X, -pointA.Y); myImage.RotateTransform((float) angle, MatrixOrder.Append); myImage.TranslateTransform(pointA.X, pointA.Y, MatrixOrder.Append);
Is there anyone who can help with the java implementation for this case?