⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 watermarkutil.java

📁 java watermark to a picture.
💻 JAVA
字号:
package nx.flat.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 
 * @author lulu
 * @两种方法:doWaterPic添加Logo图片水印,doWaterWord添加Logo文字水印
 */

public class WaterMarkUtil {

	/**
	 * @param 水印图片
	 * @param srcPath(上传图片)
	 * @param aimPath(logo图片)
	 * @throws IOException
	 */

	public static void doWaterPic(String srcPath, String aimPath,float alpha ) {
		// 源图片
		BufferedImage originImg;
		try {
			originImg = ImageIO.read(new File(srcPath));
			// 目标图片
			BufferedImage waterMarkImg = ImageIO.read(new File(aimPath));
			// 置于右下角
			int x = originImg.getWidth() - waterMarkImg.getWidth();
			int y = originImg.getHeight() - waterMarkImg.getHeight();
			//int x = 0;
			//int y = 0;
			addWatermark(originImg, waterMarkImg, x, y, alpha);

			// 保存
			try {
				FileOutputStream out = new FileOutputStream(srcPath);
				JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
				encoder.encode(originImg);
				out.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

	/**
	 * @param 水印图片
	 * @param originImg(上传图片源)
	 * @param watermarkImg(logo图片源)
	 * @param x(位置)
	 * @param y
	 * @param alpha(透明度)
	 * @throws ImageFormatException
	 * @throws IOException
	 */
	public static void addWatermark(BufferedImage originImg,
			Image watermarkImg, int x, int y, float alpha)
			throws ImageFormatException, IOException {
		// 处理水印图片
		int width = watermarkImg.getWidth(null);
		int height = watermarkImg.getHeight(null);
		// 和水印文件同大小的BufferdImage
		BufferedImage tempImg = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_ARGB);

		Graphics2D g2d = tempImg.createGraphics();
		g2d.drawImage(watermarkImg, 0, 0, null);
		g2d.setColor(new Color(1.0f, 1.0f, 1.0f, alpha));
		g2d.setComposite(AlphaComposite.DstIn);
		g2d.fillRect(0, 0, width, height);
		g2d.dispose();

		g2d = originImg.createGraphics();
		g2d.drawImage(tempImg, x, y, null);
		g2d.dispose();

	}

	/** */
	/**
	 * 给图片添加文字水印
	 * 
	 * @param srcPath
	 *            需要添加水印的图片的路径
	 * @param s
	 *            水印的文字
	 * @return
	 */
	public boolean doMarkWord(String srcPath, String s,float alphaValue) {
		// 上传图片
		ImageIcon imgIcon = new ImageIcon(srcPath);
		Image theImg = imgIcon.getImage();
		int width = theImg.getWidth(null);
		int height = theImg.getHeight(null);

		BufferedImage bimage = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);

		Graphics2D g = bimage.createGraphics();
		g.drawImage(theImg, 0, 0, null);

		g.setColor(Color.white);
		// g.setBackground(Color.white);
		// 设置透明度
		AlphaComposite alpha = AlphaComposite.getInstance(
				AlphaComposite.SRC_OVER, alphaValue);
		g.setComposite(alpha);

		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

		// font
		FontMetrics fontMetrics = g.getFontMetrics();
		Rectangle2D rect = fontMetrics.getStringBounds(s, g);

		// 设置文字样式,样式,颜色,大小等
		AttributedString ats = new AttributedString(s);
		Font f = new Font("黑体", Font.BOLD, 23);
		ats.addAttribute(TextAttribute.FONT, f, 0, s.length());
		AttributedCharacterIterator iter = ats.getIterator();

		// 添加在右下角
		g.drawString(iter, (int) (width - rect.getWidth()) / 10 * 8,
				(int) (height - rect.getHeight()));
		// 添加水印的文字和设置水印文字出现的内容
		g.dispose();

		// 保存
		try {
			FileOutputStream out = new FileOutputStream(srcPath);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
			// 图片质量
			param.setQuality(70f, true);
			encoder.encode(bimage, param);
			out.close();
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	
	/**
	 * @param 给学校和专业加水印图片
	 * @param srcPath(上传图片)
	 * @param aimPath(logo图片)
	 * @throws IOException
	 * @author denny.li
	 */

	public static void doWaterPicForBase(String srcPath, String aimPath,float alpha ) {
		try {
			
			//源图片
			BufferedImage originImg = ImageIO.read(new File(srcPath));
			int width = originImg.getWidth(null);
			int height = originImg.getHeight(null);
			//水印图片
			BufferedImage waterMarkImg = ImageIO.read(new File(aimPath));
			int lw=waterMarkImg.getWidth(null);
			int lh=waterMarkImg.getHeight(null);
			
			//水印的临时图
			//临时图片,主要画水印内容,设置属性,按照源图的大小画水印内容
			BufferedImage tempImg =  new BufferedImage(width,height,
					BufferedImage.TYPE_INT_ARGB);
			//画水印的个数
			int num=1;
			if(height%lh==0){
				num=height/lh;
			}else{
				num=height/lh+1;
			}
			
			Graphics2D g2d = tempImg.createGraphics();
			for(int i=0;i<num;i++){
				g2d.drawImage(waterMarkImg,0,i*lh,null);
				
			}
			g2d.setColor(new Color(1.0f, 1.0f, 1.0f, alpha));
			g2d.setComposite(AlphaComposite.DstIn);
			g2d.fillRect(0,0,width,height);
			g2d.dispose();
			
		
			//目标图片
			BufferedImage objectImg =  new BufferedImage(width,height,
					BufferedImage.TYPE_INT_ARGB);
			
			Graphics2D g2dnew = objectImg.createGraphics();
			
			g2dnew.drawImage(originImg,0,0,null);
			g2dnew.drawImage(tempImg,0,0,null);
			
			g2d.fillRect(0,0,width,height);
			g2dnew.dispose();

			// 保存
			try {
				FileOutputStream out = new FileOutputStream(srcPath);
				AnimatedGifEncoder e = new AnimatedGifEncoder();
				e.start(out);
				e.setDelay(1000); // 1 frame per sec
				e.addFrame(objectImg);
				e.finish();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}
	
	
	/**
	 * @param 给学校和专业加水印图片
	 * @param srcPath(上传图片)
	 * @param aimPath(logo图片)
	 * @throws IOException
	 * @author denny.li
	 */

	public static void doWaterPicTest(String srcPath, String aimPath,String objPath,float alpha) {
		
		
		try {
			
			//源图片
			BufferedImage originImg = ImageIO.read(new File(srcPath));
			int width = originImg.getWidth(null);
			int height = originImg.getHeight(null);
			
			
			//水印图片
			BufferedImage waterMarkImg = ImageIO.read(new File(aimPath));
			int lw=waterMarkImg.getWidth(null);
			int lh=waterMarkImg.getHeight(null);
			
			
			
			//水印的临时图
			//临时图片,主要画水印内容,设置属性,按照源图的大小画水印内容
			BufferedImage tempImg =  new BufferedImage(width,height,
					BufferedImage.TYPE_INT_ARGB);
			
			
			//画水印的个数
			int num=1;
			if(height%lh==0){
				num=height/lh;
			}else{
				num=height/lh+1;
			}
			
			Graphics2D g2d = tempImg.createGraphics();
			for(int i=0;i<num;i++){
				g2d.drawImage(waterMarkImg,0,i*lh,null);
				
			}
			g2d.setColor(new Color(1.0f, 1.0f, 1.0f, alpha));
			g2d.setComposite(AlphaComposite.DstIn);
			g2d.fillRect(0,0,width,height);
			g2d.dispose();
			
			//源文件的临时图
			
/*			BufferedImage tempImg1 =  new BufferedImage(width,height,
					BufferedImage.TYPE_INT_ARGB);
			
			
			Graphics2D g2d1 = tempImg1.createGraphics();
			
			g2d1.drawImage(originImg,0,0,null);
				

			g2d1.setColor(new Color(1.0f, 1.0f, 1.0f, alpha));
			g2d1.setComposite(AlphaComposite.DstIn);
			g2d1.fillRect(0,0,width,height);
			g2d1.dispose();*/
			
			
			//目标图片
			BufferedImage objectImg =  new BufferedImage(width,height,
					BufferedImage.TYPE_INT_ARGB);
			
			Graphics2D g2dnew = objectImg.createGraphics();
			//第一方案,先原图,后水印
			g2dnew.drawImage(originImg,0,0,null);
			g2dnew.drawImage(tempImg,0,0,null);
			//第二方案,先水印,后原图
			//g2dnew.drawImage(tempImg,0,0,null);
			//g2dnew.setColor(new Color(1.0f, 1.0f, 1.0f, alpha));
			//g2dnew.setComposite(AlphaComposite.DstIn);
			//g2dnew.drawImage(originImg,0,0,null);
			//第三方案,两个图都重新画一遍
			//g2dnew.drawImage(tempImg,0,0,null);
			//g2dnew.drawImage(tempImg1,0,0,null);
			
			
			g2d.fillRect(0,0,width,height);
			

			
			
			//g2d.setColor(new Color(1.0f, 1.0f, 1.0f, alpha));
			//g2d.setComposite(AlphaComposite.DstIn);
			//g2d.setBackground(new Color(Color.TRANSLUCENT));
			//g2d.drawImage(waterMarkImg,0,0,width,height, null);
			
			
			g2dnew.dispose();

			

			// 保存
			try {
				FileOutputStream out = new FileOutputStream(objPath);
				
				AnimatedGifEncoder e = new AnimatedGifEncoder();
				e.start(out);
				e.setDelay(1000); // 1 frame per sec
				e.addFrame(objectImg);
				e.finish();
				/*JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
				encoder.encode(originImg);
				out.close();*/
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}
	
	
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -