yzpure's recent timeline updates
yzpure

yzpure

V2EX member #325889, joined on 2018-06-30 20:11:02 +08:00
yzpure's recent replies
header 和 data 之间少一个空行,把最后 buffW.write("\r\n")上移一行。另外 header 最好加上 Content-Length
Dec 28, 2018
Replied to a topic by samples 程序员 Java 如何根据头像地址生成圆形的头像?
随便写了下,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 略
}
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3020 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 21ms · UTC 15:06 · PVG 23:06 · LAX 08:06 · JFK 11:06
♥ Do have faith in what you're doing.