java png添加水印
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import javax.imageio.ImageIO;
import com.yunwei.boss.common.utils.DateUtil;
public class PNGUtil {
/**
* 生成背景透明的 文字水印,文字位于透明区域正中央,可设置旋转角度
*
* @param width 生成图片宽度
* @param heigth 生成图片高度
* @param text 水印文字
* @param color 颜色对象
* @param font awt字体
* @param degree 水印文字旋转角度
* @param alpha 水印不透明度0f-1.0f
*/
public static BufferedImage waterMarkByText(int width, int heigth, String text, Color color,
Font font, Double degree, float alpha) {
BufferedImage buffImg = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_RGB);
/**2、得到画笔对象*/
Graphics2D g2d = buffImg.createGraphics();
// ---------- 增加下面的代码使得背景透明 -----------------
buffImg = g2d.getDeviceConfiguration()
.createCompatibleImage(width, heigth, Transparency.TRANSLUCENT);
g2d.dispose();
g2d = buffImg.createGraphics();
// ---------- 背景透明代码结束 -----------------
// 设置对线段的锯齿状边缘处理
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
// 把源图片写入
// Image image = Image.getInstance("D:/20191023114538.png");
// g2d.drawImage(image.getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_SMOOTH), 0, 0,null);
// 设置水印旋转
if (null != degree) {
//注意rotate函数参数theta,为弧度制,故需用Math.toRadians转换一下
//以矩形区域中央为圆心旋转
g2d.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2,
(double) buffImg.getHeight() / 2);
}
// 设置颜色
g2d.setColor(color);
// 设置 Font
g2d.setFont(font);
//设置透明度:1.0f为透明度 ,值从0-1.0,依次变得不透明
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
//获取真实宽度
float realWidth = getRealFontWidth(text);
float fontSize = font.getSize();
//计算绘图偏移x、y,使得使得水印文字在图片中居中
//这里需要理解x、y坐标是基于Graphics2D.rotate过后的坐标系
float x = 0.5f * width - 0.5f * fontSize * realWidth;
float y = 0.5f * heigth + 0.5f * fontSize;
//取绘制的字串宽度、高度中间点进行偏移,使得文字在图片坐标中居中
g2d.drawString(text, x, y);
//释放资源
g2d.dispose();
// System.out.println("添加水印文字完成!");
return buffImg;
}
public static BufferedImage waterMarkByText(int width, int heigth, String text, Color color, float alpha) {
//jdk默认字体
Font font = new Font("Dialog", Font.ROMAN_BASELINE, 33);
return waterMarkByText(width, heigth, text, color,font, -30d, alpha);
}
public static BufferedImage waterMarkByText(int width, int heigth, String text, float alpha) {
return waterMarkByText(width, heigth, text, Color.GRAY, alpha);
}
public static BufferedImage waterMarkByText(int width, int heigth, String text) {
return waterMarkByText(width, heigth, text, 0.2f);
}
public static BufferedImage waterMarkByText(String text) {
return waterMarkByText(200, 150, text);
}
/**
* 获取真实字符串宽度,ascii字符占用0.5,中文字符占用1.0
*/
private static float getRealFontWidth(String text) {
int len = text.length();
float width = 0f;
for (int i = 0; i < len; i++) {
if (text.charAt(i) < 256) {
width += 0.5f;
} else {
width += 1.0f;
}
}
return width;
}
/**
* 图片覆盖(覆盖图压缩到width*height大小,覆盖到底图上)
*
* @param baseBufferedImage 底图
* @param coverBufferedImage 覆盖图
* @param x 起始x轴
* @param y 起始y轴
* @param width 覆盖宽度
* @param height 覆盖长度度
* @return
* @throws Exception
*/
public static BufferedImage coverImage(BufferedImage baseBufferedImage, BufferedImage coverBufferedImage, int x, int y, int width, int height) throws Exception{
// 创建Graphics2D对象,用在底图对象上绘图
Graphics2D g2d = baseBufferedImage.createGraphics();
// 绘制
g2d.drawImage(coverBufferedImage, x, y, width, height, null);
// g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f)); // 1.0f为透明度 ,值从0-1.0,依次变得不透明
g2d.dispose();// 释放图形上下文使用的系统资源
return baseBufferedImage;
}
/**
* 图片上添加新图片
* @param baseImgUrl 底图地址 D:/00.png
* @param coverImgUrl 覆盖地址
* @param newImgUrl 新图片生成地址
*/
public static void pngCoverPng(String baseImgUrl, String coverImgUrl, String newImgUrl) throws Exception{
int width = 400;
int heigth = 400;
File baseFile = new File(baseImgUrl);//底图
BufferedImage buffImg = ImageIO.read(baseFile);
File coverFile = new File(coverImgUrl); //覆盖层
BufferedImage coverImg = ImageIO.read(coverFile);
buffImg = coverImage(buffImg, coverImg, 0, 0, width, heigth);
buffImg = coverImage(buffImg, coverImg, 200, 200, width, heigth);
buffImg = coverImage(buffImg, coverImg, 400, 400, width, heigth);
buffImg = coverImage(buffImg, coverImg, 600, 600, width, heigth);
buffImg = coverImage(buffImg, coverImg, 600, 0, width, heigth);
buffImg = coverImage(buffImg, coverImg, 600, 200, width, heigth);
buffImg = coverImage(buffImg, coverImg, 0, 600, width, heigth);
buffImg = coverImage(buffImg, coverImg, 200, 800, width, heigth);
buffImg = coverImage(buffImg, coverImg, 200, 1000, width, heigth);
ImageIO.write(buffImg, "png", new File(newImgUrl));//写入文件
if(null != coverImg) {
coverImg.flush();
}
}
/**
* 给图片添加水印文字、可设置水印文字的旋转角度
* @param logoText 要写入的文字
* @param srcImgPath 源图片路径
* @param newImagePath 新图片路径
* @param fontsize 字体大小
* @param color 字体颜色
* @param formaName 图片后缀
*/
@SuppressWarnings("unused")
public static void markImageByText(String srcImgPath, String newImagePath) throws Exception{
String dateString = DateUtil.y_m_s(new Date()); // 需要加入的水印文字 (时间)
InputStream is = null;
OutputStream os = null;
// 1、源图片
java.awt.Image srcImg = ImageIO.read(new File(srcImgPath));
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
// 2、得到画笔对象
Graphics2D g = buffImg.createGraphics();
buffImg = g.getDeviceConfiguration().createCompatibleImage(srcImg.getWidth(null), srcImg.getHeight(null), Transparency.TRANSLUCENT);
g.dispose();
g = buffImg.createGraphics();
// 3、设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
// 4、把原图画到新画板上
g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), java.awt.Image.SCALE_SMOOTH), 0, 0, null);
// 5、设置水印文字颜色
g.setColor(Color.blue);
// 6、设置水印文字Font
g.setFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 20));
// 7、设置水印文字透明度 必须要用AlphaComposite.SRC_OVER
// g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP));
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f)); // 1.0f为透明度 ,值从0-1.0,依次变得不透明
// 8、设置水印旋转
g.rotate(Math.toRadians(-30),(double)buffImg.getWidth(), (double)buffImg.getHeight());
// 9、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
g.drawString(dateString, 250 , 135);
// 10、释放资源
g.dispose();
// 11、生成图片
os = new FileOutputStream(newImagePath);
ImageIO.write(buffImg, "png", os);
if (null != is) {
is.close();
}
if (null != os) {
os.close();
}
}
public static void main(String[] args) {
// try {
// String srcImgPath = "D:/pdfUtil/utilImg/conver1.png";
// String newImagePath = "D:/pdfUtil/conver_img/2019-10-24.png";
// markImageByText(srcImgPath, newImagePath);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// String baseImgUrl = "D:/pdfUtil/utilImg/conver0.png";
// String coverImgUrl = "D:/pdfUtil/conver_img/2019-10-24.png";
// String newImgUrl = "D:/pdfUtil/conver_pdf/2019-10-24.png";
// PNGUtil.pngCoverPng(baseImgUrl, coverImgUrl, newImgUrl);
// int width = 328;
// int heigth = 328;
// Font font = new Font("微软雅黑", Font.ROMAN_BASELINE, 33);//字体
BufferedImage bi1 = waterMarkByText(width, heigth, "测试aB~,", Color.GRAY, font, -30d,
0.2f);//给图片添加文字水印
BufferedImage bi = waterMarkByText(width, heigth, "测试aB~,", Color.GRAY, -30d,
0.2f);//给图片添加文字水印
BufferedImage bi2 = waterMarkByText(width, heigth, "测试aB~,");//给图片添加文字水印
BufferedImage bi3 = waterMarkByText("测试aB~,");
// try {
//
//
// File baseFile = new File("D:/00.png");//底图
// BufferedImage buffImg = ImageIO.read(baseFile);
//
// File coverFile = new File("D:/2.png"); //覆盖层
// BufferedImage coverImg = ImageIO.read(coverFile);
//
// buffImg = coverImage(buffImg, coverImg, 80, 0, width, heigth);
// buffImg = coverImage(buffImg, coverImg, 130, 300, width, heigth);
// buffImg = coverImage(buffImg, coverImg, 180, 180, width, heigth);
// buffImg = coverImage(buffImg, coverImg, 220, 420, width, heigth);
// buffImg = coverImage(buffImg, coverImg, 280, 15, width, heigth);
//
// ImageIO.write(buffImg, "png", new File("D:/03.png"));//写入文件
//
//
ImageIO.write(bi1, "png", new File("D:/test.png"));//写入文件
// System.out.println(Color.decode("#00bcd4"));
// } catch (Exception e) {
// e.printStackTrace();
// }
}
}