• Java图片添加水印


    一、文件夹下所有图片添加水印文字

    package day05;
    //添加水印文字
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    public class Protice01 {
           public static int num = 0;
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
               show(new File("D:/QQQ"));
               System.out.println(num);
        }
        public static void show(File file) throws IOException {
            if(file.isDirectory()) {
                File[] fs = file.listFiles();
                for(File f : fs) {
                    if(f.isFile()) {
                        if(f.getAbsolutePath().endsWith(".jpg")) {
                            File s = f.getAbsoluteFile();
                            BufferedImage i = ImageIO.read(f);
                               Graphics g = i.getGraphics();
                               g.setColor(Color.RED);
                               String str = "中兴教育";
                               int x = 150;
                               int y = 200;
                               g.drawString(str, x, y);
                               g.dispose();
                               ImageIO.write(i, "jpg", s);
                            //System.out.println(f.getAbsolutePath());
                            num++;                
        }        
        }else {
            show(f);
        }
        }        
      }  
    }    
    }

    二、添加水印图标

    package day07;
    //添加水印图标
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    
    public class Logo {
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
            BufferedImage logo =  ImageIO.read(new FileInputStream("D:/QQQ/b.jpg"));
            int lw = logo.getWidth();
            int lh = logo.getHeight();
            BufferedImage src = ImageIO.read(new FileInputStream("D:/QQQ/a.jpg"));
            int sh = src.getHeight();
            int sw = src.getWidth();
            Graphics s = src.getGraphics();    
            //int x = 0;
            //int y = 0;   //左上角
            
            //int x = (sw - lw)/2;
            //int y = 0;   //上中
            
            //int x = (sw - lw)+100;
            //int y = 0;   //右上
            
            int x = 0;
            int y = (sh - lh)/2;
            s.drawImage(logo, x, y, null);
            s.dispose();
            ImageIO.write(src, "jpg", new File("D:/QQQ/a_logo.jpg"));  
        }
    
    }
  • 相关阅读:
    【Java】Java中的集合框架、泛型
    【Java】Java中日期获取、数值精度、异常处理
    【Java】包装类、及字符序列CharSequence接口
    【微服务】Docker环境安装及卸载教程-持续更新中
    【Java】Stream流式编程实战
    【Java】Lambda表达式详解及实战
    【Java】Java中I/O流使用及详解
    加入博客园的第八个月
    Structs复习 Structs标签
    Structs复习 OGNL
  • 原文地址:https://www.cnblogs.com/zxwen/p/9477791.html
Copyright © 2020-2023  润新知