导入相关包:
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
读取图片:
public static BufferedImage read(File file) {
try {
BufferedImage bufferedImage = ImageIO.read(file);
return bufferedImage;
} catch (IOException e) {
logger.error("ERROR", e);
}
return null;
}
旋转图片:
public BufferedImage rImage(){
//读取需要旋转的图片
BufferedImage chars = read(new File("C:\\a.png"));
Color color = new Color(255, 255, 255);
int degree = 30;
int width = 65;
int height = 65;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
// 全部刷成设置的color,注释这行代码,则是透明背景
g.setBackground(color);
g.fillRect(0, 0, width, height);
//旋转
g.rotate(Math.toRadians(degree), width / 2, height / 2);
//注意,要先旋转画布,然后再将图片画上去
g.drawImage(chars, 14, 15, null);
g.dispose();
return image;
}