java生成圆_Java 如何根据头像地址生成圆形的头像?

yzpure

2018-12-28 19:03:57 +08:00

随便写了下,ARGB 带透明效果,输出 png 图片也会比 jpg 大不少,实时生成图片比较耗时的,访问量大的话要注意

----------------------代码分割线-------------------------

public static void main(String[] args) throws IOException {

String str = "这里是图片链接";

URL url = new URL(str);

BufferedImage srcImage = ImageIO.read(url);

int width = srcImage.getWidth();

int height = srcImage.getHeight();

BufferedImage dstImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

Graphics2D g2 = dstImage.createGraphics();

TexturePaint texturePaint = new TexturePaint(srcImage, new Rectangle2D.Float(0, 0, width, height));

g2.setPaint(texturePaint);

Ellipse2D.Float ellipse = new Ellipse2D.Float(0, 0, width, height);

// 抗锯齿

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

g2.fill(ellipse);

g2.dispose();

// write to file

ImageIO.write(dstImage, "png", new File("portrait.png"));

// to bytes or inputStream 略

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值