合并多张图片为一张图片并添加水印

  //合并多张图片为一张图片
  public String mergeImgFile(List<String> list ,String assignUser,  String filePath) {
    int size = list.size();
    int height = 0;
    if (size == 0) {
      height = 150 * 1;
    } else {
      height = 150 * size;
    }

    BufferedImage bi = new BufferedImage(300, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    long len = 0L;
    for (int i = 0; i < list.size(); i++) {
      String s = list.get(i);
      try {
        BufferedImage image = null;
        if (!new File(s).exists()) {
          //s= "modeFile\\1px.png";      System.out.println(Arrays.toString(b));打印完整数组
//          byte [] bu = image2byte("modeFile\\1px.png");

          byte[] bu = new byte[]{-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 3, 0, 0, 0, 3, 8, 6, 0, 0, 0, 86, 40, -75, -65, 0, 0,
              0, 1, 115, 82, 71, 66, 0, -82, -50, 28, -23, 0, 0, 0, 4, 103, 65, 77, 65, 0, 0, -79, -113, 11, -4, 97, 5, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 14,
              -61, 0, 0, 14, -61, 1, -57, 111, -88, 100, 0, 0, 0, 16, 73, 68, 65, 84, 24, 87, 99, -8, -113, 4, 112, 113, -2, -1, 7, 0, -69, -121, 35, -35, -124,
              -73, -16, -20, 0, 0, 0, 0, 73, 69, 78, 68, -82, 66, 96, -126};

          filePath = "modeFile";

          image = (BufferedImage) ImageIO.read(new ByteArrayInputStream(bu));
        } else {
          image = (BufferedImage) ImageIO.read(new File(s));
        }

        len += new File(s).length();
        g.drawImage(image, 0, i * 150, 300, 150, null);

        Font f = new Font("宋体", Font.PLAIN,68);
        Color mycolor = Color.black;//new Color(0, 0, 255);
        g.setColor(mycolor);
        g.setFont(f);
        g.drawString(assignUser,30,80);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    g.dispose();

    Random random = new Random();
    String fourRandom = random.nextInt(10000) + "";
    int randLength = fourRandom.length();
    if (randLength < 4) {
      for (int i = 1; i <= 4 - randLength; i++) {
        fourRandom = "0" + fourRandom;
      }
    }
    String fn = "test" + fourRandom + ".png";

    String newImage = filePath + "\\" + fn;

    if (newImage != null && bi != null) {
      try {
        File outputfile = new File(newImage);
        ImageIO.write(bi, "png", outputfile);
      } catch (IOException e) {
        System.out.println(e.getMessage());
      }
    }

    while (new File(newImage).length() >= len) {
      return newImage;
    }
    return "";
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人生万事须自为,跬步江山即寥廓。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值