java 旋转图片尺寸变大 怎么办
时间: 2023-10-11 08:14:58 浏览: 49
你可以使用 Java 中的 AffineTransform 类来旋转和缩放图片。下面是一个示例代码,它将一个图片旋转90度并且尺寸变大了两倍:
```java
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class ImageRotationExample {
public static void main(String[] args) throws Exception {
// Load the original image
BufferedImage originalImage = ImageIO.read(new File("original.jpg"));
// Calculate the new size of the image after rotation and scaling
int newWidth = originalImage.getHeight() * 2;
int newHeight = originalImage.getWidth() * 2;
// Create a new image with the new size
BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
// Rotate and scale the original image and draw it onto the new image
Graphics2D g = newImage.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.translate(newWidth / 2, newHeight / 2);
g.rotate(Math.toRadians(90));
g.scale(2, 2);
g.drawImage(originalImage, -originalImage.getWidth() / 2, -originalImage.getHeight() / 2, null);
g.dispose();
// Save the new image
ImageIO.write(newImage, "png", new File("rotated.png"));
}
}
```
这个代码会加载名为 "original.jpg" 的图片文件,将其旋转90度并且尺寸变大了两倍,然后将处理后的图片保存为名为 "rotated.png" 的 PNG 文件。你可以根据需要修改代码来适应不同的图片尺寸和旋转角度。