Java实现对image图片、pdf文件加水印

1、图片

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class ImageWatermark {

    // 旋转角度(单位:弧度)
	private static final double ROTATE_ANGLE_RADIANS = Math.toRadians(30);  
	private static final double SIN_ROTATE_ANGLE = Math.sin(ROTATE_ANGLE_RADIANS);
	private static final double COS_ROTATE_ANGLE = Math.cos(ROTATE_ANGLE_RADIANS);

	//定义水印文字样式
	private static final Color FONT_COLOR = new Color(0x70, 0x70, 0x70);
    //宋体  微软雅黑
	private static final String FONT_NAME = "宋体";
	private static final int FONT_STYLE = Font.BOLD;
	private static final float ALPHA = 0.2F;

    public static void main(String[] args) throws IOException {
        ImageWatermark.paintWatermarkImage(inputPath, outPutPath);
    }

    /**
	 * 
	 * @param input 图片原始路径
	 * @param outPutPath 图片加水印之后的路径
	 */
    public static void paintWatermarkImage(String input, String outPutPath) {
		FileOutputStream bos = null;
		String text = "水印内容\n换行符换行";
		Image image = null;
		try {
			image = ImageIO.read(new File(input));
		} catch (IOException e) {
			e.printStackTrace();
		}
		//计算原始图片宽度长度
		int width = image.getWidth(null);
		int height = image.getHeight(null);

		// 三角函数算出来的最小宽高,不浪费内存
		width = (int) (width * COS_ROTATE_ANGLE + height * SIN_ROTATE_ANGLE);
		height = (int) (width * SIN_ROTATE_ANGLE + height * COS_ROTATE_ANGLE);
		BufferedImage bufferedImage =null;
		try {
			//创建图片缓存对象
			bufferedImage= new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			//创建java绘图工具对象
			Graphics2D g = bufferedImage.createGraphics();
			//参数主要是,原图,坐标&

你可能感兴趣的:(java)