//注意x,y的设置就好
public class WaterMarkUtils {
/**
* @param file 源图片路径
* @param date 图片生成日期
* @param address 图片生成地址
*/
public static byte[] addWaterMark(InputStream file,String date,String address,String imageName) {
byte[] buff = null;
//字体大小
int size = 30;
Font fonts = null;
try {
// 读取原图片信息
BufferedImage bufImg = ImageIO.read(file);
// 获取图片的宽
int srcImgWidth = bufImg.getWidth(null);
// 获取图片的高
int srcImgHeight = bufImg.getHeight(null);
//计算字体大小
size = (srcImgWidth-1080)/40 +size;
fonts = new Font("微软雅黑", Font.PLAIN, size);
// 加水印
Graphics2D g = bufImg.createGraphics();
// 设置水印的坐标
//设置水印的坐标
int x = 0;
int y = srcImgHeight-150;
// 去除锯齿(当设置的字体过大的时候,会出现锯齿)
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(new Color(0, 0, 0, 40));
//画矩形不带框的
g.fillRect(x, y, 4000, 120);
g.setColor(new Color(255, 255, 255, 153));
// 设置字体
g.setFont(fonts);
if(StringUtils.isNotBlank(date)){
// 画出水印
g.drawString(date, x+30, y+50);
}
if(StringUtils.isNotBlank(address)){
// 画出水印
g.drawString(address, x+30, y+100);
}
// 输出图片
// FileOutputStream outImgStream = new FileOutputStream("D:/"+file.getOriginalFilename());
// ImageIO.write(bufImg, "jpg", outImgStream);
// outImgStream.flush();
// outImgStream.close();
String prefix = imageName.substring(imageName.lastIndexOf(".")+1);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufImg, prefix, os);
buff = os.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return buff;
}
/**
* @param file 源图片路径
* @param date 图片生成日期
* @param address 图片生成地址
*/
public static byte[] addWaterMark(File file, String date, String address, String imageName) {
byte[] buff = null;
//字体大小
int size = 30;
//俩条水印的距离
int line = 45;
//底色的宽度
int width = 1000;
Font fonts = null;
try {
// 读取原图片信息
BufferedImage bufImg = ImageIO.read(file);
// 获取图片的宽
int srcImgWidth = bufImg.getWidth(null);
// 获取图片的高
int srcImgHeight = bufImg.getHeight(null);
//计算字体大小
size = (srcImgWidth-1080)/40 +size;
fonts = new Font("微软雅黑", Font.PLAIN, size);
// 加水印
Graphics2D g = bufImg.createGraphics();
// 设置水印的坐标
//设置水印的坐标
int x = 0;
int y = srcImgHeight-150;
// 去除锯齿(当设置的字体过大的时候,会出现锯齿)
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(new Color(0, 0, 0, 40));
//画矩形不带框的
g.fillRect(x, y, 4000, 120);
g.setColor(new Color(255, 255, 255, 153));
// 设置字体
g.setFont(fonts);
if(StringUtils.isNotBlank(date)){
// 画出水印
g.drawString(date, x+30, y+50);
}
if(StringUtils.isNotBlank(address)){
// 画出水印
g.drawString(address, x+30, y+100);
}
// 输出图片
FileOutputStream outImgStream = new FileOutputStream("/Users/zhangsl/Desktop/b.png");
ImageIO.write(bufImg, "png", outImgStream);
outImgStream.flush();
outImgStream.close();
String prefix = imageName.substring(imageName.lastIndexOf(".")+1);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufImg, prefix, os);
buff = os.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return buff;
}
public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
}
public static void main(String arg[]){
File file = new File("/Users/zhangsl/Desktop/a.png");
WaterMarkUtils.addWaterMark(file, "20180626","zhangs","werer");
}
}