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();
			//参数主要是,原图,坐标&
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值