|
@@ -1,6 +1,14 @@
|
|
|
package com.steerinfo.dil.util;
|
|
package com.steerinfo.dil.util;
|
|
|
|
|
|
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
+
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
|
|
+import java.awt.*;
|
|
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
|
|
+import java.io.File;
|
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
|
|
+import java.text.DateFormat;
|
|
|
import java.text.DecimalFormat;
|
|
import java.text.DecimalFormat;
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Calendar;
|
|
import java.util.Calendar;
|
|
@@ -16,7 +24,7 @@ import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
public class DataChange {
|
|
public class DataChange {
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+ static String tempDir = "/temp/";
|
|
|
/**
|
|
/**
|
|
|
* 时间转换类
|
|
* 时间转换类
|
|
|
* 处理了三种类型 yyyy-MM-dd HH:mm:ss yyyy/MM/dd HH:mm:ss 时间戳类型(带毫秒数时间戳13位)
|
|
* 处理了三种类型 yyyy-MM-dd HH:mm:ss yyyy/MM/dd HH:mm:ss 时间戳类型(带毫秒数时间戳13位)
|
|
@@ -289,4 +297,57 @@ public class DataChange {
|
|
|
map.put("lastDay",lastDay);
|
|
map.put("lastDay",lastDay);
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public static File paintWater(MultipartFile file, String text,int xOffset ,int yOffset) throws Exception{
|
|
|
|
|
+ //将文件对象转化为图片对象
|
|
|
|
|
+ Image srcImg = ImageIO.read(file.getInputStream());
|
|
|
|
|
+ //获取图片的宽
|
|
|
|
|
+ int srcImgWidth = srcImg.getWidth(null);
|
|
|
|
|
+ //获取图片的高
|
|
|
|
|
+ int srcImgHeight = srcImg.getHeight(null);
|
|
|
|
|
+ // 加水印
|
|
|
|
|
+ BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
|
|
|
|
|
+ //创建画笔
|
|
|
|
|
+ Graphics2D graphics = bufImg.createGraphics();
|
|
|
|
|
+ //srcImg 为上面获取到的原始图片的图片对象
|
|
|
|
|
+ graphics.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
|
|
|
|
|
+ //根据图片的背景设置水印颜色
|
|
|
|
|
+ graphics.setColor(new Color(255,0,0,255));
|
|
|
|
|
+ //设置字体 画笔字体样式为微软雅黑,加粗,文字大小为60pt
|
|
|
|
|
+ graphics.setFont(new Font("微软雅黑", Font.BOLD, 40));
|
|
|
|
|
+ //设置水印的坐标
|
|
|
|
|
+ int x = 0;
|
|
|
|
|
+ if(xOffset >= 0){
|
|
|
|
|
+ x = xOffset;
|
|
|
|
|
+ }else{
|
|
|
|
|
+ x = srcImgWidth + (xOffset % srcImgWidth);
|
|
|
|
|
+ }
|
|
|
|
|
+ int y = 0;
|
|
|
|
|
+ if(yOffset >= 0){
|
|
|
|
|
+ y = yOffset;
|
|
|
|
|
+ }else {
|
|
|
|
|
+ y = srcImgHeight + (yOffset % srcImgWidth);
|
|
|
|
|
+ }
|
|
|
|
|
+ //自定义水印 第一个参数是水印内容,第二个参数是x轴坐标,第三个参数是y轴坐标
|
|
|
|
|
+ graphics.drawString(text, x, y);
|
|
|
|
|
+ //时间水印
|
|
|
|
|
+ DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
+ graphics.drawString(df.format(new Date()), x, y + 50);
|
|
|
|
|
+ graphics.dispose();
|
|
|
|
|
+ //待存储的地址
|
|
|
|
|
+ File fileDir = new File(tempDir);
|
|
|
|
|
+ if(!fileDir.exists() || !fileDir.isDirectory()){
|
|
|
|
|
+ fileDir.mkdir();
|
|
|
|
|
+ }
|
|
|
|
|
+ String tarImgPath = tempDir + file.getOriginalFilename();
|
|
|
|
|
+ // 输出图片
|
|
|
|
|
+ FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
|
|
|
|
|
+ ImageIO.write(bufImg, "png", outImgStream);
|
|
|
|
|
+ outImgStream.flush();
|
|
|
|
|
+ outImgStream.close();
|
|
|
|
|
+ //返回目标图片
|
|
|
|
|
+ File targetFile = new File(tarImgPath);
|
|
|
|
|
+// targetFile.delete();//临时文件返回后,保存在其他地方然后及时删除
|
|
|
|
|
+ return targetFile;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|