java代碼 圖片鋪滿水印文字

原圖:

 效果圖:

代碼:

	/**
	 * @description
	 * @param sourceImgPath 源圖片路徑
	 * @param tarImgPath 保存的圖片路徑
	 * @param waterMarkContent 水印內容
	 * @param fileExt 圖片格式
     * @param lasterPicture 最新一期圖片存放位置
	 * @return void
	 */
	public void addWatermark(String sourceImgPath, String tarImgPath, String waterMarkContent,String fileExt,String lasterPicture){
		// 水印文字大小
		int FONT_SIZE = 28;
		// 水印之間的間隔
		int XMOVE = 180;
		// 水印之間的間隔
		int YMOVE = 180;
//		Color markContentColor = Color.red;//水印顏色
		Color markContentColor = new Color(105,105,105);//水印顏色
		Integer degree = -45;//設置水印文字的旋轉角度
		float alpha = 0.3f;//設置水印透明度 默認爲1.0  值越小顏色越淺
		OutputStream outImgStream = null;
		OutputStream outImgStream2 = null;
		try {
			File srcImgFile = new File(sourceImgPath);//得到文件
			Image srcImg = ImageIO.read(srcImgFile);//文件轉化爲圖片
			int srcImgWidth = srcImg.getWidth(null);//獲取圖片的寬
			int srcImgHeight = srcImg.getHeight(null);//獲取圖片的高
			if (srcImgWidth < srcImgHeight){
				FONT_SIZE = 18;
				XMOVE = 130;
				YMOVE = 130;
			}
			Font font = new Font("宋體", Font.BOLD, FONT_SIZE);//水印字體,大小
			// 加水印
			BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
			// 得到畫筆對象
			Graphics2D g = bufImg.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) bufImg.getWidth () / 2, (double) bufImg.getHeight () / 2);
			}
			g.setColor(markContentColor); //設置水印顏色
			g.setFont(font);              //設置字體
			// 設置水印文字透明度
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

			int x = -srcImgWidth / 2;
			int y = -srcImgHeight / 2;
			int markWidth = FONT_SIZE * getTextLength (waterMarkContent);// 字體長度
			int markHeight = FONT_SIZE;// 字體高度

			// 循環添加水印
			while (x < srcImgWidth * 1.5) {
				y = -srcImgHeight / 2;
				while (y < srcImgHeight * 1.5) {
					g.drawString (waterMarkContent, x, y);

					y += markHeight + YMOVE;
				}
				x += markWidth + XMOVE;
			}
			// 釋放資源
			g.dispose ();
			// 輸出圖片
			outImgStream = new FileOutputStream(tarImgPath);
			outImgStream2 = new FileOutputStream(lasterPicture);
			ImageIO.write(bufImg, fileExt, outImgStream);
            ImageIO.write(bufImg, fileExt, outImgStream2);
		} catch (Exception e) {
			e.printStackTrace();
			e.getMessage();
		} finally{
			try {
				if(outImgStream != null){
					outImgStream.flush();
					outImgStream.close();
				}
				if(outImgStream2 != null){
					outImgStream2.flush();
					outImgStream2.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
				e.getMessage();
			}
		}
	}

	/**
	 * 獲取文本長度。漢字爲1:1,英文和數字爲2:1
	 */
	private 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;
	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章