java实现给图片添加水印

  1 package michael.io.image;   
  2   
  3 import java.awt.AlphaComposite;   
  4 import java.awt.Graphics2D;   
  5 import java.awt.Image;   
  6 import java.awt.RenderingHints;   
  7 import java.awt.image.BufferedImage;   
  8 import java.io.File;   
  9 import java.io.FileInputStream;   
 10 import java.io.FileOutputStream;   
 11 import java.io.InputStream;   
 12 import java.io.OutputStream;   
 13   
 14 import javax.imageio.ImageIO;   
 15 import javax.swing.ImageIcon;   
 16   
 17 import com.sun.image.codec.jpeg.JPEGCodec;   
 18 import com.sun.image.codec.jpeg.JPEGImageDecoder;   
 19 import com.sun.image.codec.jpeg.JPEGImageEncoder;   
 20   
 21 /**  
 22  * 图片水印  
 23  * @blog http://sjsky.iteye.com  
 24  * @author Michael  
 25  */  
 26 public class ImageMarkLogoByIcon {   
 27   
 28     /**  
 29      * @param args  
 30      */  
 31     public static void main(String[] args) {   
 32         String srcImgPath = "d:/test/michael/myblog_01.png";   
 33         String iconPath = "d:/test/michael/blog_logo.png";   
 34         String targerPath = "d:/test/michael/img_mark_icon.jpg";   
 35         String targerPath2 = "d:/test/michael/img_mark_icon_rotate.jpg";   
 36         // 给图片添加水印   
 37         ImageMarkLogoByIcon.markImageByIcon(iconPath, srcImgPath, targerPath);   
 38         // 给图片添加水印,水印旋转-45   
 39         ImageMarkLogoByIcon.markImageByIcon(iconPath, srcImgPath, targerPath2,   
 40                 -45);   
 41   
 42     }   
 43   
 44     /**  
 45      * 给图片添加水印  
 46      * @param iconPath 水印图片路径  
 47      * @param srcImgPath 源图片路径  
 48      * @param targerPath 目标图片路径  
 49      */  
 50     public static void markImageByIcon(String iconPath, String srcImgPath,   
 51             String targerPath) {   
 52         markImageByIcon(iconPath, srcImgPath, targerPath, null);   
 53     }   
 54   
 55     /**  
 56      * 给图片添加水印、可设置水印图片旋转角度  
 57      * @param iconPath 水印图片路径  
 58      * @param srcImgPath 源图片路径  
 59      * @param targerPath 目标图片路径  
 60      * @param degree 水印图片旋转角度  
 61      */  
 62     public static void markImageByIcon(String iconPath, String srcImgPath,   
 63             String targerPath, Integer degree) {   
 64         OutputStream os = null;   
 65         try {   
 66             Image srcImg = ImageIO.read(new File(srcImgPath));   
 67   
 68             BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),   
 69                     srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);   
 70   
 71             // 得到画笔对象   
 72             // Graphics g= buffImg.getGraphics();   
 73             Graphics2D g = buffImg.createGraphics();   
 74   
 75             // 设置对线段的锯齿状边缘处理   
 76             g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,   
 77                     RenderingHints.VALUE_INTERPOLATION_BILINEAR);   
 78   
 79             g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg   
 80                     .getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);   
 81   
 82             if (null != degree) {   
 83                 // 设置水印旋转   
 84                 g.rotate(Math.toRadians(degree),   
 85                         (double) buffImg.getWidth() / 2, (double) buffImg   
 86                                 .getHeight() / 2);   
 87             }   
 88   
 89             // 水印图象的路径 水印一般为gif或者png的,这样可设置透明度   
 90             ImageIcon imgIcon = new ImageIcon(iconPath);   
 91   
 92             // 得到Image对象。   
 93             Image img = imgIcon.getImage();   
 94   
 95             float alpha = 0.5f; // 透明度   
 96             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,   
 97                     alpha));   
 98   
 99             // 表示水印图片的位置   
100             g.drawImage(img, 150, 300, null);   
101   
102             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));   
103   
104             g.dispose();   
105   
106             os = new FileOutputStream(targerPath);   
107   
108             // 生成图片   
109             ImageIO.write(buffImg, "JPG", os);   
110   
111             System.out.println("图片完成添加Icon印章。。。。。。");   
112         } catch (Exception e) {   
113             e.printStackTrace();   
114         } finally {   
115             try {   
116                 if (null != os)   
117                     os.close();   
118             } catch (Exception e) {   
119                 e.printStackTrace();   
120             }   
121         }   
122     }   
123 }  

 

posted @ 2016-03-23 11:05  魔流剑  阅读(1763)  评论(0编辑  收藏  举报