rain
2024-05-06 bc16d0f0fc46ca6cb7d9bfb8496ad8f20ffba677
src/main/java/com/dji/sample/territory/utils/WaterMarkUtil.java
@@ -23,7 +23,9 @@
     * @return 添加水印并压缩后的图片文件。
     * @throws IOException 如果读取或保存图片失败。
     */
    public static File addWatermark(File file, String path, Long pssj, Double lat, Double lng) throws IOException {
    public static File addWatermark(File file, String path, Long pssj, Double lat, Double lng, Double angles) throws IOException {
        String author="机巢";
        String angel=angle(angles);
        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))));
@@ -39,31 +41,26 @@
        Graphics2D g2d = originalImage.createGraphics();
        // 设置水印文字 "国土调查云"
        String watermarkText = "国土调查云";
        // 设置水印文字样式
        g2d.setColor(Color.WHITE); // 设置为白色
        String watermarkText = "江西调查云";
        float alpha = 0.5f; // 设置透明度为 0.5
        AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
        g2d.setComposite(alphaComposite);
        g2d.setFont(new Font("宋体", Font.BOLD, 100)); // 使用宋体字体
        FontMetrics fms = g2d.getFontMetrics();
        int textHeights = fms.getHeight();
        g2d.setFont(new Font("宋体", Font.BOLD, 200)); // 使用宋体字体
        // 设置水印位置(左上角)
        int x1 = 400; // 左上角水印位置 x 坐标
        int y1 = 1200; // 左上角水印位置 y 坐标
        // 设置水印位置(左居中)
        int x1 = 100; // 左边距离图片左边缘的距离
        int y1 = (originalImage.getHeight() - textHeights) / 2; // 垂直居中
        // 旋转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);
        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);
        // 计算文本宽度和高度
@@ -95,8 +92,46 @@
            throw new IllegalArgumentException("水印图片保存失败");
        }
        float compressionQuality = 0.5f; // 0.0 - 1.0, higher value means better quality
        File compressedImageFile = compressImageAndGetFile(originalImageFiles, compressionQuality);
        return compressedImageFile;
        return compressImageAndGetFile(originalImageFiles, compressionQuality);
    }
    public static String angle(double angle) {
        if (angle > 0 && angle < 45) {
            return "北偏东";
        }
        if (angle > 45 && angle < 90) {
            return "东偏北";
        }
        if (angle > 90 && angle < 135) {
            return "东偏南";
        }
        if (angle > 135 && angle < 180) {
            return "南偏东";
        }
        if (angle > -45 && angle < 0) {
            return "北偏西";
        }
        if (angle > -90 && angle < -45) {
            return "西偏北";
        }
        if (angle > -135 && angle < -90) {
            return "西偏南";
        }
        if (angle > -180 && angle < -135) {
            return "南偏西";
        }
        if (angle == 0) {
            return "正北";
        }
        if (angle == 90) {
            return "正东";
        }
        if (angle == 180|| angle == -180) {
            return "正南";
        }
        if (angle == -90) {
            return "正西";
        }
        return "未知";
    }
}