java图片旋转90度_Java实现图片内容无损任意角度旋转
时间: 2023-09-16 19:11:56 浏览: 153
可以使用Java的Graphics2D类实现图片内容无损任意角度旋转。以下是一个示例代码:
```java
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
public class ImageRotation {
public static BufferedImage rotate(BufferedImage image, double angle) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage rotated = new BufferedImage(width, height, image.getType());
Graphics2D g = rotated.createGraphics();
g.rotate(Math.toRadians(angle), width / 2, height / 2);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(image, 0, 0, null);
g.dispose();
return rotated;
}
}
```
使用该方法,可以将任意角度的图片旋转,并且不会损失图像内容。以下是一个示例代码:
```java
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class Main {
public static void main(String[] args) throws Exception {
BufferedImage image = ImageIO.read(new File("image.jpg"));
BufferedImage rotated = ImageRotation.rotate(image, 90);
ImageIO.write(rotated, "jpg", new File("rotated.jpg"));
}
}
```
该代码会将名为"image.jpg"的图片旋转90度,并将结果保存为"rotated.jpg"。
阅读全文