原图
修改后
方法一
//文件路径
File imageFile = new File(path);
public BufferedImage changeImages(File imageFile) {
BufferedImage avatarImage = null;
try {
avatarImage = ImageIO.read(imageFile);
avatarImage = scaleByPercentage(avatarImage, avatarImage.getWidth(), avatarImage.getWidth());
int width = avatarImage.getWidth();
// 透明底的图片
BufferedImage formatAvatarImage = new BufferedImage(avatarImage.getWidth(), avatarImage.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D graphics = formatAvatarImage.createGraphics();
//把图片切成一个园
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//留一个像素的空白区域,这个很重要,画圆的时候把这个覆盖
int border = 1;
//图片是一个圆型
Ellipse2D.Double shape = new Ellipse2D.Double(border, border, width - border * 2, width - border * 2);
//需要保留的区域
graphics.setClip(shape);
graphics.drawImage(avatarImage, border, border, width - border * 2, width - border * 2, null);
//在圆图外面再画一个圆
//新创建一个graphics,这样画的圆不会有锯齿
// graphics = formatAvatarImage.createGraphics();
// graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// int border1 = 3;
// //画笔是4.5个像素,BasicStroke的使用可以查看下面的参考文档
// //使画笔时基本会像外延伸一定像素,具体可以自己使用的时候测试
// Stroke s = new BasicStroke(5F, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
// graphics.setStroke(s);
// graphics.setColor(Color.WHITE);
// graphics.drawOval(border1, border1, width - border1 * 2, width - border1 * 2);
// graphics.dispose();
//
return formatAvatarImage;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 缩小Image,此方法返回源图像按给定宽度、高度限制下缩放后的图像
*
* @param inputImage
* :压缩后宽度
* :压缩后高度
* @throws java.io.IOException
* return
*/
public BufferedImage scaleByPercentage(BufferedImage inputImage, int newWidth, int newHeight){
// 获取原始图像透明度类型
try {
int type = inputImage.getColorModel().getTransparency();
int width = inputImage.getWidth();
int height = inputImage.getHeight();
// 开启抗锯齿
RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 使用高质量压缩
renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
BufferedImage img = new BufferedImage(newWidth, newHeight, type);
Graphics2D graphics2d = img.createGraphics();
graphics2d.setRenderingHints(renderingHints);
graphics2d.drawImage(inputImage, 0, 0, newWidth, newHeight, 0, 0, width, height, null);
graphics2d.dispose();
return img;
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
方法二
String url = "https://www.pngall.com/wp-content/uploads/13/Christmas-Golden-Bell-PNG-Photo.png";
BufferedImage avatarImage = ImageIO.read(new URL(url));
// avatarImage = scaleByPercentage(avatarImage, avatarImage.getWidth(), avatarImage.getWidth());
int width = 120;
// 透明底的图片
BufferedImage formatAvatarImage = new BufferedImage(width, width, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D graphics = formatAvatarImage.createGraphics();
// 把图片切成一个圓
{
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 留一个像素的空白区域,这个很重要,画圆的时候把这个覆盖
int border = 1;
// 图片是一个圆型
Ellipse2D.Double shape = new Ellipse2D.Double(border, border, width - border * 2, width - border * 2);
// 需要保留的区域
graphics.setClip(shape);
graphics.drawImage(avatarImage, border, border, width - border * 2, width - border * 2, null);
graphics.dispose();
}
// 在圆图外面再画一个圆
{
// 新创建一个graphics,这样画的圆不会有锯齿
graphics = formatAvatarImage.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int border = 3;
// 画笔是4.5个像素,BasicStroke的使用可以查看下面的参考文档
// 使画笔时基本会像外延伸一定像素,具体可以自己使用的时候测试
Stroke s = new BasicStroke(4.5F, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
graphics.setStroke(s);
graphics.setColor(Color.WHITE);
graphics.drawOval(border, border, width - border * 2, width - border * 2);
graphics.dispose();
}
try (OutputStream os = new FileOutputStream("d:/temp-avatar.png")) {
ImageIO.write(formatAvatarImage, "PNG", os);
}
{
url = "https://img-blog.csdn.net/20180529213113521";
BufferedImage srcImg = ImageIO.read(new URL(url));
// scrImg加载完之后没有任何颜色
BufferedImage blankImage = new BufferedImage(srcImg.getWidth(), srcImg.getHeight(),
BufferedImage.TYPE_INT_RGB);
graphics = blankImage.createGraphics();
graphics.drawImage(srcImg, 0, 0, null);
int x = (blankImage.getWidth() - width) / 2;
int y = (blankImage.getHeight() - width) / 2;
graphics.drawImage(formatAvatarImage, x, y, width, width, null);
try (OutputStream os = new FileOutputStream("d:/temp-avatar.png")) {
ImageIO.write(blankImage, "PNG", os);
}
}