现在的位置: 首页 > 综合 > 正文

Java实现给图片添加水印

2013年10月30日 ⁄ 综合 ⁄ 共 11789字 ⁄ 字号 评论关闭

本门目录:

  • 图片水印
  • 文字水印

[一]、图片水印

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.     } 

原始图片myblog_01.jpg:

水印图片blog_logo.png:

添加水印后的效果图:

水印图标旋转的效果图:

 

[二]、文字水印

Java代码 
  1. package michael.io.image; 
  2.  
  3. import java.awt.AlphaComposite; 
  4. import java.awt.Color; 
  5. import java.awt.Font; 
  6. import java.awt.Graphics2D; 
  7. import java.awt.Image; 
  8. import java.awt.RenderingHints; 
  9. import java.awt.image.BufferedImage; 
  10. import java.io.File; 
  11. import java.io.FileOutputStream; 
  12. import java.io.InputStream; 
  13. import java.io.OutputStream; 
  14.  
  15. import javax.imageio.ImageIO; 
  16.  
  17. /**
  18. * 文字水印
  19. * @blog http://sjsky.iteye.com
  20. * @author Michael
  21. */ 
  22. public class ImageMarkLogoByText { 
  23.  
  24.     /**
  25.      * @param args
  26.      */ 
  27.     public static
    void main(String[] args) { 
  28.         String srcImgPath = "d:/test/michael/myblog_01.jpg"
  29.         String logoText = "[ 测试文字水印 http://sjsky.iteye.com ]"
  30.         String targerPath = "d:/test/michael/img_mark_text.jpg"
  31.  
  32.         String targerPath2 = "d:/test/michael/img_mark_text_rotate.jpg"
  33.  
  34.         // 给图片添加水印 
  35.         ImageMarkLogoByText.markByText(logoText, srcImgPath, targerPath); 
  36.  
  37.         // 给图片添加水印,水印旋转-45 
  38.         ImageMarkLogoByText.markByText(logoText, srcImgPath, targerPath2, -45); 
  39.     } 
  40.  
  41.     /**
  42.      * 给图片添加水印
  43.      * @param logoText
  44.      * @param srcImgPath
  45.      * @param targerPath
  46.      */ 
  47.     public static
    void markByText(String logoText, String srcImgPath, 
  48.             String targerPath) { 
  49.         markByText(logoText, srcImgPath, targerPath,
    null
    ); 
  50.     } 
  51.  
  52.     /**
  53.      * 给图片添加水印、可设置水印的旋转角度
  54.      * @param logoText
  55.      * @param srcImgPath
  56.      * @param targerPath
  57.      * @param degree
  58.      */ 
  59.     public static
    void markByText(String logoText, String srcImgPath, 
  60.             String targerPath, Integer degree) { 
  61.         // 主图片的路径 
  62.         InputStream is = null
  63.         OutputStream os = null
  64.         try
  65.             Image srcImg = ImageIO.read(new File(srcImgPath)); 
  66.  
  67.             BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), 
  68.                     srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB); 
  69.  
  70.             // 得到画笔对象 
  71.             // Graphics g= buffImg.getGraphics(); 
  72.             Graphics2D g = buffImg.createGraphics(); 
  73.  
  74.             // 设置对线段的锯齿状边缘处理 
  75.             g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
  76.                     RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
  77.  
  78.             g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg 
  79.                     .getHeight(null), Image.SCALE_SMOOTH),
    0, 0,
    null); 
  80.  
  81.             if (null != degree) { 
  82.                 // 设置水印旋转 
  83.                 g.rotate(Math.toRadians(degree), 
  84.                         (double) buffImg.getWidth() /
    2, (double) buffImg 
  85.                                 .getHeight() / 2); 
  86.             } 
  87.  
  88.             // 设置颜色 
  89.             g.setColor(Color.red); 
  90.  
  91.             // 设置 Font 
  92.             g.setFont(new Font("宋体", Font.BOLD,
    30)); 
  93.  
  94.             float alpha = 0.5f; 
  95.             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 
  96.                     alpha)); 
  97.  
  98.             // 第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y) . 
  99.             g.drawString(logoText, 150,
    300); 
  100.  
  101.             g.dispose(); 
  102.  
  103.             os = new FileOutputStream(targerPath); 
  104.  
  105.             // 生成图片 
  106.             ImageIO.write(buffImg, "JPG", os); 
  107.  
  108.             System.out.println("图片完成添加文字印章。。。。。。"); 
  109.         } catch (Exception e) { 
  110.             e.printStackTrace(); 
  111.         } finally
  112.             try
  113.                 if (null != is) 
  114.                     is.close(); 
  115.             } catch (Exception e) { 
  116.                 e.printStackTrace(); 
  117.             } 
  118.             try
  119.                 if (null != os) 
  120.                     os.close(); 
  121.             } catch (Exception e) { 
  122.                 e.printStackTrace(); 
  123.             } 
  124.         } 
  125.     } 

 

 

 

抱歉!评论已关闭.