Graphics2D 图片base64加水印

base64加水印

	/**
	 * 增加图片水印
	 */
	public static byte[] AddWaterMark(byte[] wjnr, String text) throws Exception {
		Font font = new Font("微软雅黑", Font.ITALIC, 19); // 水印文字字体
		Color color = Color.RED; // 水印文字颜色
		Float alpha = 0.2f; // 水印透明度
	
		// 组装水印文字  图片水印#时间
		text = text + "#" + DateUtil.dateToString(DateUtil.getDBTime(), "yyyy年MM月dd日  hh:mm:ss");
		String[] arrText = text.split("#");

		InputStream is = null;
		OutputStream os = null;
		
		try {
			is = new java.io.ByteArrayInputStream(wjnr);
			BufferedImage srcImg = ImageIO.read(is);
			BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
 
			// 得到画笔对象
			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);
			
			// 设置水印旋转
			Integer degree = -40;
			g.rotate(Math.toRadians(degree),
			(double) buffImg.getWidth() / 5,
			(double) buffImg.getHeight() / 5);
			
			g.setColor(color); // 设置水印文字颜色
			g.setFont(font); // 设置水印文字Font
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 设置水印文字透明度
			
			int strHeight = getStringHeight(g);
			int height = srcImg.getHeight();
			
            // 换行展示
            int maxLength = arrText[0].length();
            for(int i = 1 ; i < arrText.length; i++){
            	if(maxLength < arrText[i].length()){
            		maxLength = arrText[i].length();
            	} 
            }

			for (int i = 0; i < arrText.length; i++) {
				String s = "";
				int j = maxLength - arrText[i].length(); // 增加空格数

				int n = isNumeric(arrText[i]); // 字符串包含数字
				j = j + n / 2;
				for (int k = 0; k < j; k++) {
					s = s.concat("  ");
				}
				StringBuffer str = new StringBuffer(arrText[i]);
				str.insert(0, s);
				arrText[i] = str.toString();
			}

			FontMetrics fm = g.getFontMetrics(font);
			int x = -50;
			int y = 50 + (height - fm.getHeight()) / 2 + fm.getAscent();

			for (int i = 0; i < arrText.length; i++) {
				char[] strContentArr = arrText[i].toCharArray();
				g.drawString(arrText[i].substring(0, strContentArr.length), x, y
						+ strHeight * i);
			}

			g.dispose(); // 释放资源

			// 把图片转换为字节
			ByteArrayOutputStream bot = new ByteArrayOutputStream();
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bot);
			encoder.encode(buffImg);

			byte[] rstByte = bot.toByteArray();
			return rstByte;
		} catch (Exception e) {
			return wjnr;
		} finally {
			try {
				if (null != is) {
					is.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				if (null != os){
					os.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 * 字符高度-水印
	 */
	private static int getStringHeight(Graphics2D g) {
        int height = g.getFontMetrics().getHeight();
        return height;
    }
	
	/**
	 * 统计字符串中数字个数
	 * 
	 * @author gsl
	 * @date 创建时间 2022-4-14
	 * @since V1.0
	 */
	public static int isNumeric(String str){
	       byte[] array1 = str.getBytes();//将字符串转换为字符数组
	       int count = 0;
	       for (int i = 0; i < array1.length ; i++) {
	           if (array1[i] >= 48 && array1[i] <= 57){//数字的ASCII码为48--57
	               count++;
	           }
	       }
	       return count;
	   }
}

文字展示方式


//=======================循环展示====================================
			/*int width = srcImg.getWidth();
			int height = srcImg.getHeight();

			int FONT_SIZE = 12;
			int x = 50;
			int y = 50;
			System.out.println(logoText.length());
			int markWidth = FONT_SIZE * logoText.length();// 字体长度
			int markHeight = 20;// 字体高度	
			
			while (x < width * 1.5) {
				y = -height / 2;

				while (y < height * 1.5) {
					g.drawString(logoText, x, y);
					y += markHeight + (markHeight * 3);
				}
				x += markWidth + (markWidth * 2);//
			}*/
			
//=======================单行居中展示==============================
			/*//int width = srcImg.getWidth();
			int height = srcImg.getHeight();
			FontMetrics fm = g.getFontMetrics(font);
            //int textWidth = fm.stringWidth(logoText);
            // 调整x y--只需调整50 -50两个参数
            int x = -50;
            int y = 50 + (height-fm.getHeight())/2 + fm.getAscent();
          
            g.drawString(logoText, x, y);*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值