Java实现水印功能

一,水印功能,

就是在图片上加上某个标示性的图片,起到宣传作用;一般常见于论坛的上传图片,在上传的图片中某个位置加上自家网站水印标示图,起到很好的宣传作用;

Java实现水印功能很多,因需要自己实现了一个,完成了写到博客中,方便日后使用,懒人计划....

二,实现步骤,

1,实现原理

         * Theory/原理:
         * 1,建立一个画布,设置其边线纹理处理方式;再其上根据原图尺寸画上原图;
         * 2,再设置透明值,计算水印图位置,再在画布的指定位置画上指定大小的水印图;
         * 3,然后混合和处理画布中的图片数据,最后输出到指定目标图片文件;


2,实现代码,

	public static void addWaterMark(String _srcImagePath, String _targetImagePath, 
			String _iconImagePath, String _iconPosition, Float _iconAlpha, 
			Integer _targetIconWidth, Integer _targetIconHeight) {     
		/**
		 * Theory/原理:
		 * 1,建立一个画布,设置其边线纹理处理方式;再其上根据原图尺寸画上原图;
		 * 2,再设置透明值,计算水印图位置,再在画布的指定位置画上指定大小的水印图;
		 * 3,然后混合和处理画布中的图片数据,最后输出到指定目标图片文件;
		 * @auther: shenzhenNBA
		 * @param _srcImagePath is absolute path of source image which need to add water mark, required;
		 * @param _targetImagePath is absolute path of target image which will be 
		 * 			contains water mark icon image, if empty then will save to source image, optional;
		 * 	
		 * @param _iconImagePath is absolute path of water mark icon image, required; 
		 * @param _iconPosition is position of water mark icon in source image, optional and
		 * 			default position is bottom right, value is one of [topLeft,topRight,bottomLeft,bottomRight]; 
		 * @param _iconAlpha is to set alpha value of water mark icon image, optional; 
		 * @param _targetIconWidth is width of target water mark icon image, optional;
		 * @param _targetIconHeight is height of target water mark icon image, optional;
		 * 
		 * usage (example):
		 * CommonFunService.addWaterMark(
		 * 		"c:\path\srcImage.jpg"
		 * 		,"c:\path\targetImage.gif"
		 * 		,"c:\path\iconImage.png"
		 * 		,"bottomright"
		 * 		,new Float(0.8)
		 * 		,0
		 * 		,0
		 * );
		 */
		
		if(null != _srcImagePath && !_srcImagePath.trim().isEmpty()
			&& null != _iconImagePath && !_iconImagePath.trim().isEmpty()){
			
			String srcImagePath = _srcImagePath.trim();
			String targetImagePath = "";
			String iconImagePath = _iconImagePath.trim();
			String iconPosition = "";	
			Integer targetIconWidth = 0; 
			Integer targetIconHeight = 0; 
			Integer imgIconAxisX = 0;
			Integer imgIconAxisY = 0;
			Integer imgIconMargin = 5;
			Float iconAlpha = 0.8f;
			if(null == _targetImagePath || _targetImagePath.trim().isEmpty()){
				targetImagePath = srcImagePath;
			}else{
				targetImagePath = _targetImagePath.trim();
			}
			if(null == _iconPosition || _iconPosition.trim().isEmpty()){
				iconPosition = "bottomRight";
			}else{
				iconPosition = _iconPosition.trim();
			}
			if(null == _iconAlpha){
				iconAlpha = 0.8f;
			}
			if(null != _iconAlpha && _iconAlpha < 0){
				iconAlpha = 0.1f;
			}
			OutputStream os = null;
			try {
				//read and get image data base on parameter
				Image srcImg = ImageIO.read(new File(srcImagePath));
				Integer srcImgWidth = srcImg.getWidth(null);
				Integer srcImgHeight = srcImg.getHeight(null);
				BufferedImage buffImg = new BufferedImage(srcImgWidth,
						srcImgHeight, BufferedImage.TYPE_INT_RGB);
				
				// create or get the canvas of buffer image,创建或得到画布对象
				// Graphics g= buffImg.getGraphics();
				Graphics2D g = buffImg.createGraphics();
	
				// set the sawtooth style of line, 设置对线段的锯齿状边缘处理
				g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
						RenderingHints.VALUE_INTERPOLATION_BILINEAR);
	
				g.drawImage(srcImg.getScaledInstance(srcImgWidth,
						srcImgHeight, Image.SCALE_SMOOTH), 0, 0, null);
				
				/*
				//----------------------------------------------------
				// set the rotate of icon image,no active, 设置水印图的旋转,未启用,
				if (null != degree) {
					g.rotate(Math.toRadians(degree),
							(double) buffImg.getWidth() / 2,
							(double) buffImg.getHeight() / 2);
				}
				//----------------------------------------------------
				*/
				
				
				// read icon image base on parameter, it should be gif or png file normally as which can set alpha
				// 读取水印图象,水印图一般为gif或者png的,这样可设置透明度
				
				// read and get icon image data, width and height ,读取Image数据对象,宽和高
				Image imgIcon = ImageIO.read(new File(iconImagePath));
				Integer imgIconWidth = imgIcon.getWidth(null);
				Integer imgIconHeight = imgIcon.getHeight(null);
				
				if(null == _targetIconWidth){
					targetIconWidth =  imgIconWidth;
				}else{
					targetIconWidth = _targetIconWidth;
				}
				if(targetIconWidth <= 0){
					targetIconWidth = imgIconWidth;
				}
				
				if(null == _targetIconHeight){
					targetIconHeight =  imgIconHeight;
				}else{
					targetIconHeight = _targetIconHeight;
				}
				if(targetIconHeight <= 0){
					targetIconHeight = imgIconHeight;
				}
				
				Image targetIconImage = imgIcon.getScaledInstance(targetIconWidth,
						targetIconHeight, Image.SCALE_SMOOTH);
				
				// set icon image position in source image,设置水印图片印在原图中的位置
				if("bottomRight".equalsIgnoreCase(iconPosition)){
					imgIconAxisX = srcImgWidth - targetIconWidth - imgIconMargin;
					imgIconAxisY= srcImgHeight - targetIconHeight - imgIconMargin;
				}else if("bottomLeft".equalsIgnoreCase(iconPosition)){
					imgIconAxisX = imgIconMargin;
					imgIconAxisY= srcImgHeight - targetIconHeight - imgIconMargin;
				}else if("topRight".equalsIgnoreCase(iconPosition)){
					imgIconAxisX = srcImgWidth - targetIconWidth - imgIconMargin;
					imgIconAxisY= imgIconMargin;
				}else{//topLeft
					imgIconAxisX = imgIconMargin;
					imgIconAxisY= imgIconMargin;
				}
				if(imgIconAxisX < 0){
					imgIconAxisX = (Integer)(srcImgWidth / 2);
				}
				if(imgIconAxisY < 0){
					imgIconAxisX = (Integer)(srcImgHeight / 2);
				}
				
				// set the alpha to icon image,设置水印图的透明度
				g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, iconAlpha));
				
				//draw the target icon image at target position width target size in source image
				//在原图中指定位置绘制指定大小的水印图
				g.drawImage(targetIconImage, imgIconAxisX, imgIconAxisY, null);
				
				g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
				g.dispose();
				
				// create target image which containers icon image,
				// 生成含有在指定位置指定大小加了水印图的新图片
				os = new FileOutputStream(targetImagePath);
				ImageIO.write(buffImg, "JPG", os);
				
				//release system source
				srcImg = null;
				//imageIcon = null;
				imgIcon = null;
				targetIconImage = null;
				buffImg = null;
				g = null;
			} catch (Exception e) {
				//e.printStackTrace();
			} finally {
				try {
					if (null != os){
						os.close();
						os = null;
					}
				} catch (Exception e) {
					//e.printStackTrace();
				}
			}
		}
    }
把该方法放到类中即可使用;

3,使用方式,

		xxxClass.addWaterMark("c:\path\srcImage.jpg",
				,"c:\path\targetImage.gif"
				,"c:\path\iconImage.png"
				,"bottomright"
				,new Float(0.8)
				,0
				,0);
根据设置不同的参数可以得到需要的效果,具体见参数说明;


4,写在最后,

这个功能很多地方可以用到,具有公共方法属性,所以记录下来,日后使用,实际测试可用,可能有些简陋之处,欢迎大家提出指正或改进;当然还有其它的实现方式,如果你知道或有更好的实现方式,不妨回复或留下点滴.....




你可能感兴趣的:(Java,管理者/开发者)