/**
* 将图片处理为圆形图片
* 传入的图片必须是正方形的才会生成圆形 如果是长方形的比例则会变成椭圆的
*
* @param url
* @return
*/
public BufferedImage transferImgForRoundImgage(String url){
BufferedImage resultImg = null;
try {
if (StringUtils.isBlank(url)) {
return null;
}
BufferedImage buffImg1 = ImageIO.read(new URL(url));
resultImg = new BufferedImage(buffImg1.getWidth(), buffImg1.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = resultImg.createGraphics();
Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, buffImg1.getWidth(), buffImg1.getHeight());
// 使用 setRenderingHint 设置抗锯齿
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
resultImg = g.getDeviceConfiguration().createCompatibleImage(buffImg1.getWidth(), buffImg1.getHeight(),
Transparency.TRANSLUCENT);
//g.fill(new Rectangle(buffImg2.getWidth(), buffImg2.getHeight()));
g = resultImg.createGraphics();
// 使用 setRenderingHint 设置抗锯齿
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setClip(shape);
g.drawImage(buffImg1, 0, 0, null);
g.dispose();
} catch (MalformedURLException e) {
logger.error("URL格式异常" + e.getMessage(), e);
} catch (IOException e) {
logger.error("读取图片异常" + e.getMessage(), e);
}
return resultImg;
}
* 将图片处理为圆形图片
* 传入的图片必须是正方形的才会生成圆形 如果是长方形的比例则会变成椭圆的
*
* @param url
* @return
*/
public BufferedImage transferImgForRoundImgage(String url){
BufferedImage resultImg = null;
try {
if (StringUtils.isBlank(url)) {
return null;
}
BufferedImage buffImg1 = ImageIO.read(new URL(url));
resultImg = new BufferedImage(buffImg1.getWidth(), buffImg1.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = resultImg.createGraphics();
Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, buffImg1.getWidth(), buffImg1.getHeight());
// 使用 setRenderingHint 设置抗锯齿
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
resultImg = g.getDeviceConfiguration().createCompatibleImage(buffImg1.getWidth(), buffImg1.getHeight(),
Transparency.TRANSLUCENT);
//g.fill(new Rectangle(buffImg2.getWidth(), buffImg2.getHeight()));
g = resultImg.createGraphics();
// 使用 setRenderingHint 设置抗锯齿
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setClip(shape);
g.drawImage(buffImg1, 0, 0, null);
g.dispose();
} catch (MalformedURLException e) {
logger.error("URL格式异常" + e.getMessage(), e);
} catch (IOException e) {
logger.error("读取图片异常" + e.getMessage(), e);
}
return resultImg;
}