/**
* 水印文字效果
*/
public static void string(File f) throws IOException {
if (f.isDirectory()) {
File[] fs = f.listFiles();
for (File a : fs) {
if (a.isDirectory()) {
show(a);
} else {
if (a.getName().endsWith(".jpg")) {
BufferedImage s = ImageIO.read(a);
Graphics g = s.getGraphics();
g.setColor(new Color(int, int, int, int));//调整文字的颜色
g.setFont(new Font(String name, int style, int size));//调整字体格式和大小
g.drawString(String str, int x, int y);//要写入的内容和坐标位置
g.dispose();
ImageIO.write(s, "jpg", a);
}
}
}
} else {
if (f.getName().endsWith(".jpg")) {
BufferedImage s = ImageIO.read(f);
Graphics g = s.getGraphics();
g.setColor(new Color(int, int, int, int));
g.setFont(new Font(String name, int style, int size));
g.drawString(String str, int x, int y);
g.dispose();
ImageIO.write(s, "jpg", f);
}
}
}
/**
* 水印图片效果
*/
public static void image(File f,File f1) throws IOException {
BufferedImage bi = ImageIO.read(f);
int fw = bi.getWidth();
int fh = bi.getHeight();
BufferedImage logo = ImageIO.read(f1);
int logow = logo.getWidth();
int logoh = logo.getHeight();
Graphics g = bi.getGraphics();
/**
* int x = fw - logow;
* int y = fh - logoh;
* 水印位于右下角,刚好贴近右下角位置,不想贴近边缘可以视情况减去一个数字留一个空间
* int x = fw/2 - logow/2;
* int y = fh/2 - logoh/2;
* 水印位于中间
*/
Random rand = new Random();
int x = rand.nextInt(fw - logow)+1;
int y = rand.nextInt(fh - logoh)+1;
//水印位于随机位置
g.drawImage(logo, x, y, logow, logoh, null);//水印效果
g.dispose();
ImageIO.write(bi,"jpg",f);
}