一、文件夹下所有图片添加水印文字
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")); } }