随便写了下,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 略
}