给图片添加满屏的图文水印

前言: 为了防止网站的图片资源被盗用,需要给图片打上图文的水印,之前做了一版只是在图片的右下角打上图文水印,这次升级了,要求在整个屏幕上都打上水印。话不多说,直接上代码。

	/**
	 * 水印图片的大小
	 */
	public static final double BASE_SIZE = 200d;
	/**
	 * 图片的宽高倍数
	 */
	public static final float BASE_MULTIPLE = 1.5f;
	/**
	 * 水印透明度
	 */
	private static float alpha = 0.2f;
	/**
	 * 水印文字大小
	 */
	public static final int FONT_SIZE = 11;
	/**
	 * 水印图片的大小
	 */
	public static final int LOGO_SIZE = 16;
	/**
	 * 水印文字颜色
	 */
	private static Color color = Color.WHITE;
	/**
	 * 水印之间的间隔
	 */
	private static final int XMOVE = 60;
	/**
	 * 水印之间的间隔
	 */
	private static final int YMOVE = 60;

	/**
	 * 获取文本长度。汉字为1:1,英文和数字为2:1
	 */
	private static int getTextLength(String text) {
		int length = text.length();
		for (int i = 0; i < text.length(); i++) {
			String s = String.valueOf(text.charAt(i));
			if (s.getBytes().length > 1) {
				length++;
			}
		}
		length = length % 2 == 0 ? length / 2 : length / 2 + 1;
		return length;
	}

	/**
	 * 给图片添加水印文字、可设置水印文字的旋转角度
	 *
	 * @param logoText
	 * @param srcImgPath
	 * @param degree
	 */
	public static BufferedImage addWaterAndText(String srcImgPath, BufferedImage markImgPath, String logoText, Integer degree) {
		try {
			// 源图片
			BufferedImage buffImg = ImageUtil.read(new File(srcImgPath));
			int width = buffImg.getWidth(null);
			int height = buffImg.getHeight(null);
			int smallNum = Integer.MAX_VALUE;
			smallNum = NumberUtils.min(smallNum, width, height);
			// 得到画笔对象
			Graphics2D g = buffImg.createGraphics();
			// 设置对线段的锯齿状边缘处理
			g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
			g.drawImage(buffImg, 0, 0, null);
			// 设置水印旋转
			if (null != degree) {
				g.rotate(Math.toRadians(degree));
			}
			// 设置水印文字颜色
			g.setColor(color);
			double multipleSize = smallNum / BASE_SIZE;
			// 设置水印文字Font
			Font font = new Font("微软雅黑", Font.BOLD, Double.valueOf(FONT_SIZE * multipleSize).intValue());
			g.setFont(font);
			// 设置水印文字透明度
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
			int x = 0;
			double widthDIvHeight = width / Double.valueOf(height);
			double heightDivWidth = height / Double.valueOf(width);
			if (widthDIvHeight < BASE_MULTIPLE) {
				widthDIvHeight = BASE_MULTIPLE;
			}
			if (heightDivWidth < BASE_MULTIPLE) {
				heightDivWidth = BASE_MULTIPLE;
			}
			int y = Double.valueOf(-height * widthDIvHeight).intValue();
			int markWidth = Double.valueOf(FONT_SIZE * multipleSize).intValue() * getTextLength(logoText);
			int markHeight = Double.valueOf(FONT_SIZE * multipleSize).intValue();
			// 循环添加水印
			while (y < height * widthDIvHeight) {
				x = Double.valueOf(-width * heightDivWidth).intValue();
				while (x < width * heightDivWidth) {
				   // 添加文字的水印
					g.drawString(logoText, x, y);
					x += markWidth + Double.valueOf(XMOVE * multipleSize).intValue();
				}
				y += markHeight + Double.valueOf(YMOVE * multipleSize).intValue();
			}
			// 释放资源
			g.dispose();
			BufferedImage destImage2 = new BufferedImage(width, height, 1);
			Graphics2D g2 = destImage2.createGraphics();
			g2.drawImage(buffImg, 0, 0, width, height, null);
			// 设置水印旋转
			if (null != degree) {
				g2.rotate(Math.toRadians(degree));
			}
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
			// 循环添加水印
			y = Double.valueOf(-height * widthDIvHeight).intValue();
			int logoSize = Double.valueOf(LOGO_SIZE * multipleSize).intValue();
			while (y < height * widthDIvHeight) {
				x = Double.valueOf(-width * heightDivWidth).intValue();
				while (x < width * heightDivWidth) {
				    // 画水印的
					g2.drawImage(markImgPath, x - logoSize, y - Double.valueOf(logoSize * 0.8).intValue(), logoSize, logoSize, null);
					x += markWidth + Double.valueOf(XMOVE * multipleSize).intValue();
				}
				y += markHeight + Double.valueOf(YMOVE * multipleSize).intValue();
			}
			g2.dispose();
			// 生成图片
			return destImage2;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

效果图:

水印效果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值