AddWatermarkUtils
package com.xiaoqiu.test.utils;
import cn.hutool.core.date.DateUtil;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
/**
* 图片添加水印
*
* @author qzz
*/
public class AddWatermarkUtils {
public static void main(String[] args) throws IOException {
//1.获取网络图片
String onlineUrl = "http://i0.hdslb.com/bfs/article/4a1e5d9833f4ac81eddea79b052b5e58fdd22ab0.jpg";
//2.设置水印文字(自定义)
String watermark = "位置*经度*纬度*日期";
//3.获取添加水印后的图片
BufferedImage bufferedImage = onLine(onlineUrl, watermark);
//4.将图片转为base64位
bfferedImageToBase64(bufferedImage);
//打印结果
System.out.println(bfferedImageToBase64(bufferedImage));
}
/**
* 本地地址添加水印
*
* @param localPath
* @throws IOException
*/
public static BufferedImage local(String localPath, String text) throws IOException {
// 读取原图片信息 得到文件(本地图片)
File srcImgFile = new File(localPath);
//将文件对象转化为图片对象
Image srcImg = ImageIO.read(srcImgFile);
//获取图片的宽
int srcImgWidth = srcImg.getWidth(null);
//获取图片的高
int srcImgHeight = srcImg.getHeight(null);
//添加水印
return addWatermark(srcImg, srcImgWidth, srcImgHeight, text);
}
/**
* 网络图片添加水印
*
* @param urlLink 网路图片链接
* @return
* @throws IOException
*/
public static BufferedImage onLine(String urlLink, String text) throws IOException {
//创建一个URL对象,获取网络图片的地址信息(网络图片)
URL url = new URL(urlLink);
//将URL对象输入流转化为图片对象 (url.openStream()方法,获得一个输入流)
Image srcImg = ImageIO.read(url.openStream());
//获取图片的宽
int srcImgWidth = srcImg.getWidth(null);
//获取图片的高
int srcImgHeight = srcImg.getHeight(null);
//添加水印
return addWatermark(srcImg, srcImgWidth, srcImgHeight, text);
}
/**
* 添加水印
*
* @param srcImg 源图片
* @param srcImgWidth 图片宽度
* @param srcImgHeight 图片高度
* @param text 水印文字
* @return
* @throws IOException
*/
public static BufferedImage addWatermark(Image srcImg, int srcImgWidth, int srcImgHeight, String text) throws IOException {
//1.源图片
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
//2.加水印
//①创建画笔
Graphics2D g = bufImg.createGraphics();
//②绘制原始图片
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
//根据图片的背景设置水印颜色
g.setColor(new Color(23, 12, 16));
//设置字体 画笔字体样式为微软雅黑,加粗,文字大小为60pt
g.setFont(new Font("微软雅黑", Font.BOLD, 30));
//水印内容
//第一行文字
String lineText1 = text.split("\\*")[0];
//第二行文字
String lineText2 = text.split("\\*")[1] + "," + text.split("\\*")[2];
//第三行文字
String lineText3 = String.valueOf(DateUtil.date());
//左下角===》(srcImgWidth - getWatermarkLength(lineText1, g))
// int x = (srcImgWidth - getWatermarkLength(lineText1, g));
//设置水印的坐标左下角
int x = srcImgWidth / 100 * 5;
int y = srcImgHeight / 100 * 85;
int y2 = srcImgHeight / 100 * 90;
int y3 = srcImgHeight / 100 * 95;
//画出水印 第一个参数是水印内容,第二个参数是x轴坐标,第三个参数是y轴坐标
g.drawString(lineText1, x, y);
g.drawString(lineText2, x, y2);
g.drawString(lineText3, x, y3);
g.dispose();
return bufImg;
}
/**
* 获取水印文字的长度
*
* @param waterMarkContent
* @param g
* @return
*/
public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
}
/**
* BufferedImage 编码转换为 base64
*
* @param bufferedImage
* @return
*/
public static String bfferedImageToBase64(BufferedImage bufferedImage) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
try {
ImageIO.write(bufferedImage, "png", baos);//写入流中
} catch (IOException e) {
e.printStackTrace();
}
byte[] bytes = baos.toByteArray();//转换成字节
BASE64Encoder encoder = new BASE64Encoder();
String png_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串
png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
// System.out.println("值为:" + "data:image/jpg;base64," + png_base64);
return png_base64;
}
}