用Java为图片添加水印的源代码

import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
import java.awt.image.*; 
import org.w3c.dom.*; 
import com.sun.image.codec.jpeg.*; 
import javax.imageio.*; 
/**
author myweb
*/
public class ImgBean{ 
public void ImgBean(){} 
public void ImgYin(String s,String ImgName){ 
try{ 
File _file = new File(ImgName); 
Image src = ImageIO.read(_file); 
int wideth=src.getWidth(null); 
int height=src.getHeight(null); 
BufferedImage image=new BufferedImage(wideth,height,BufferedImage.TYPE_INT_RGB); 
Graphics g=image.createGraphics(); 
g.drawImage(src,0,0,wideth,height,null); 
String s="我要加的水印 ,来源http://www.bt285.cn BT下载"; 
g.setColor(Color.RED); 
g.setFont(new Font("宋体",Font.PLAIN,20)); 
Font aa=new Font("宋体",Font.PLAIN,20); 
g.drawString(s,wideth-150,height-10); 
g.dispose(); 
FileOutputStream out=new FileOutputStream(ImgName); 
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
encoder.encode(image); 
out.close(); 
} 
catch(Exception e){ 
System.out.println(e); 
} 
} 
}


 

你可能感兴趣的:(用Java为图片添加水印的源代码)