|
|
|
|
|
package com.nmgs.util;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 处理水印类
|
|
|
|
|
|
*/
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
|
|
|
@Async
|
|
|
|
|
|
public class ImageUtils {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 添加水印文字
|
|
|
|
|
|
* @param imageBase64
|
|
|
|
|
|
* @param text 水印文字
|
|
|
|
|
|
* @return
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String addImageWaterMark(String imageBase64, String text, String fileName) throws Exception {
|
|
|
|
|
|
String retStr="";
|
|
|
|
|
|
Properties props = System.getProperties();
|
|
|
|
|
|
String os = props.getProperty("os.name").toLowerCase();
|
|
|
|
|
|
String fileAddress = PathUtil.uploadPath;
|
|
|
|
|
|
ByteArrayInputStream inputStream = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (os.startsWith("win")) {
|
|
|
|
|
|
// fileAddress = PropertiesUtil.getValue("fileAddressWin");
|
|
|
|
|
|
fileAddress = fileAddress.replace("/", "\\");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// fileAddress = PropertiesUtil.getValue("fileAddressLinux");
|
|
|
|
|
|
}
|
|
|
|
|
|
byte[] imageBytes = Base64.getDecoder().decode(imageBase64);
|
|
|
|
|
|
// 构造 BufferedImage 对象
|
|
|
|
|
|
inputStream = new ByteArrayInputStream(imageBytes);
|
|
|
|
|
|
BufferedImage bufferedImage1 = ImageIO.read(inputStream);
|
|
|
|
|
|
LogUtil.WriteLog_Info("ImageUtils====附件地址urlfileAddress=====" + fileAddress, "FileController");
|
|
|
|
|
|
if (PubTools.isEmpty(bufferedImage1)) {
|
|
|
|
|
|
File fileDir = new File(fileAddress); // 指定上传位置
|
|
|
|
|
|
if (!fileDir.exists()) {
|
|
|
|
|
|
fileDir.mkdirs();
|
|
|
|
|
|
}
|
|
|
|
|
|
// 创建文件
|
|
|
|
|
|
File file = new File(fileAddress, fileName); // 指定上传位置
|
|
|
|
|
|
//先删除附件
|
|
|
|
|
|
if (file.exists()) {
|
|
|
|
|
|
file.delete();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 保存文件
|
|
|
|
|
|
org.apache.commons.io.FileUtils.writeByteArrayToFile(file, imageBytes);
|
|
|
|
|
|
|
|
|
|
|
|
//保存到附件服务器上面
|
|
|
|
|
|
String fileUploadUrl = PropertiesUtil.getValue("fileUploadUrl");
|
|
|
|
|
|
LogUtil.WriteLog_Info("ImageUtils====附件上传接口fileUploadUrl=====" + fileUploadUrl, "FileController");
|
|
|
|
|
|
Map<String, String> map = new HashMap<String, String>();
|
|
|
|
|
|
JSONArray jsonA = new JSONArray();
|
|
|
|
|
|
JSONObject mapParam = new JSONObject();
|
|
|
|
|
|
mapParam.put("fileName", fileName);
|
|
|
|
|
|
mapParam.put("imageBase64", imageBase64);
|
|
|
|
|
|
jsonA.add(mapParam);
|
|
|
|
|
|
map.put("jsonArray", jsonA.toJSONString());
|
|
|
|
|
|
String s = HttpClientUtil.doPost(fileUploadUrl, map);
|
|
|
|
|
|
LogUtil.WriteLog_Info("ImageUtils====附件上传结果显示=====" + s, "FileController");
|
|
|
|
|
|
return imageBase64;
|
|
|
|
|
|
}
|
|
|
|
|
|
int w1 = bufferedImage1.getWidth();
|
|
|
|
|
|
int h1 = bufferedImage1.getHeight();
|
|
|
|
|
|
// 生成新图片
|
|
|
|
|
|
BufferedImage destImage = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_RGB);
|
|
|
|
|
|
Graphics2D graphics2D = destImage.createGraphics();
|
|
|
|
|
|
destImage = graphics2D.getDeviceConfiguration().createCompatibleImage(w1, h1, Transparency.TRANSLUCENT);
|
|
|
|
|
|
graphics2D = destImage.createGraphics();
|
|
|
|
|
|
Color color1Init = graphics2D.getColor();
|
|
|
|
|
|
//添加白底
|
|
|
|
|
|
graphics2D.setColor(Color.white);
|
|
|
|
|
|
graphics2D.fillRect(0, 0, w1, h1);
|
|
|
|
|
|
|
|
|
|
|
|
//写入字体
|
|
|
|
|
|
Font font = new Font("宋体", Font.PLAIN,30);
|
|
|
|
|
|
int markWidth = font.getSize() * text.length();// 字体长度
|
|
|
|
|
|
if(markWidth >w1){
|
|
|
|
|
|
markWidth=markWidth/2;
|
|
|
|
|
|
}
|
|
|
|
|
|
int markHeight = font.getSize();// 字体高度
|
|
|
|
|
|
// Color color = new Color(213, 216, 217,128);//写入的颜色
|
|
|
|
|
|
graphics2D.setColor(Color.gray);
|
|
|
|
|
|
graphics2D.setFont(font);
|
|
|
|
|
|
//设置透明度
|
|
|
|
|
|
// graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
|
|
|
|
|
|
//设置选择度
|
|
|
|
|
|
//graphics2D.rotate(10,50,50);
|
|
|
|
|
|
graphics2D.drawString(text, w1/8, h1/2);
|
|
|
|
|
|
//合成图片
|
|
|
|
|
|
//graphics2D.drawImage(backImage, w1/2-50, h1-50-16,null);
|
|
|
|
|
|
graphics2D.dispose();
|
|
|
|
|
|
float alpha=0.7f;//透明度,0-1 ,1完全透明
|
|
|
|
|
|
Color color = new Color(184, 186, 187);//写入的颜色
|
|
|
|
|
|
Color textColorA = new Color(((float) color.getRed())/255, ((float)color.getGreen())/255, ((float)color.getBlue())/255, alpha);
|
|
|
|
|
|
int outRGB = textColorA.getRGB();
|
|
|
|
|
|
//将水印添加到图片中去
|
|
|
|
|
|
for (int i = 0; i < w1; i++) {
|
|
|
|
|
|
for (int j = 0; j < h1; j++) {
|
|
|
|
|
|
int rgb = destImage .getRGB(i, j);
|
|
|
|
|
|
//如果是文字水印的像素点的话
|
|
|
|
|
|
if (new Color(rgb).getRGB() != Color.white.getRGB()) {
|
|
|
|
|
|
bufferedImage1 .setRGB(i, j, rgb);
|
|
|
|
|
|
int bufImgRGB = destImage.getRGB(i, j);
|
|
|
|
|
|
if (bufImgRGB>>24==0) {
|
|
|
|
|
|
//如果该像素点为全透明,则使用半透明的像素点
|
|
|
|
|
|
bufferedImage1 .setRGB(i, j, outRGB);
|
|
|
|
|
|
}else {
|
|
|
|
|
|
//将原图像素点合成到半透明文字上去
|
|
|
|
|
|
Color backColor = new Color(bufImgRGB);
|
|
|
|
|
|
float red = textColorA.getRed()*alpha+backColor.getRed()*(1-alpha);
|
|
|
|
|
|
float green = textColorA.getGreen()*alpha+backColor.getGreen()*(1-alpha);
|
|
|
|
|
|
float blue = textColorA.getBlue()*alpha+backColor.getBlue()*(1-alpha);
|
|
|
|
|
|
|
|
|
|
|
|
Color colorCompose = new Color(red/255, green/255, blue/255, 1f);
|
|
|
|
|
|
bufferedImage1 .setRGB(i, j, colorCompose.getRGB());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 将 BufferedImage 转换为 byte 数组
|
|
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
|
|
ImageIO.write(bufferedImage1, "jpg", baos);
|
|
|
|
|
|
byte[] watermarkedImageBytes = baos.toByteArray();
|
|
|
|
|
|
retStr = Base64.getEncoder().encodeToString(watermarkedImageBytes);
|
|
|
|
|
|
// 创建文件,若有则删除在新增
|
|
|
|
|
|
File file = new File(fileAddress, fileName); // 指定上传位置
|
|
|
|
|
|
if (file.exists()) {
|
|
|
|
|
|
file.delete();
|
|
|
|
|
|
}
|
|
|
|
|
|
// 保存文件
|
|
|
|
|
|
org.apache.commons.io.FileUtils.writeByteArrayToFile(file, watermarkedImageBytes);
|
|
|
|
|
|
|
|
|
|
|
|
//保存到附件服务器上面
|
|
|
|
|
|
String fileUploadUrl = PropertiesUtil.getValue("fileUploadUrl");
|
|
|
|
|
|
LogUtil.WriteLog_Info("ImageUtils====附件上传接口fileUploadUrl=====" + fileUploadUrl, "FileController");
|
|
|
|
|
|
Map<String, String> map = new HashMap<String, String>();
|
|
|
|
|
|
JSONArray jsonA = new JSONArray();
|
|
|
|
|
|
JSONObject mapParam = new JSONObject();
|
|
|
|
|
|
mapParam.put("fileName", fileName);
|
|
|
|
|
|
mapParam.put("imageBase64", imageBase64);
|
|
|
|
|
|
jsonA.add(mapParam);
|
|
|
|
|
|
map.put("jsonArray", jsonA.toJSONString());
|
|
|
|
|
|
String s = HttpClientUtil.doPost(fileUploadUrl, map);
|
|
|
|
|
|
LogUtil.WriteLog_Info("ImageUtils====附件上传结果显示END=====" + s, "FileController");
|
|
|
|
|
|
|
|
|
|
|
|
return retStr;
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
return retStr="";
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
inputStream.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取字符长度,一个汉字作为 1 个字符, 一个英文字母作为 0.5 个字符
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param text
|
|
|
|
|
|
* @return 字符长度,如:text="中文",返回 2;text="test",返回 2;text="中文ABC",返回 4.
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static int getLength(String text) {
|
|
|
|
|
|
int textLength = text.length();
|
|
|
|
|
|
int length = textLength;
|
|
|
|
|
|
for (int i = 0; i < textLength; i++) {
|
|
|
|
|
|
if (String.valueOf(text.charAt(i)).getBytes().length > 1) {
|
|
|
|
|
|
length++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return (length % 2 == 0) ? length / 2 : length / 2 + 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static int delFile(String fileName, boolean isXunhuan) {
|
|
|
|
|
|
Properties props = System.getProperties();
|
|
|
|
|
|
String os = props.getProperty("os.name").toLowerCase();
|
|
|
|
|
|
String fileAddressLocal = "";
|
|
|
|
|
|
fileAddressLocal = PathUtil.uploadPath;
|
|
|
|
|
|
if (os.startsWith("win")) {
|
|
|
|
|
|
//fileAddressLocal = PropertiesUtil.getValue("fileAddressWin");
|
|
|
|
|
|
fileAddressLocal = fileAddressLocal.replace("/", "\\");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// fileAddressLocal = PropertiesUtil.getValue("fileAddressLinux");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LogUtil.WriteLog_Info("ImageUtils本地文件地址=====fileAddressLocal====" + fileAddressLocal, "FileController");
|
|
|
|
|
|
|
|
|
|
|
|
File file = new File(fileAddressLocal, fileName); // 指定上传位置
|
|
|
|
|
|
if (file.exists()) {
|
|
|
|
|
|
file.delete();
|
|
|
|
|
|
}
|
|
|
|
|
|
if (isXunhuan) {
|
|
|
|
|
|
String fileDelAddress = PropertiesUtil.getValue("fileDelUrl");
|
|
|
|
|
|
LogUtil.WriteLog_Info("ImageUtil远程删除文件地址=====fileAddressLocal====" + fileDelAddress, "FileController");
|
|
|
|
|
|
Map<String, String> map = new HashMap<String, String>();
|
|
|
|
|
|
map.put("fileName", fileName);
|
|
|
|
|
|
map.put("canDel", "N");
|
|
|
|
|
|
String s = HttpClientUtil.doPost(fileDelAddress, map);
|
|
|
|
|
|
LogUtil.WriteLog_Info("ImageUtil远程删除文件地址=====执行结果====" + s, "FileController");
|
|
|
|
|
|
}
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|