From 28f661933ffdeb1f65ecee52f8f4b7a3b373da9b Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Mon, 03 Jun 2024 18:29:52 +0800
Subject: [PATCH] 完善图斑功能,完善水印,增加图片的角度信息

---
 src/main/java/com/dji/sample/territory/utils/WaterMarkUtil.java |   59 ++++++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 38 insertions(+), 21 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 9518c4c..ffed172 100644
--- a/src/main/java/com/dji/sample/territory/utils/WaterMarkUtil.java
+++ b/src/main/java/com/dji/sample/territory/utils/WaterMarkUtil.java
@@ -12,6 +12,7 @@
 import java.util.Date;
 
 import static com.dji.sample.territory.utils.ImgZipUtil.compressImageAndGetFile;
+
 @Component
 public class WaterMarkUtil {
     /**
@@ -24,9 +25,9 @@
      * @return 添加水印并压缩后的图片文件。
      * @throws IOException 如果读取或保存图片失败。
      */
-    public static File addWatermark(File file,  Long pssj, Double lat, Double lng, Double angles) throws IOException, FontFormatException {
-
-        String angel=angle(angles);
+    public static File addWatermark(File file, Long pssj, Double lat, Double lng, Double angles) throws IOException, FontFormatException {
+        double anglses = convertAngle(angles);
+        String angel = angle(anglses);
         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))));
@@ -52,7 +53,7 @@
 
         // 设置水印位置(左居中)
         int x1 = 600; // 左边距离图片左边缘的距离
-        int y1 = (originalImage.getHeight() - textHeights) -800; // 垂直居中
+        int y1 = (originalImage.getHeight() - textHeights) - 800; // 垂直居中
         // 旋转35度角
         double angle = Math.toRadians(50);
         AffineTransform at = AffineTransform.getRotateInstance(angle, x1, y1);
@@ -61,9 +62,9 @@
         g2d.drawString(watermarkText, x1, y1);
         alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f);
         g2d.setComposite(alphaComposite);
-        String extraInfo = String.format("Lon:%.7f Lat:%.7f\n%s %s", lng, lat,angel,sd);
+        String extraInfo = String.format("Lon:%.7f Lat:%.7f\n%s %s", lng, lat, angel, sd);
 
-        File fontFile = new File("/tmp/jave/MiSans-Normal.ttf"); // 替换为你的字体文件路径
+        File fontFile = new File("src/main/java/com/dji/sample/territory/utils/MiSans-Normal.ttf"); // 替换为你的字体文件路径
         Font customFont = Font.createFont(Font.TRUETYPE_FONT, fontFile).deriveFont(36.00F);
         // 注册字体
         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
@@ -81,18 +82,18 @@
         int y2 = originalImage.getHeight() - textHeight - 30; // 最底部位置 y 坐标
 
 
-        int rectX = originalImage.getWidth() - textWidth  +490;
-        int rectY = originalImage.getHeight() - textHeight -15;
+        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);
+        g2d.fillRect(rectX, rectY, textWidth, textHeight - 6);
 
-        int rectXs = originalImage.getWidth() - textWidth +420 ;
-        int rectYs = originalImage.getHeight() - textHeight  -62;
+        int rectXs = originalImage.getWidth() - textWidth + 420;
+        int rectYs = originalImage.getHeight() - textHeight - 62;
         // 绘制半透明黑色背景矩形
         g2d.setColor(semiTransparentColor);
-        g2d.fillRect(rectXs, rectYs, textWidth, textHeight-10);
+        g2d.fillRect(rectXs, rectYs, textWidth, textHeight - 10);
 
 //            int rectXw = originalImage.getWidth() - textWidth  +1510 ;
 //            int rectYw = originalImage.getHeight() - textHeight -124;
@@ -128,39 +129,55 @@
         if (angle > 0 && angle < 45) {
             return "北偏东";
         }
-        if (angle > 45 && angle < 90) {
+        if (angle >= 45 && angle < 90) {
             return "东偏北";
         }
         if (angle > 90 && angle < 135) {
             return "东偏南";
         }
-        if (angle > 135 && angle < 180) {
+        if (angle >= 135 && angle < 180) {
             return "南偏东";
         }
-        if (angle > -45 && angle < 0) {
+        if (angle >= 315 && angle < 360) {
             return "北偏西";
         }
-        if (angle > -90 && angle < -45) {
+        if (angle > 270 && angle < 315) {
             return "西偏北";
         }
-        if (angle > -135 && angle < -90) {
+        if (angle >= 225 && angle < 270) {
             return "西偏南";
         }
-        if (angle > -180 && angle < -135) {
+        if (angle > 180 && angle < 225) {
             return "南偏西";
         }
-        if (angle == 0) {
+        if (angle == 0 || angle == 360) {
             return "正北";
         }
         if (angle == 90) {
             return "正东";
         }
-        if (angle == 180|| angle == -180) {
+        if (angle == 180) {
             return "正南";
         }
-        if (angle == -90) {
+        if (angle == 270) {
             return "正西";
         }
         return "未知";
     }
+
+    public static double convertAngle(double angle) {
+        // 处理负数角度,将其转换为正数
+        if (angle < 0) {
+            angle += 360;
+        }
+        return angle;
+    }
+
+    public static void main(String[] args) {
+        // 测试不同的输入角度
+        double[] testAngles = {25};
+        for (double angle : testAngles) {
+            System.out.printf("输入角度: %.1f, 转换后的方位角: %.1f\n", angle, convertAngle(angle));
+        }
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3