public static void main(String[] args) throws IOException {
//从链接中获取图片,如OSS,服务器等。
// String url = "https://mjmall.oss-cn-shanghai.aliyuncs.com/18/1/merchantIcon.png";
// BufferedImage avatarImage = ImageIO.read(new URL(url));
BufferedImage avatarImage = ImageIO.read(new File("C:/head/mager.jpg"));
int width = 120;//如果剪切出来的图片画质模糊,请将width 调大点.
// 透明底的图片
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 border1 = 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(border1, border1, width - border1 * 2, width - border1 * 2);
graphics.dispose();
OutputStream os = new FileOutputStream("C:/head/oo.png");//发布项目时,如:Tomcat 他会在服务器本地tomcat webapps文件下创建此文件名
ImageIO.write(formatAvatarImage, "PNG", os);
}
.改为工具类使用截取头像成圆形使用上传到oss(或其他,请自己更改,我只提供简单的方法)需要OSS架包或连接参数说明请跳转点击打开链接
public class CopeImageUtil {
private static Logger LOGGER = LoggerFactory.getLogger(CopeImageUtil.class);
//head参数图片,其他参数都是连接OSS使用的参数。
public static String headImages(String head, String ENDPOINT, String ACCESS_KEY_ID, String ACCESS_KEY_SECRET, String BUCKET_NAME) throws MalformedURLException{
//从链接中获取图片,如OSS,服务器等。如果不是从连接中获取请使用File上传。
String url = head;
BufferedImage avatarImage;
try {
avatarImage = ImageIO.read(new URL(url));
int width = 400;
// 透明底的图片
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 border1 = 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(border1, border1, width - border1 * 2, width - border1 * 2);
graphics.dispose();
// 创建OSSClient实例
OSSClient ossClient = new OSSClient(ENDPOINT,
ACCESS_KEY_ID, ACCESS_KEY_SECRET);
//随机生成图片名
String filePath = new DateTime().toString("yyyy/MM/dd/");
String fileName = IdUtils.genImageName() + ".png";
ImageIO.write(formatAvatarImage, "png", new FileOutputStream("copeImage.png"));
ossClient.putObject(BUCKET_NAME, filePath + fileName, new File("copeImage.png"));
//返回上传到OSS的图片地址链接
return "https://" + BUCKET_NAME + "."+ENDPOINT+"/" + filePath + fileName;
}catch (Exception e) {
LOGGER.error("调用小程序生成微信永久小程序码URL接口异常", e);
}
return null;
}
-----------------------------------------