现在只能将图片变为圆角,但不是透明的,背景是白色的,
BufferedImage image = ImageIO.read(imageFile);
int w = image.getWidth();
int h = image.getHeight();
BufferedImage output = new BufferedImage(w, h,
BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = output.createGraphics();
// This is what we want, but it only does hard-clipping, i.e. aliasing
// g2.setClip(new RoundRectangle2D ...) // so instead fake soft-clipping by first drawing the desired clip shape
// in fully opaque white with antialiasing enabled...
g2.setComposite(AlphaComposite.SrcOut);
//g2.setComposite(AlphaComposite.getInstance(AlphaComposite.DST, 0.0f));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(new Color(0,0,0));
g2.setBackground(Color.black);
g2.setPaint(new Color(0,0,0));
g2.fill(new RoundRectangle2D.Float(0, 0, w, h, 60,
60));
// ... then compositing the image on top,
// using the white shape from above as alpha source
g2.setComposite(AlphaComposite.SrcAtop);
g2.drawImage(image, 0, 0, null); g2.dispose();
ImageIO.write(output, "png", new File(filePath));
BufferedImage image = ImageIO.read(imageFile);
int w = image.getWidth();
int h = image.getHeight();
BufferedImage output = new BufferedImage(w, h,
BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = output.createGraphics();
// This is what we want, but it only does hard-clipping, i.e. aliasing
// g2.setClip(new RoundRectangle2D ...) // so instead fake soft-clipping by first drawing the desired clip shape
// in fully opaque white with antialiasing enabled...
g2.setComposite(AlphaComposite.SrcOut);
//g2.setComposite(AlphaComposite.getInstance(AlphaComposite.DST, 0.0f));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(new Color(0,0,0));
g2.setBackground(Color.black);
g2.setPaint(new Color(0,0,0));
g2.fill(new RoundRectangle2D.Float(0, 0, w, h, 60,
60));
// ... then compositing the image on top,
// using the white shape from above as alpha source
g2.setComposite(AlphaComposite.SrcAtop);
g2.drawImage(image, 0, 0, null); g2.dispose();
ImageIO.write(output, "png", new File(filePath));
int w = image.getWidth();
int h = image.getHeight();
BufferedImage output = new BufferedImage(w, h,
BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = output.createGraphics();
g2.setColor(new Color(0,0,0,0)); //用全透明颜色
g2.fillRect(0,0,w,h);//覆盖整个画布 // This is what we want, but it only does hard-clipping, i.e. aliasing
// g2.setClip(new RoundRectangle2D ...) // so instead fake soft-clipping by first drawing the desired clip shape
// in fully opaque white with antialiasing enabled...
g2.setComposite(AlphaComposite.SrcOut);
//g2.setComposite(AlphaComposite.getInstance(AlphaComposite.DST, 0.0f));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(new Color(0,0,0));
g2.setBackground(Color.black);
g2.setPaint(new Color(0,0,0));
g2.fill(new RoundRectangle2D.Float(0, 0, w, h, 60,
60));
// ... then compositing the image on top,
// using the white shape from above as alpha source
g2.setComposite(AlphaComposite.SrcAtop);
g2.drawImage(image, 0, 0, null); g2.dispose();
ImageIO.write(output, "png", new File(filePath));
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.7</version>
</dependency>
然后java代码读取上传图片
BufferedImage image = ImageIO.read(imageFile);
圆角处理
image = Img.from(image).round(0.05).getImg();
最后一定要使用png格式
ImageIO.write(output, "png", new File(filePath));
是不是透明和图片文件格式有关系,我所知道的只有psd格式(photoshop格式文件)和tif格式的图片才支持图层模式。其他格式会在保存时会强制合并图层,造成透明部分被填白色。改图片的推荐还是用专业软件比较好。