1. 程式人生 > >JAVA 給圖片新增水印,可設定大小、位置、透明度

JAVA 給圖片新增水印,可設定大小、位置、透明度

/*******************************************************************************
 * Description: 圖片水印工具類
 * 
 * @author xcwc1995
 * @version 1.0
 */
public class ImageRemarkUtil {

	// 水印透明度
	private static float alpha = 1f;
	// 水印橫向位置
	private static int positionWidth = 0;
	// 水印縱向位置
	private static int positionHeight = 0;

	/**
	 * 
	 * @param alpha
	 *            水印透明度
	 * @param positionWidth
	 *            水印橫向位置
	 * @param positionHeight
	 *            水印縱向位置
	 */
	public static void setImageMarkOptions(float alpha, int positionWidth,
			int positionHeight, Font font, Color color) {
		if (alpha != 0.0f)
			ImageRemarkUtil.alpha = alpha;
		if (positionWidth != 0)
			ImageRemarkUtil.positionWidth = positionWidth;
		if (positionHeight != 0)
			ImageRemarkUtil.positionHeight = positionHeight;
	}

	/**
	 * 給圖片新增水印圖片
	 * 
	 * @param iconPath
	 *            水印圖片路徑
	 * @param srcImgPath
	 *            源圖片路徑
	 * @param targerPath
	 *            目標圖片路徑
	 * @param height
	 * @param width
	 */
//	public static void markImageByIcon(String iconPath, String srcImgPath,
//			String targerPath,String watermarkPosition,int width, int height) {
//		markImageByIcon(iconPath, srcImgPath, targerPath,watermarkPosition,width, height);
//	}

	/**
	 * 給圖片新增水印圖片、可設定水印圖片旋轉角度
	 * 
	 * @param iconPath
	 *            水印圖片路徑
	 * @param srcImgPath
	 *            源圖片路徑
	 * @param targerPath
	 *            目標圖片路徑
	 * @param degree
	 *            水印圖片旋轉角度
	 * @param markAlpha 
	 */
	public static void markImageByIcon(String iconPath, String srcImgPath,
			String targerPath, Integer degree, String watermarkPosition,float markAlpha, int width, int height) {
		OutputStream os = null;
		try {

			Image srcImg = ImageIO.read(new File(srcImgPath));

			BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
					srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
			int srcWidth=srcImg.getWidth(null);
            int srcHeight=srcImg.getHeight(null);
			// 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();
			int watermarkImageWidth=img.getWidth(null);
            int watermarkImageHeight=img.getHeight(null);
            double dmarkWidth = width*0.4;// 設定水印的寬度為圖片寬度的0.4倍
			double dmarkHeight = dmarkWidth * ((double)watermarkImageHeight/(double)watermarkImageWidth);//強轉為double計算寬高比例
			int imarkWidth = (int) dmarkWidth;
			int imarkHeight = (int) dmarkHeight;
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,	markAlpha));
			// 6、水印圖片的位置
			int x;
			int y;
			switch (watermarkPosition) {
			case "topLeft":
				x = 0;
				y = 0;
				break;
			case "topRight":
				x = srcWidth - imarkWidth;
				y = 0;
				break;
			case "topCenter":
				x = (srcWidth - imarkWidth) / 2;
				y = 0;
				break;
			case "center":
				x = (srcWidth - imarkWidth) / 2;
				y = (srcHeight - imarkHeight) / 2;
				break;
			case "centerLeft":
				x = 0;
				y = (srcHeight - imarkHeight) / 2;
				break;
			case "centerRight":
				x = srcWidth - imarkWidth;
				y = (srcHeight - imarkHeight) / 2;
				break;
			case "bottomLeft":
				x = 0;
				y = srcHeight - imarkHeight;
				break;
			case "bottomCenter":
				x = (srcWidth - imarkWidth) / 2;
				y = srcHeight - imarkHeight;
				break;
			case "bottomRight":
				x = srcWidth - imarkWidth;
				y = srcHeight - imarkHeight;
				break;
			default:
				x = srcWidth - imarkWidth;
				y = srcHeight - imarkHeight;
				break;
			}
			
//			double dmarkWidth = width*0.4;// 設定水印的寬度為圖片寬度的0.4倍
//			double dmarkHeight = dmarkWidth * ((double)watermarkImageHeight/(double)watermarkImageWidth);//強轉為double計算寬高比例
//			int imarkWidth = (int) dmarkWidth;
//			int imarkHeight = (int) dmarkHeight;
			//positionWidth = (int) (width - dmarkWidth);
		//	positionHeight = (int) (height - dmarkHeight);
			
			
			
			g.drawImage(img, x, y, imarkWidth,	imarkHeight, null);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
			// 7、釋放資源
			g.dispose();
			// 8、生成圖片
			os = new FileOutputStream(targerPath);
			ImageIO.write(buffImg, "JPG", os);
			System.out.println("圖片完成新增水印圖片");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != os)
					os.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
}