package com.ruoyi.worker.utils;
|
|
|
import com.ruoyi.worker.entity.MultipartFileDto;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.imageio.ImageIO;
|
|
import java.awt.AlphaComposite;
|
|
import java.awt.Color;
|
|
import java.awt.Font;
|
|
import java.awt.Graphics2D;
|
|
import java.awt.RenderingHints;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.*;
|
|
/**
|
|
* @author inspur
|
|
* 定义文字加水印
|
|
*/
|
|
public class FileImageWatermarkUtils {
|
|
/**
|
|
* 水印字体
|
|
*/
|
|
private static final Font FONT = new Font("宋体", Font.PLAIN, 40);
|
|
/**
|
|
* 透明度
|
|
*/
|
|
private static final AlphaComposite COMPOSITE = AlphaComposite
|
|
.getInstance(AlphaComposite.SRC_OVER, 0.9f);
|
|
/**
|
|
* 水印之间的间隔
|
|
*/
|
|
private static final int X_MOVE = 150;
|
|
/**
|
|
* 水印之间的间隔
|
|
*/
|
|
private static final int Y_MOVE = 200;
|
|
/**
|
|
* 打水印(文字)
|
|
*
|
|
* @param file MultipartFile
|
|
* @return MultipartFile
|
|
*/
|
|
public static MultipartFile markWithContent(MultipartFile file,String keyword,String address) {
|
|
FileOutputStream fos = null;
|
|
try {
|
|
BufferedImage srcImg = ImageIO.read(file.getInputStream());
|
|
// 图片宽、高
|
|
int imgWidth = srcImg.getWidth();
|
|
int imgHeight = srcImg.getHeight();
|
|
// 图片缓存
|
|
BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
|
|
// 创建绘图工具
|
|
Graphics2D graphics = bufImg.createGraphics();
|
|
// 画入原始图像
|
|
graphics.drawImage(srcImg, 0, 0, imgWidth, imgHeight, null);
|
|
// 设置水印颜色
|
|
graphics.setColor(Color.RED);
|
|
// 设置水印透明度
|
|
graphics.setComposite(COMPOSITE);
|
|
// 设置倾斜角度
|
|
/* graphics.rotate(Math.toRadians(-35), (double) bufImg.getWidth() / 2,
|
|
(double) bufImg.getHeight() / 2);*/
|
|
// 设置水印字体
|
|
graphics.setFont(FONT);
|
|
// 消除java.awt.Font字体的锯齿
|
|
// graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
int xCoordinate , yCoordinate;
|
int xCoordinate1 , yCoordinate1;
|
int xCoordinate2 , yCoordinate2;
|
|
String mark = "ls65535";
|
|
// 字体长度
|
|
int markWidth = FONT.getSize() * getTextLength(keyword);
|
|
// 字体高度
|
|
int markHeight = FONT.getSize();
|
|
//位置控制 水印位于图片 下方右下角
|
if (address.length()<=23){
|
|
|
xCoordinate = (int) ((imgWidth - getWatermarkLength(keyword, graphics)) * 0.05);
|
|
yCoordinate = (int) (imgHeight * 0.91);
|
|
xCoordinate1 = (int) ((imgWidth - getWatermarkLength(keyword, graphics)) * 0.05);
|
|
yCoordinate1 = (int) (imgHeight * 0.96);
|
|
|
graphics.drawString("地址:" + address, xCoordinate, yCoordinate);
|
graphics.drawString("时间:" + keyword, xCoordinate1, yCoordinate1);
|
|
InputStream inputStream = buffToInputStream(bufImg);
|
|
|
// 释放画图工具
|
|
graphics.dispose();
|
|
return new MultipartFileDto(file.getName(),file.getOriginalFilename(),file.getContentType(),inputStream);
|
|
}else {
|
xCoordinate2 = (int) ((imgWidth - getWatermarkLength(keyword, graphics)) * 0.05);
|
|
yCoordinate2 = (int) (imgHeight * 0.91);
|
|
xCoordinate = (int) ((imgWidth - getWatermarkLength(keyword, graphics)) * 0.05);
|
|
yCoordinate = (int) (imgHeight * 0.88);
|
|
xCoordinate1 = (int) ((imgWidth - getWatermarkLength(keyword, graphics)) * 0.05);
|
|
yCoordinate1 = (int) (imgHeight * 0.96);
|
|
|
graphics.drawString("地址:" + address.substring(0,20), xCoordinate, yCoordinate);
|
graphics.drawString(address.substring(20), xCoordinate2, yCoordinate2);
|
|
graphics.drawString("时间:" + keyword, xCoordinate1, yCoordinate1);
|
|
InputStream inputStream = buffToInputStream(bufImg);
|
|
|
// 释放画图工具
|
|
graphics.dispose();
|
|
return new MultipartFileDto(file.getName(),file.getOriginalFilename(),file.getContentType(),inputStream);
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
if (fos != null) {
|
|
try {
|
|
fos.flush();
|
|
fos.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
/**
|
|
* 计算水印文本长度
|
|
* 1、中文长度即文本长度 2、英文长度为文本长度二分之一
|
|
* @param text
|
|
* @return
|
|
*/
|
|
public static int getTextLength(String text) {
|
|
//水印文字长度
|
|
int length = text.length();
|
|
for (int i = 0; i < text.length(); i++) {
|
|
String s = String.valueOf(text.charAt(i));
|
|
if (s.getBytes().length > 1) {
|
|
length++;
|
|
}
|
|
}
|
|
length = length % 2 == 0 ? length / 2 : length / 2 + 1;
|
|
return length;
|
|
}
|
|
public static InputStream buffToInputStream(BufferedImage buffer) throws IOException {
|
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
ImageIO.write(buffer, "png", os);
|
|
return new ByteArrayInputStream(os.toByteArray());
|
|
}
|
|
public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
|
|
return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
|
|
}
|
|
}
|