Java图片添加水印工具

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ck3345143/article/details/88667628

Java图片添加水印工具

废话少说,直接上代码:

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
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 ImgUtil {
	public static void main(String[] args) {
		String srcImgPath = "D:/test/123.jpg";
		String iconPath = "d:test/printtext.png";
		pressImage(srcImgPath, iconPath);
		pressText(srcImgPath, "水印内容说明。如(仅限xxx使用,他用无效)", 0, Color.red);
	}
	public static boolean pressImage(String targetImg, String waterImg) {
		try {
			File file = new File(targetImg);
			Image image = ImageIO.read(file);
			int width = image.getWidth(null);
			int height = image.getHeight(null);
			BufferedImage bufferedImage = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g = bufferedImage.createGraphics();
			g.drawImage(image, 0, 0, width, height, null);
			Image waterImage = ImageIO.read(new File(waterImg)); // 水印文件
			int width_1 = waterImage.getWidth(null);
			int height_1 = waterImage.getHeight(null);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
					0.5f));
			int widthDiff = width - width_1;
			int heightDiff = height - height_1;
			int x = widthDiff / 2;// 水印位置
			int y = heightDiff / 2;// 水印位置
			g.drawImage(waterImage, x, y, width_1, height_1, null); // 水印文件结束
			g.dispose();
			ImageIO.write(bufferedImage, "png", file);
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 照片添加水印
	 * @param targetImg 原始照片路径
	 * @param pressText 水印文字
	 * @param fontSize 文字大小 <=0时字体大小自适应
	 * @param color 文字颜色
	 * @return
	 */
	public static Boolean pressText(String targetImg, String pressText, int fontSize, Color color) {
		try {
			File file = new File(targetImg);

			Image image = ImageIO.read(file);
			int width = image.getWidth(null);
			int height = image.getHeight(null);
			BufferedImage bufferedImage = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g = bufferedImage.createGraphics();
			g.drawImage(image, 0, 0, width, height, null);
			if (fontSize<=0) {
				fontSize = 4*width/(5*pressText.length());
			}
			g.setFont(new Font("宋体", Font.PLAIN, fontSize));
			g.setColor(color);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
					0.5f));
			int width_1 = fontSize * getLength(pressText);
			int height_1 = fontSize;
			int widthDiff = width - width_1;
			int heightDiff = height - height_1;
			int x = widthDiff / 2;//水印位置
			int y = heightDiff / 2;
			g.drawString(pressText, x, y + height_1);
			g.dispose();
			ImageIO.write(bufferedImage, "jpg", file);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public static int getLength(String text) {
		int textLength = text.length();
		int length = textLength;
		for (int i = 0; i < textLength; i++) {
			if (String.valueOf(text.charAt(i)).getBytes().length > 1) {
				length++;
			}
		}
		return (length % 2 == 0) ? length / 2 : length / 2 + 1;
	}
}

猜你喜欢

转载自blog.csdn.net/ck3345143/article/details/88667628
今日推荐