Rotate image in java

I have a Pan Card image, and when I try to rotate it 45 degrees and save it, I get a cropped image. Code for image rotation:

BufferedImage dimg = new BufferedImage(w, h, img.getType()); Graphics2D g = dimg.createGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias! RenderingHints.VALUE_ANTIALIAS_ON); g.rotate(Math.toRadians(angle), w / 2, h / 2); g.drawImage(img, null, 0, 0); 
+4
source share
1 answer

Take a look at this example using AffineTransform:

http://www.billthelizard.com/2008/07/rotate-image-in-java.html

if there is code to download the image, then this is the kernel:

 private Image image; AffineTransform identity = new AffineTransform(); Graphics2D g2d = (Graphics2D)g; AffineTransform trans = new AffineTransform(); trans.setTransform(identity); trans.rotate( Math.toRadians(45) ); g2d.drawImage(image, trans, this); 
+7
source

Source: https://habr.com/ru/post/1413523/


All Articles