package com.yonge.toolset.utils.image;

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 WatermarkUtils {

	/**
	 * Embeds a textual watermark over a source image to produce
	 * a watermarked one.
	 * @param text The text to be embedded as watermark.
	 * @param sourceImageFile The source image file.
	 * @param destImageFile The output image file.
	 * @throws IOException 
	 */
	public static void addTextWatermark(String text, File sourceImageFile, File destImageFile) throws IOException {

		BufferedImage sourceImage = ImageIO.read(sourceImageFile);
		Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();

		// initializes necessary graphic properties
		AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f);
		g2d.setComposite(alphaChannel);
		g2d.setColor(Color.BLUE);
		g2d.setFont(new Font("Arial", Font.BOLD, 64));
		FontMetrics fontMetrics = g2d.getFontMetrics();
		Rectangle2D rect = fontMetrics.getStringBounds(text, g2d);

		// calculates the coordinate where the String is painted
		int centerX = (sourceImage.getWidth() - (int) rect.getWidth()) / 2;
		int centerY = sourceImage.getHeight() / 2;

		// paints the textual watermark
		g2d.drawString(text, centerX, centerY);

		ImageIO.write(sourceImage, "png", destImageFile);
		g2d.dispose();

	}

	/**
	 * Embeds an image watermark over a source image to produce
	 * a watermarked one.
	 * @param watermarkImageFile The image file used as the watermark.
	 * @param sourceImageFile The source image file.
	 * @param destImageFile The output image file.
	 * @throws IOException 
	 */
	public static void addImageWatermark(File watermarkImageFile, File sourceImageFile, File destImageFile) throws IOException {
		BufferedImage sourceImage = ImageIO.read(sourceImageFile);
		BufferedImage watermarkImage = ImageIO.read(watermarkImageFile);

		// initializes necessary graphic properties
		Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();
		AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
		g2d.setComposite(alphaChannel);

		// calculates the coordinate where the image is painted
		int topLeftX = (sourceImage.getWidth() - watermarkImage.getWidth()) / 2;
		int topLeftY = (sourceImage.getHeight() - watermarkImage.getHeight()) / 2;

		// paints the image watermark
		g2d.drawImage(watermarkImage, topLeftX, topLeftY, null);

		ImageIO.write(sourceImage, "png", destImageFile);
		g2d.dispose();
	}
}
