java 给图片加水印图片(水印位置与角度可定义)

java 给图片加水印(水印位置与角度可定义)

添加水印图片方法

	import javax.imageio.ImageIO;
	import javax.swing.*;
	import java.awt.*;
	import java.awt.image.BufferedImage;
	import java.io.*;
	
	/**
     * 给图片添加水印、可设置水印图片位置与旋转角
     * @param iconPath      水印图片路径
     * @param srcImgPath    源图片路径
     * @param targerPath    目标图片路径
     * @param location      水印位置:1、左上角,2、右上角,3、左下角,4、右下角,5、中间
     * @param degree        旋转角:顺时针角度
     */
    public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath, Integer location, Integer degree) {
        OutputStream os = null;
        try {
            Image srcImg = ImageIO.read(new File(srcImgPath));

            int width = srcImg.getWidth(null);
            int height = srcImg.getHeight(null);

            BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

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

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

            g.drawImage(srcImg, 0, 0, width, height, 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 syImg = imgIcon.getImage();

            float alpha = 0.5f; // 透明度

            int syWidth = syImg.getWidth(null);
            int syHeight = syImg.getHeight(null);

            // 如果水印图片高或宽大于目标图片是做的处理,使水印宽或高等于目标图片的宽高,并且等比例缩放
            int newSyWidth = syWidth;
            int newSyHeight = syHeight;

            if (syWidth > width) {
                newSyWidth = width;
                newSyHeight = (int) ((double)newSyWidth / syWidth * height);
            }

            if (newSyHeight > height) {
                newSyHeight = height;
                newSyWidth = (int) ((double)newSyHeight / syHeight * newSyWidth);
            }

            // 根据位置参数确定坐标位置
            int x = 0, y = 0;
            // location 位置: 1、左上角,2、右上角,3、左下角,4、右下角,5、中间
            switch (location) {
                case 1: break;
                case 2:
                    x = width - newSyWidth;
                    break;
                case 3:
                    y = height - newSyHeight;
                    break;
                case 4:
                    x = width - newSyWidth;
                    y = height - newSyHeight;
                    break;
                case 5:
                    x = (width - newSyWidth) / 2;
                    y = (height - newSyHeight) / 2;
                    break;
                default: break;
            }

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

            // 表示水印图片的位置
            g.drawImage(syImg, x, y, newSyWidth, newSyHeight, null);

            // 水印文件结束
            g.dispose();

            os = new FileOutputStream(targerPath);

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

            log.info("图片完成添加Icon印章... srcImgPath:{}, targerPath:{}", srcImgPath, targerPath);
        } catch (Exception e) {
            log.info("图片添加Icon印章异常! iconPath:{}, srcImgPath:{}, targerPath:{}, location:{}", iconPath, srcImgPath, targerPath, location);
        } finally {
            try {
                if (null != os) {
                    os.close();
                }
            } catch (Exception e) {
                log.error("", e);
            }
        }
    }

重载

	/**
     * 给图片添加中间位置水印图
     * @param iconPath 水印图片路径
     * @param srcImgPath 源图片路径
     * @param targerPath 目标图片路径
     */
    public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath) {
        markImageByIcon(iconPath, srcImgPath, targerPath, 5, null);
    }
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值