Java实现给图片添加水印

本门目录:

  • 图片水印
  • 文字水印

[一]、图片水印

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.     } 
package michael.io.image;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 图片水印
 * @blog http://sjsky.iteye.com
 * @author Michael
 */
public class ImageMarkLogoByIcon {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String srcImgPath = "d:/test/michael/myblog_01.png";
        String iconPath = "d:/test/michael/blog_logo.png";
        String targerPath = "d:/test/michael/img_mark_icon.jpg";
        String targerPath2 = "d:/test/michael/img_mark_icon_rotate.jpg";
        // 给图片添加水印
        ImageMarkLogoByIcon.markImageByIcon(iconPath, srcImgPath, targerPath);
        // 给图片添加水印,水印旋转-45
        ImageMarkLogoByIcon.markImageByIcon(iconPath, srcImgPath, targerPath2,
                -45);

    }

    /**
     * 给图片添加水印
     * @param iconPath 水印图片路径
     * @param srcImgPath 源图片路径
     * @param targerPath 目标图片路径
     */
    public static void markImageByIcon(String iconPath, String srcImgPath,
            String targerPath) {
        markImageByIcon(iconPath, srcImgPath, targerPath, null);
    }

    /**
     * 给图片添加水印、可设置水印图片旋转角度
     * @param iconPath 水印图片路径
     * @param srcImgPath 源图片路径
     * @param targerPath 目标图片路径
     * @param degree 水印图片旋转角度
     */
    public static void markImageByIcon(String iconPath, String srcImgPath,
            String targerPath, Integer degree) {
        OutputStream os = null;
        try {
            Image srcImg = ImageIO.read(new File(srcImgPath));

            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

            // 得到画笔对象
            // Graphics g= buffImg.getGraphics();
            Graphics2D g = buffImg.createGraphics();

            // 设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);

            g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg
                    .getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);

            if (null != degree) {
                // 设置水印旋转
                g.rotate(Math.toRadians(degree),
                        (double) buffImg.getWidth() / 2, (double) buffImg
                                .getHeight() / 2);
            }

            // 水印图象的路径 水印一般为gif或者png的,这样可设置透明度
            ImageIcon imgIcon = new ImageIcon(iconPath);

            // 得到Image对象。
            Image img = imgIcon.getImage();

            float alpha = 0.5f; // 透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                    alpha));

            // 表示水印图片的位置
            g.drawImage(img, 150, 300, null);

            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));

            g.dispose();

            os = new FileOutputStream(targerPath);

            // 生成图片
            ImageIO.write(buffImg, "JPG", os);

            System.out.println("图片完成添加Icon印章。。。。。。");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

原始图片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.     } 
package michael.io.image;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

/**
 * 文字水印
 * @blog http://sjsky.iteye.com
 * @author Michael
 */
public class ImageMarkLogoByText {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String srcImgPath = "d:/test/michael/myblog_01.jpg";
        String logoText = "[ 测试文字水印 http://sjsky.iteye.com ]";
        String targerPath = "d:/test/michael/img_mark_text.jpg";

        String targerPath2 = "d:/test/michael/img_mark_text_rotate.jpg";

        // 给图片添加水印
        ImageMarkLogoByText.markByText(logoText, srcImgPath, targerPath);

        // 给图片添加水印,水印旋转-45
        ImageMarkLogoByText.markByText(logoText, srcImgPath, targerPath2, -45);
    }

    /**
     * 给图片添加水印
     * @param logoText
     * @param srcImgPath
     * @param targerPath
     */
    public static void markByText(String logoText, String srcImgPath,
            String targerPath) {
        markByText(logoText, srcImgPath, targerPath, null);
    }

    /**
     * 给图片添加水印、可设置水印的旋转角度
     * @param logoText
     * @param srcImgPath
     * @param targerPath
     * @param degree
     */
    public static void markByText(String logoText, String srcImgPath,
            String targerPath, Integer degree) {
        // 主图片的路径
        InputStream is = null;
        OutputStream os = null;
        try {
            Image srcImg = ImageIO.read(new File(srcImgPath));

            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

            // 得到画笔对象
            // Graphics g= buffImg.getGraphics();
            Graphics2D g = buffImg.createGraphics();

            // 设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);

            g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg
                    .getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);

            if (null != degree) {
                // 设置水印旋转
                g.rotate(Math.toRadians(degree),
                        (double) buffImg.getWidth() / 2, (double) buffImg
                                .getHeight() / 2);
            }

            // 设置颜色
            g.setColor(Color.red);

            // 设置 Font
            g.setFont(new Font("宋体", Font.BOLD, 30));

            float alpha = 0.5f;
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                    alpha));

            // 第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y) .
            g.drawString(logoText, 150, 300);

            g.dispose();

            os = new FileOutputStream(targerPath);

            // 生成图片
            ImageIO.write(buffImg, "JPG", os);

            System.out.println("图片完成添加文字印章。。。。。。");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != is)
                    is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (null != os)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值