I have an image and I have to rotate it by 45, 90, 135, 180 degrees. What I am doing:
try {
BufferedImage src = ImageIO.read(new File("src.png"));
double ang = Math.toRadians(90);
AffineTransform t = new AffineTransform();
t.setToRotation(ang, src.getWidth() / 2, src.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(t, null);
BufferedImage dst = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
op.filter(src, dst);
ImageIO.write(dst, "png", new File("output.png"));
} catch(Exception ex) { ex.printStackTrace();
}
The problem is that image changes its position and gets out of bounds of destination image:
I've googled this and found the solution in this question: AffineTransform truncates image, what do I wrong? But I quite don't understand it and it works only for quadrants. I've tried to multiply twice width and height of destination, but it failed:
How to fix this? The destination image shouldn't have any extra (except required for diagonal rotation) whitespace or truncated area. Angle problems (0 == 180 or is it clockwise) aren't important.
Thanks for any help.