工具系列——为图片增加水印

5 篇文章 0 订阅

在这里插入图片描述

前言
      在开发公司项目时,需求提出要对公司上传的图片添加水印,要有版权意识,防止他人盗用,于是自己便写了这个工具类

不多废话直接上代码

/**
 * @author wyy
 * @version 1.0
 * @Classname ImgWaterRemark
 * @date 2020/12/4 13:08
 * @description 图片增加水印文字、水印图片
 **/
public class ImgWaterRemark {



    /**
     * 水印透明度
     */
    private static float alpha = 0.6f;

    /**
     * 水印横向位置
     */
    private static int positionWidth = 150;
    /**
     * 水印纵向位置
     */
    private static int positionHeight = 300;
    /**
     * 水印文字字体
     */
    private static Font font = new Font("宋体", Font.BOLD, 77);
    /**
     * 水印文字颜色
     */
    private static Color color = Color.LIGHT_GRAY;

    /**
     * 设置参数方法
     *
     * @param alpha          水印透明度
     * @param positionWidth  水印横向位置
     * @param positionHeight 水印纵向位置
     * @param font           水印文字字体
     * @param color          水印文字颜色
     */
    public static void setImageMarkOptions(float alpha, int positionWidth,
                                           int positionHeight, Font font, Color color) {
        if (alpha > 0) {
            ImgWaterRemark.alpha = alpha;
        }
        if (positionWidth != 0) {
            ImgWaterRemark.positionWidth = positionWidth;
        }
        if (positionHeight != 0) {
            ImgWaterRemark.positionHeight = positionHeight;
        }
        if (font != null) {
            ImgWaterRemark.font = font;
        }
        if (color != null) {
            ImgWaterRemark.color = color;
        }
    }


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

            Image srcImg = ImageIO.read(srcImgPath);

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

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

            // 2、设置对线段的锯齿状边缘处理
            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);
            // 3、设置水印旋转
            if (null != degree) {
                g.rotate(Math.toRadians(degree),
                        (double) buffImg.getWidth() / 2,
                        (double) buffImg.getHeight() / 2);
            }

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

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

            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                    alpha));

            // 6、水印图片的位置
            g.drawImage(img, positionWidth, positionHeight, null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            // 7、释放资源
            g.dispose();

            // 8、生成图片
            ImageIO.write(buffImg, "JPG", targetPath);

            System.out.println("图片完成添加水印图片");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != targetPath) {
                    targetPath.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 给图片添加水印文字、可设置水印文字的旋转角度
     *
     * @param logoText
     * @param srcImgPath
     * @param targetPath
     * @param degree
     */
    public static void markImageByText(String logoText, InputStream srcImgPath,
                                       OutputStream targetPath, Integer degree) {

        InputStream is = null;
        OutputStream os = null;
        try {
            // 1、源图片
            Image srcImg = ImageIO.read(srcImgPath);
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                    srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

            // 2、得到画笔对象
            Graphics2D g = buffImg.createGraphics();
            // 3、设置对线段的锯齿状边缘处理
            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);
            // 4、设置水印旋转
            if (null != degree) {
                g.rotate(Math.toRadians(degree),
                        (double) buffImg.getWidth() / 2,
                        (double) buffImg.getHeight() / 2);
            }
            // 5、设置水印文字颜色
            g.setColor(color);
            // 6、设置水印文字Font
            g.setFont(font);
            // 7、设置水印文字透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                    alpha));
            // 8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
            g.drawString(logoText, positionWidth, positionHeight);
            // 9、释放资源
            g.dispose();
            // 10、生成图片
            ImageIO.write(buffImg, "JPG", targetPath);

            System.out.println("图片完成添加水印文字");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != srcImgPath) {
                    srcImgPath.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (null != targetPath) {
                    targetPath.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

工具类

/**
 * Description: 图片水印工具类
 * @author wyy
 * @version 1.0
 */
public class ImageRemarkUtil implements BaseUtil {


    /**
     * 设置水印参数
     * @author wyy
     * @param alpha  透明度
     * @param positionWidth  长度
     * @param positionHeight  高度
     * @param font  大小
     * @param color 颜色
     */
    public static void setRemarkData(float alpha, int positionWidth,
                                     int positionHeight, Font font, Color color){
        ImgWaterRemark.setImageMarkOptions(alpha,positionWidth,positionHeight,font,color);
    }


    /**
     * 给图片添加水印文字 不旋转
     * @author wyy
     * @param logoText   水印文字
     * @param srcImgPath 源图片路径
     * @param targetPath 目标图片路径
     */
    public static void markImageByText(String logoText, InputStream srcImgPath,
                                       OutputStream targetPath) {
        ImgWaterRemark.markImageByText(logoText, srcImgPath, targetPath, null);
    }

    /**
     * 给图片添加水印文字 旋转
     * @author wyy
     * @param logoText   水印文字
     * @param srcImgPath 源图片路径
     * @param targetPath 目标图片路径
     * @param degree 目标图片路径
     * @description
     */
    public static void markImageByText(String logoText, InputStream srcImgPath,
                                       OutputStream targetPath,Integer degree) {
        ImgWaterRemark.markImageByText(logoText, srcImgPath, targetPath, degree);
    }


    /**
     * 给图片添加水印文字 不旋转水平水印
     * @author wyy
     * @param logoText   水印文字
     * @param srcImgPath 源图片路径
     * @param targetPath 目标图片路径
     */
    public static void markImageByText(String logoText, InputStream srcImgPath,
                                       String targetPath) {
        if ("".equals(targetPath)) {
            throw new RuntimeException("目标路径不能为空");
        }
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(targetPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        ImgWaterRemark.markImageByText(logoText, srcImgPath, outputStream, null);
    }


    /**
     * 给图片添加水印文字 不旋转水平水印
     * @author wyy
     * @param logoText   水印文字
     * @param srcImgPath 源图片路径
     * @param targetPath 目标图片路径
     * @param degree 旋转角度
     */
    public static void markImageByText(String logoText, InputStream srcImgPath,
                                       String targetPath,Integer degree) {
        if ("".equals(targetPath)) {
            throw new RuntimeException("目标路径不能为空");
        }
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(targetPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        ImgWaterRemark.markImageByText(logoText, srcImgPath, outputStream, degree);
    }


    /**
     * 给图片添加水印图片 不旋转水平水印
     * @author wyy
     * @param iconPath   水印图片路径
     * @param srcImgPath 源图片路径
     * @param targetPath 目标图片路径
     */
    public static void markImageByIcon(String iconPath, InputStream srcImgPath,
                                       String targetPath) {
        if ("".equals(targetPath)) {
            throw new RuntimeException("目标路径不能为空");
        }
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(targetPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        ImgWaterRemark.markImageByIcon(iconPath, srcImgPath, outputStream, null);
    }


    /**
     * 给图片添加水印图片 不旋转水平水印
     * @author wyy
     * @param iconPath   水印图片路径
     * @param srcImgPath 源图片路径
     * @param targetPath 目标图片路径
     * @param degree 旋转角度
     */
    public static void markImageByIcon(String iconPath, InputStream srcImgPath,
                                       String targetPath,Integer degree) {
        if ("".equals(targetPath)) {
            throw new RuntimeException("目标路径不能为空");
        }
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(targetPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        ImgWaterRemark.markImageByIcon(iconPath, srcImgPath, outputStream, degree);
    }

    /**
     * 给图片添加水印图片 水平水印
     * @author wyy
     * @param iconPath   水印图片路径
     * @param srcImgPath 源图片路径
     * @param targetPath 目标图片路径
     */
    public static void markImageByIcon(String iconPath, InputStream srcImgPath,
                                       OutputStream targetPath) {
        ImgWaterRemark.markImageByIcon(iconPath, srcImgPath, targetPath, null);
    }



    /**
     * 给图片添加水印图片 水平水印
     * @author wyy
     * @param iconPath   水印图片路径
     * @param srcImgPath 源图片路径
     * @param targetPath 目标图片路径
     */
    public static void markImageByIcon(String iconPath, InputStream srcImgPath,
                                       OutputStream targetPath,Integer degree) {
        ImgWaterRemark.markImageByIcon(iconPath, srcImgPath, targetPath, degree);
    }

}

演示代码

@Test
    void contextLoads() throws IOException {
       
        InputStream inputStream = new FileInputStream("D:\\work\\img\\1.png");
       // ImageRemarkUtil.markImageByText("复印无效",inputStream,"D:\\work\\img\\1new.png");
        
        ImageRemarkUtil.markImageByText("测试复印无效",inputStream,"D:\\work\\img\\1new.png",-45);
    }

结果

注意:不可连续处理图片,因为文件流已经关闭
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值