一、给图片添加文本水印

1、代码

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class AddTextWatermark {

	private static void addTextWatermark(String text, String type, File source, File destination) throws IOException{
		BufferedImage image = ImageIO.read(source);
		
		int imageType = BufferedImage.TYPE_INT_RGB;
		if("png".equals(type)){
			//PNG图像支持透明度
			imageType = BufferedImage.TYPE_INT_ARGB;
		}
		BufferedImage watermark = new BufferedImage(image.getWidth(), image.getHeight(), imageType);
		
		Font font = new Font(Font.SANS_SERIF, Font.BOLD, 20);
		Graphics2D graphics2d = (Graphics2D) watermark.getGraphics();
		graphics2d.drawImage(image, 0, 0, null);
		AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
		graphics2d.setComposite(alphaComposite);
		graphics2d.setFont(font);
		
		//位置
		FontMetrics fontMetrics = graphics2d.getFontMetrics();
		Rectangle2D textRect = fontMetrics.getStringBounds(text, graphics2d);
		int width = (int)textRect.getWidth();
		int height = (int)textRect.getHeight();
		int x = image.getWidth() - width;
		int y = image.getHeight() - height;
		
		graphics2d.setColor(new Color(205, 206, 208));
		graphics2d.fillRect(x, y, width, height);
		
		graphics2d.setColor(Color.white);
		graphics2d.drawString(text, x, image.getHeight() - 5);
		ImageIO.write(watermark, type, destination);
		graphics2d.dispose();
	}
	
	public static void main(String[] args) throws IOException {
		String text = "albertbamboo";
		File source = new File("G://test.png");
		File destination = new File("G://test-watermark.png");
		addTextWatermark(text, "png", source, destination);
	}
}

2、效果图

  • 原图

  • 处理之后

二、给图片添加图像水印

1、代码

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class AddImageWatermark {

	private static BufferedImage resize(BufferedImage image, int width, int height){
		Image temp = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
		BufferedImage resizedImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		Graphics2D graphics2d = resizedImg.createGraphics();
		graphics2d.drawImage(temp, 0, 0, null);
		graphics2d.dispose();
		return resizedImg;
	}
	
	private static void addImageWatermark(File watermark, String type, File source, File destination) throws IOException{
		BufferedImage image = ImageIO.read(source);
		BufferedImage overlay = resize(ImageIO.read(watermark), 100, 35);
		
		int imageType = BufferedImage.TYPE_INT_RGB;
		if("png".equals(type)){
			//PNG图像支持透明度
			imageType = BufferedImage.TYPE_INT_ARGB;
		}
		
		BufferedImage watermarkImg = new BufferedImage(image.getWidth(), image.getHeight(), imageType);
		Graphics2D graphics2d = (Graphics2D) watermarkImg.getGraphics();
		graphics2d.drawImage(image, 0, 0, null);
		AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
		graphics2d.setComposite(alphaComposite);
		
		graphics2d.drawImage(overlay, image.getWidth() - overlay.getWidth(), image.getHeight() - overlay.getHeight(), null);
		ImageIO.write(watermarkImg, type, destination);
		graphics2d.dispose();
	}
	
	public static void main(String[] args) throws IOException {
		File watermark = new File("G://watermark.png");
		File source = new File("G://test.png");
		File destination = new File("G://test-watermark.png");;
		addImageWatermark(watermark, "png", source, destination);
	}
}

2、效果图

  • 原图

  • 处理之后

参考资料:

Adding Text or Image as Watermark to an Image in Java