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

来源:互联网 发布:淘宝助手5.7 编辑:程序博客网 时间:2024/05/20 01:09

如何用java 添加水印

 

[java] view plaincopy
  1. import java.awt.*;   
  2. import java.awt.event.*;   
  3. import java.io.*;   
  4. import java.awt.image.*;   
  5. import org.w3c.dom.*;   
  6. import com.sun.image.codec.jpeg.*;   
  7. import javax.imageio.*;   
  8. /** 
  9. author myweb 
  10. */  
  11. public class ImgBean{   
  12. public void ImgBean(){}   
  13. public void ImgYin(String s,String ImgName){   
  14. try{   
  15. File _file = new File(ImgName);   
  16. Image src = ImageIO.read(_file);   
  17. int wideth=src.getWidth(null);   
  18. int height=src.getHeight(null);   
  19. BufferedImage image=new BufferedImage(wideth,height,BufferedImage.TYPE_INT_RGB);   
  20. Graphics g=image.createGraphics();   
  21. g.drawImage(src,0,0,wideth,height,null);   
  22. String s="我要加的水印 ,来源http://www.bt285.cn BT下载";   
  23. g.setColor(Color.RED);   
  24. g.setFont(new Font("宋体",Font.PLAIN,20));   
  25. Font aa=new Font("宋体",Font.PLAIN,20);   
  26. g.drawString(s,wideth-150,height-10);   
  27. g.dispose();   
  28. FileOutputStream out=new FileOutputStream(ImgName);   
  29. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);   
  30. encoder.encode(image);   
  31. out.close();   
  32. }   
  33. catch(Exception e){   
  34. System.out.println(e);   
  35. }   
  36. }   
  37. }  

原创粉丝点击