给图片添加水印

打印 上一主题 下一主题

主题 884|帖子 884|积分 2652

  1. @Slf4j
  2. public class ImageUtils {
  3.     @Test
  4.     public void test1() throws IOException {
  5.         //得到全部的图片文件
  6.         Path path = Paths.get("D:\\Files\\CDN\\file\\_resources");
  7.         if (!Files.exists(path)) {
  8.             throw new RuntimeException("目录或文件不存在!");
  9.         }
  10.         List<Path> collect = new ArrayList<>();
  11.         if (Files.isDirectory(path)) {
  12.             collect = Files.walk(path).filter(temp -> temp.getFileName().toString().endsWith(".png")).collect(Collectors.toList());
  13.         } else {
  14.             boolean b = path.getFileName().toString().endsWith(".png");
  15.             if (b) {
  16.                 collect.add(path);
  17.             }
  18.         }
  19.         if (CollectionUtils.isEmpty(collect)) {
  20.             return;
  21.         }
  22.         for (Path temp : collect) {
  23.             changeImg(temp.toFile());
  24.         }
  25.     }
  26.     /**
  27.      * 加水印
  28.      *
  29.      * @param srcImgFile 本地图片地址
  30.      * @throws IOException
  31.      */
  32.     private void changeImg(File srcImgFile) throws IOException {
  33.         //将文件对象转化为图片对象
  34.         Image srcImg = ImageIO.read(srcImgFile);
  35.         //获取图片的宽
  36.         int srcImgWidth = srcImg.getWidth(null);
  37.         //获取图片的高
  38.         int srcImgHeight = srcImg.getHeight(null);
  39. //        System.out.println("图片的宽:" + srcImgWidth);
  40. //        System.out.println("图片的高:" + srcImgHeight);
  41.         BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
  42.         // 加水印
  43.         //创建画笔
  44.         Graphics2D g = bufImg.createGraphics();
  45.         //绘制原始图片
  46.         g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
  47.         //-------------------------文字水印 start----------------------------
  48.         //根据图片的背景设置水印颜色
  49.         g.setColor(new Color(158,160,161));
  50.         //设置字体  画笔字体样式为微软雅黑,加粗,文字大小为12pt
  51.         g.setFont(new Font("微软雅黑", Font.BOLD, 12));
  52.         //水印内容
  53.         String waterMarkContent = "https://www.cnblogs.com/lemonpuer";
  54.         //设置水印的坐标
  55.         int x = (srcImgWidth - getWatermarkLength(waterMarkContent, g)) - 5;
  56.         int y = srcImgHeight - 5;
  57.         //画出水印 第一个参数是水印内容,第二个参数是x轴坐标,第三个参数是y轴坐标
  58.         g.drawString(waterMarkContent, x, y);
  59.         g.dispose();
  60.         //-------------------------文字水印 end----------------------------
  61.         //待存储的地址
  62. //        String tarImgPath = "F:\\0a8de9fc675db86eab79aa36b7575374.png";
  63.         // 输出图片
  64.         FileOutputStream outImgStream = new FileOutputStream(srcImgFile);
  65.         ImageIO.write(bufImg, "png", outImgStream);
  66.         log.info("图片{}成功添加水印",srcImgFile.getName());
  67.         outImgStream.flush();
  68.         outImgStream.close();
  69.     }
  70.     /**
  71.      * 获取水印文字的长度
  72.      *
  73.      * @param waterMarkContent
  74.      * @param g
  75.      * @return
  76.      */
  77.     private int getWatermarkLength(String waterMarkContent, Graphics2D g) {
  78.         return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
  79.     }
  80. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

千千梦丶琪

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表