From 7166f4e2f86ed2532a38d5c210284dd9ca7be4c7 Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Mon, 03 Jun 2024 13:57:45 +0800
Subject: [PATCH] 图斑编号模糊搜索功能
---
src/main/java/com/dji/sample/territory/utils/WaterMarkUtil.java | 67 ++++++++++++++++++++++++---------
1 files changed, 48 insertions(+), 19 deletions(-)
diff --git a/src/main/java/com/dji/sample/territory/utils/WaterMarkUtil.java b/src/main/java/com/dji/sample/territory/utils/WaterMarkUtil.java
index c91c4f8..9518c4c 100644
--- a/src/main/java/com/dji/sample/territory/utils/WaterMarkUtil.java
+++ b/src/main/java/com/dji/sample/territory/utils/WaterMarkUtil.java
@@ -1,5 +1,7 @@
package com.dji.sample.territory.utils;
+import org.springframework.stereotype.Component;
+
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
@@ -10,21 +12,20 @@
import java.util.Date;
import static com.dji.sample.territory.utils.ImgZipUtil.compressImageAndGetFile;
-
+@Component
public class WaterMarkUtil {
/**
* 将图片加上水印并压缩
*
* @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, Double angles) throws IOException {
- String author="机巢";
+ public static File addWatermark(File file, Long pssj, Double lat, Double lng, Double angles) throws IOException, FontFormatException {
+
String angel=angle(angles);
Long timestamp = pssj; // 例如:Unix 时间戳(以秒为单位)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@@ -40,29 +41,35 @@
// 创建 Graphics2D 对象以在图像上绘制水印
Graphics2D g2d = originalImage.createGraphics();
- // 设置水印文字 "国土调查云"
+ // 设置水印文字 "江西调查云"
String watermarkText = "江西调查云";
- float alpha = 0.5f; // 设置透明度为 0.5
+ float alpha = 0.8f; // 设置透明度为 0.7
AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
g2d.setComposite(alphaComposite);
- g2d.setFont(new Font("宋体", Font.BOLD, 100)); // 使用宋体字体
+ g2d.setFont(new Font("宋体", Font.BOLD, 150)); // 使用宋体字体
FontMetrics fms = g2d.getFontMetrics();
int textHeights = fms.getHeight();
// 设置水印位置(左居中)
- int x1 = 100; // 左边距离图片左边缘的距离
- int y1 = (originalImage.getHeight() - textHeights) / 2; // 垂直居中
+ int x1 = 600; // 左边距离图片左边缘的距离
+ int y1 = (originalImage.getHeight() - textHeights) -800; // 垂直居中
// 旋转35度角
- double angle = Math.toRadians(35);
+ double angle = Math.toRadians(50);
AffineTransform at = AffineTransform.getRotateInstance(angle, x1, y1);
g2d.setTransform(at);
// 绘制左上角旋转水印文字
g2d.drawString(watermarkText, x1, y1);
- alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
+ alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f);
g2d.setComposite(alphaComposite);
- String extraInfo = String.format("%s\nlon:%.9f lat:%.6f\n%s 时间:%s", author,lng, lat,angel,sd);
- g2d.setFont(new Font("宋体", Font.BOLD, 50)); // 使用宋体字体,更小的字号
- g2d.setBackground(Color.black);
+ String extraInfo = String.format("Lon:%.7f Lat:%.7f\n%s %s", lng, lat,angel,sd);
+
+ File fontFile = new File("/tmp/jave/MiSans-Normal.ttf"); // 替换为你的字体文件路径
+ Font customFont = Font.createFont(Font.TRUETYPE_FONT, fontFile).deriveFont(36.00F);
+ // 注册字体
+ GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
+ ge.registerFont(customFont);
+ g2d.setFont(customFont);
+
// 计算文本宽度和高度
FontMetrics fm = g2d.getFontMetrics();
int textWidth = fm.stringWidth(extraInfo);
@@ -71,10 +78,32 @@
g2d.setTransform(new AffineTransform());
// 设置文本位置(右下角)
int x2 = originalImage.getWidth() - textWidth - 30; // 右对齐的 x 坐标
- int y2 = originalImage.getHeight() - textHeight - 65; // 最底部位置 y 坐标
+ int y2 = originalImage.getHeight() - textHeight - 30; // 最底部位置 y 坐标
+
+
+ int rectX = originalImage.getWidth() - textWidth +490;
+ int rectY = originalImage.getHeight() - textHeight -15;
+ // 绘制半透明黑色背景矩形
+ Color semiTransparentColor = new Color(0, 0, 0, 156); // 0-255 的 alpha 值,0 表示完全透明,255 表示完全不透明
+ g2d.setColor(semiTransparentColor);
+ g2d.fillRect(rectX, rectY, textWidth, textHeight-6);
+
+ int rectXs = originalImage.getWidth() - textWidth +420 ;
+ int rectYs = originalImage.getHeight() - textHeight -62;
+ // 绘制半透明黑色背景矩形
+ g2d.setColor(semiTransparentColor);
+ g2d.fillRect(rectXs, rectYs, textWidth, textHeight-10);
+
+// int rectXw = originalImage.getWidth() - textWidth +1510 ;
+// int rectYw = originalImage.getHeight() - textHeight -124;
+//
+// // 绘制半透明黑色背景矩形
+// g2d.setColor(semiTransparentColor);
+// g2d.fillRect(rectXw, rectYw, textWidth, textHeight-6);
// 绘制文本信息
String[] lines = extraInfo.split("\n");
for (String line : lines) {
+ g2d.setColor(Color.WHITE);
// 计算每行文本的宽度
int lineWidth = fm.stringWidth(line);
// 将文本的起始位置设置为图像宽度减去文本的宽度
@@ -85,14 +114,14 @@
g2d.dispose();
// 保存添加水印后的图片
File outputFile = File.createTempFile("mark", ".jpg");
- File originalImageFiles = new File(outputFile.toURI());
+
try {
- ImageIO.write(originalImage, "jpg", outputFile);
+ ImageIO.write(originalImage, "jpg", outputFile); // 将图像写入临时文件
} catch (IOException e) {
throw new IllegalArgumentException("水印图片保存失败");
}
- float compressionQuality = 0.5f; // 0.0 - 1.0, higher value means better quality
- return compressImageAndGetFile(originalImageFiles, compressionQuality);
+
+ return outputFile; // 返回保存的文件对象
}
public static String angle(double angle) {
--
Gitblit v1.9.3