package com.dji.sample.territory.utils;
|
|
import javax.imageio.ImageIO;
|
import java.awt.*;
|
import java.awt.geom.AffineTransform;
|
import java.awt.image.BufferedImage;
|
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayOutputStream;
|
import java.io.File;
|
import java.io.IOException;
|
import java.text.SimpleDateFormat;
|
import java.time.LocalDateTime;
|
import java.time.format.DateTimeFormatter;
|
import java.util.Date;
|
|
import static com.dji.sample.territory.utils.ImgZipUtil.compressImageAndGetFile;
|
|
public class WaterMark {
|
/**
|
* 将图片加上水印并压缩
|
*
|
* @param file
|
* @param path
|
* @param pssj
|
* @param lat
|
* @param lng
|
* @return 添加水印并压缩后的图片文件。
|
* @throws IOException 如果读取或保存图片失败。
|
*/
|
public static File addWatermark(File file, String path, Long pssj, Double lat, Double lng) throws IOException {
|
Long timestamp = pssj; // 例如:Unix 时间戳(以秒为单位)
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timestamp))));
|
File originalImageFile = new File(file.toURI());
|
BufferedImage originalImage;
|
try {
|
originalImage = ImageIO.read(originalImageFile);
|
} catch (IOException e) {
|
throw new IllegalArgumentException("读取图片失败"+e.getMessage());
|
}
|
|
// 创建 Graphics2D 对象以在图像上绘制水印
|
Graphics2D g2d = originalImage.createGraphics();
|
|
// 设置水印文字 "国土调查云"
|
String watermarkText = "国土调查云";
|
|
// 设置水印文字样式
|
g2d.setColor(Color.WHITE); // 设置为白色
|
float alpha = 0.5f; // 设置透明度为 0.5
|
AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
|
g2d.setComposite(alphaComposite);
|
|
g2d.setFont(new Font("宋体", Font.BOLD, 200)); // 使用宋体字体
|
|
// 设置水印位置(左上角)
|
int x1 = 400; // 左上角水印位置 x 坐标
|
int y1 = 1200; // 左上角水印位置 y 坐标
|
|
// 旋转35度角
|
double angle = Math.toRadians(35);
|
AffineTransform at = AffineTransform.getRotateInstance(angle, x1, y1);
|
g2d.setTransform(at);
|
|
// 绘制左上角旋转水印文字
|
g2d.drawString(watermarkText, x1, y1);
|
// 设置第一个水印完毕,恢复透明度为不透明状态(透明度为 1.0)
|
alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
|
g2d.setComposite(alphaComposite);
|
String extraInfo = String.format("时间:%s\nlon:%.9f lat:%.6f", sd, lng, lat);
|
g2d.setFont(new Font("宋体", Font.BOLD, 50)); // 使用宋体字体,更小的字号
|
g2d.setBackground(Color.black);
|
// 计算文本宽度和高度
|
FontMetrics fm = g2d.getFontMetrics();
|
int textWidth = fm.stringWidth(extraInfo);
|
int textHeight = fm.getHeight();
|
// 恢复坐标系到正常状态(不旋转)
|
g2d.setTransform(new AffineTransform());
|
// 设置文本位置(右下角)
|
int x2 = originalImage.getWidth() - textWidth - 30; // 右对齐的 x 坐标
|
int y2 = originalImage.getHeight() - textHeight - 65; // 最底部位置 y 坐标
|
// 绘制文本信息
|
String[] lines = extraInfo.split("\n");
|
for (String line : lines) {
|
// 计算每行文本的宽度
|
int lineWidth = fm.stringWidth(line);
|
// 将文本的起始位置设置为图像宽度减去文本的宽度
|
g2d.drawString(line, x2 + textWidth - lineWidth, y2);
|
y2 += g2d.getFontMetrics().getHeight(); // 增加行高
|
}
|
// 释放 Graphics2D 对象
|
g2d.dispose();
|
// 保存添加水印后的图片
|
File outputFile = new File(path + "mark.jpg");
|
File originalImageFiles = new File(outputFile.toURI());
|
float compressionQuality = 0.5f; // 0.0 - 1.0, higher value means better quality
|
File compressedImageFile = compressImageAndGetFile(originalImageFiles, compressionQuality);
|
try {
|
ImageIO.write(originalImage, "jpg", outputFile);
|
} catch (IOException e) {
|
throw new IllegalArgumentException("水印图片保存失败");
|
}
|
return compressedImageFile;
|
}
|
|
}
|