I need an algorithm to rotate an image in java. On the internet I found a promising algorithm in this link , and I gave a tidy up in it and adapted to use angles in degrees. It looks like this:
public static BufferedImage rotateImage(BufferedImage rotateImage, double angle) {
AffineTransform tx = new AffineTransform();
tx.rotate(Math.toRadians(angle), rotateImage.getWidth() / 2.0, rotateImage.getHeight() / 2.0);
double ytrans = tx.transform(new Point2D.Double(0.0, 0.0), null).getY();
double xtrans = tx.transform(new Point2D.Double(0, rotateImage.getHeight()), null).getX();
AffineTransform translationTransform = new AffineTransform();
translationTransform.translate(-xtrans, -ytrans);
tx.preConcatenate(translationTransform);
return new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR).filter(rotateImage, null);
}
However, this method does not work correctly for angles greater than 90 degrees (and probably for smaller than -90 too). I solved this with a game that depends on an intermediate image:
public static BufferedImage rotateImage(BufferedImage rotateImage, double angle) {
angle %= 360;
if (angle < 0) angle += 360;
int quadrants = (int) angle / 90;
double restAngle = angle % 90;
if (restAngle < 0) restAngle += 90;
AffineTransform tx = new AffineTransform();
tx.rotate(Math.toRadians(restAngle), rotateImage.getWidth() / 2.0, rotateImage.getHeight() / 2.0);
double ytrans = tx.transform(new Point2D.Double(0.0, 0.0), null).getY();
double xtrans = tx.transform(new Point2D.Double(0, rotateImage.getHeight()), null).getX();
AffineTransform translationTransform = new AffineTransform();
translationTransform.translate(-xtrans, -ytrans);
tx.preConcatenate(translationTransform);
BufferedImage b2 = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR).filter(rotateImage, null);
AffineTransform fix = AffineTransform.getQuadrantRotateInstance(
quadrants, b2.getWidth() / 2.0, b2.getHeight() / 2.0);
return new AffineTransformOp(fix, AffineTransformOp.TYPE_BILINEAR).filter(b2, null);
}
It worked. However, I know you can solve this without needing an intermediate image. I've already severed my head and I can not make it work better, I've only had images cut and distorted as a result. Anyone have an idea?
Edit: I found a bug: It does not work correctly for images that are not square, and I do not know why.