import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 作成日期: 2009-11-13 下午03:02:47<br/>
* 修改者: <br/>
* 修改日期: <br/>
* 功能描述: 给图片加水印效果<br/>
*/
public class WaterMarkAppender {
public static void main(String[] args)throws Exception {
String srcUrl = "D:/My Documents/temp/p1.jpg";
String tarUrl = "D:/My Documents/temp/p2.jpg";
String waterMark = "D:/My Documents/temp/stars2.png";
appendWaterMark(srcUrl, waterMark, tarUrl, 40, 40);
}
public static void appendWaterMark(String srcUrl,String waterMark,String tarUrl,int x,int y){
try {
File srcFile = new File(srcUrl);
Image srcImage = ImageIO.read(srcFile);
int width = srcImage.getWidth(null);
int height = srcImage.getHeight(null);
BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = bi.createGraphics();
g.drawImage(srcImage, 0, 0, width, height, null);
//加水印
File wMFile = new File(waterMark);
Image wMImage = ImageIO.read(wMFile);
int wMWidth = wMImage.getWidth(null);
int wMHeight = wMImage.getHeight(null);
g.drawImage(wMImage,width-wMWidth-x,height-wMHeight-y,wMWidth,wMHeight, null);
g.dispose();
FileOutputStream fos = new FileOutputStream(tarUrl);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
encoder.encode(bi);
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}