package com; //添加水印文字 import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Random; public class Imgword { public static void main(String[] args) throws IOException { BufferedImage im =ImageIO.read(new FileInputStream("d:/img/111.jpg")); int iw = im.getWidth(); int ih = im.getHeight(); Graphics g = im.getGraphics(); Color color = new Color(255,0,255,100); //設置隨機顏色 g.setColor(color); //g.setColor(Color.BLUE); Font font = new Font("宋體",Font.PLAIN,30); //設置字體 g.setFont(font); String str = "12345"; //int x = iw/2; //int y = ih/2; Random ran = new Random(); // 隨機位置 int x = ran.nextInt(iw); int y = ran.nextInt(ih); g.drawString(str,x,y); g.dispose(); ImageIO.write(im,"jpg",new File("d:/img/111.jpg")); } }