Created
November 2, 2019 04:25
-
-
Save gexiangdong/9be315286bbb3e52e8ff104849f60394 to your computer and use it in GitHub Desktop.
Rotate a BufferedImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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