Skip to content

Instantly share code, notes, and snippets.

@gexiangdong
Created November 2, 2019 04:25
Show Gist options
  • Save gexiangdong/9be315286bbb3e52e8ff104849f60394 to your computer and use it in GitHub Desktop.
Save gexiangdong/9be315286bbb3e52e8ff104849f60394 to your computer and use it in GitHub Desktop.
Rotate a BufferedImage
public BufferedImage rotateImage (BufferedImage img, int n) {
int w = img.getWidth();
int h = img.getHeight();
BufferedImage bImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
bImg.getGraphics().drawImage(img, 0, 0, null);
int l = (int) Math.ceil(Math.sqrt(w * w * 1.0 + h * h));
double rotationRequired = Math.toRadians (n);
AffineTransform tx = new AffineTransform();
tx.translate(l, l);
tx.rotate(rotationRequired, 0, 0);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
BufferedImage newImage =new BufferedImage(l * 2, l * 2, bImg.getType());
op.filter(bImg, newImage);
return(newImage);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment