Libraryless. Click here for Pure Java version (59L/1K).
// Thanks to ChatGPT static BufferedImage rotateImage(BufferedImage image, double angle) { int width = image.getWidth(); int height = image.getHeight(); // Calculate new image size to fit rotated image double radians = Math.toRadians(angle); double sin = Math.abs(Math.sin(radians)); double cos = Math.abs(Math.cos(radians)); int newWidth = (int) Math.floor(width * cos + height * sin); int newHeight = (int) Math.floor(height * cos + width * sin); // Create new image with transparent background BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = rotated.createGraphics(); AffineTransform at = new AffineTransform(); // Translate to center of image and rotate at.translate((newWidth - width) / 2, (newHeight - height) / 2); at.rotate(radians, width / 2, height / 2); // Draw original image onto new image with transformation g2d.setTransform(at); g2d.drawImage(image, 0, 0, null); g2d.dispose(); return rotated; }
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj
No comments. add comment
| Snippet ID: | #1036553 |
| Snippet name: | rotateImage - rotate image into a new image just big enough to fit, with transparent background |
| Eternal ID of this version: | #1036553/1 |
| Text MD5: | 34a33d7ecce17923743c2dda87fc79bb |
| Transpilation MD5: | 7bdb6b36ea9d2a592a79815754f2a4ab |
| Author: | stefan |
| Category: | javax / imaging |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2023-02-13 18:55:03 |
| Source code size: | 1047 bytes / 29 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 433 / 563 |
| Referenced in: | [show references] |