在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

java对PNG图片圆角处理 保持PNG透明背景

2016-12-20 13:15| 发布者: zhangjf| 查看: 517| 评论: 0

摘要: /** 圆角处理* @param BufferedImage* @param cornerRadius* */public static String makeRoundedCorner(String srcImageFile, String result, String type, int cornerRadius) { try { BufferedImage ima ...
  1. /*
  2. * 圆角处理
  3. * @param BufferedImage
  4. * @param cornerRadius
  5. * */
  6. public static String makeRoundedCorner(String srcImageFile, String result, String type, int cornerRadius) {
  7. try {
  8. BufferedImage image = ImageIO.read(new File(srcImageFile));
  9. int w = image.getWidth();
  10. int h = image.getHeight();
  11. BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  12. Graphics2D g2 = output.createGraphics();
  13. output = g2.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);
  14. g2.dispose();
  15. g2 = output.createGraphics();
  16. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  17. g2.fillRoundRect(0, 0,w, h, cornerRadius, cornerRadius);
  18. g2.setComposite(AlphaComposite.SrcIn);
  19. g2.drawImage(image, 0, 0, w, h, null);
  20. g2.dispose();
  21. ImageIO.write(output, type, new File(result));
  22. return result;
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. return null;
  27. }
复制代码



最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 08:31

Copyright 2015-2025 djqfx

返回顶部