给图片添加水印

  public class Photo {
// 水印透明度
    private static float alpha = 0.9f;
    // 水印横向位置
    private static int positionWidth = 60;

    // 水印纵向位置
    private static int positionHeight = 1500;
    // 水印文字字体
    private static Font font = new Font("宋体", Font.BOLD, 30);
    // 水印文字颜色
    private static Color color = Color.white;
    // 单个文件可以通过在这添加水印内容
    private static String logoText = "cyb\n-水印-";
    // 通过excel文件实现批量操作
    private static String textPath = "D:\\photo\\zhqd1.xlsx";
    // 图片位置
    private static  String srcImgPath = "";
    // 批量操作图片文件根路径
    private static String rootPath = "D:\\photo\\照片";


 public static void main(String[] args) throws IOException {

        markImageByText(logoText, srcImgPath);
    }
// 此处展示批量操作
 public static void markImageByText(String logoText, String srcImgPath) 
               throws IOException         {
     // 读取excel水印文件
     ExcelReader readerExcel = ExcelUtil.getReader(new File(textPath));
    // 从第一行开始读取
     List<List<Object>> dataList = readerExcel.read(1);
     for (Integer i =0;i<dataList.size();i++){
        List<String> list = new ArrayList<>();
        // 获取每一行每一列的文字
        list.add((String) dataList.get(i).get(0));
        list.add((String) dataList.get(i).get(1));
        list.add((String) dataList.get(i).get(2));
        list.add((String) dataList.get(i).get(3));
        // 获取目标文件夹的名称,在excel中第二列存储
        String excelName = (String)dataList.get(i).get(1) +"\\"+dataList.get(i).get(0);
        // 拼接路径
        String excelPath = rootPath+"\\"+excelName;
        // 读取文件
        File file = new File(excelPath);
        String fileName = file.getName();
        String filepath = file.getAbsolutePath();
        String[] filelist = file.list();
  if (filelist  !=null){
       for (int j = 0; j < filelist.length; j++) {
            File readfile = new File(rootPath + "\\" + filelist[j]);

            if (!readfile.isDirectory()) {
            fileList.add(readfile);
          }

        for (String aa : filelist){
              File targetFile = new File(file.getParent()+"\\"+fileName+"\\target");
              if (!targetFile.exists()){
                   targetFile.mkdirs();
                  }
             File targetJpg = new File(targetFile.getAbsolutePath());
             if (!targetJpg.exists()){
                 targetJpg.createNewFile();
                   targerPath = targetJpg.getAbsolutePath();
                }
             targerPath = targetJpg.getAbsolutePath()+"\\"+aa;


             srcImgPath = filepath+"\\"+aa;
             InputStream is = null;
             OutputStream os = null;
             try {
                   // 1、源图片
                Image srcImg = ImageIO.read(new File(srcImgPath));
                 BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

                 // 2、得到画笔对象
                  Graphics2D g = buffImg.createGraphics();
                  // 3、设置对线段的锯齿状边缘处理
                  g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                  RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                  g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null),
                  srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,null);
                  // 4、设置水印旋转
                  Integer degree = 0;
                  if (null != degree) {
                       g.rotate(Math.toRadians(degree),
                       (double) buffImg.getWidth() / 2,
                       (double) buffImg.getHeight() / 2);
                     }
                  // 5、设置水印文字颜色
                  g.setColor(color);
                  // 6、设置水印文字Font
                  g.setFont(font);
                  // 7、设置水印文字透明度
                            
         g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));



      // ----------------------------------------------------------
      // 插入注释  根据图片的大小自适应水印位置;从文档中读取文字
      positionHeight = srcImg.getHeight(null)-250;
       Integer fixWidth = srcImg.getWidth(null)-600;
       Integer fixHeight =  srcImg.getHeight(null)-400;
//            File file = new File(textPath);
//            BufferedReader reader = null;
//            List<String> list = new ArrayList<>();
//            try {
//                reader = new BufferedReader(new FileReader(file));
//                String tempStr;
//                while ((tempStr = reader.readLine()) != null) {
//                    list.add(tempStr);
//                }
//                reader.close();
//            } catch (IOException e){
//
//            }finally {
//                if (reader != null) {
//                    try {
//                        reader.close();
//                    } catch (IOException e1) {
//                        e1.printStackTrace();
//                    }
//                }
//            }
// ----------------------------------------------------------



        // ------------------------------------------------------------------------------
       // 8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
       // 写入数据换行处理
       // 写死写法
       // for (String text:logoText.split("\n"))
       // 文本读入写法
       for (String text:list){
       g.drawString(text,positionWidth,positionHeight+=g.getFontMetrics().getHeight()+5);
// --------------------------------------------------------------------------------------
//            for (String fixText:logoText.split("\n")){
//                                g.drawString(fixText,fixWidth,fixHeight+=g.getFontMetrics().getHeight()+5);
//            }

     // 9、释放资源
     g.dispose();
     // 10、生成图片
     os = new FileOutputStream(targerPath);
     ImageIO.write(buffImg, "JPG", os);

     System.out.println("图片完成添加水印文字");

     } catch (Exception e) {
           e.printStackTrace();
      } finally {
           try {
                 if (null != is)
                        is.close();
               } catch (Exception e) {
                        e.printStackTrace();
           }
                            try {
                                if (null != os)
                                    os.close();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
    }
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值