rain
2024-08-21 2db1aa88e8ab53096a936163d686b90d8e056a99
src/main/java/com/dji/sample/territory/utils/WaterMarkUtil.java
@@ -1,5 +1,7 @@
package com.dji.sample.territory.utils;
import com.dji.sample.media.service.IFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.imageio.ImageIO;
@@ -10,12 +12,10 @@
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import static com.dji.sample.territory.utils.ImgZipUtil.compressImageAndGetFile;
@Component
public class WaterMarkUtil {
    @Autowired
    private IFileService fileService;
    /**
     * 将图片加上水印并压缩
     *
@@ -26,7 +26,7 @@
     * @return 添加水印并压缩后的图片文件。
     * @throws IOException 如果读取或保存图片失败。
     */
    public static File addWatermark(File file, Long pssj, Double lat, Double lng, Double angles,String path) throws IOException, FontFormatException {
    public static File addWatermark(File file, Long pssj, Double lat, Double lng, Double angles, int type) throws IOException, FontFormatException {
        double anglses = convertAngle(angles);
        String angel = angle(anglses);
        Long timestamp = pssj; // 例如:Unix 时间戳(以秒为单位)
@@ -39,12 +39,15 @@
        } catch (IOException e) {
            throw new IllegalArgumentException("读取图片失败" + e.getMessage());
        }
//        fileService.saveMarkFile(lotInfo.getWorkspaceId(),mediaFile,file1);
        // 创建 Graphics2D 对象以在图像上绘制水印
        Graphics2D g2d = originalImage.createGraphics();
        // 设置水印文字 "江西调查云"
        String watermarkText = "江西调查云";
        if (type == 1){
            watermarkText = "国土调查云";
        }
        float alpha = 0.8f; // 设置透明度为 0.7
        AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
        g2d.setComposite(alphaComposite);
@@ -64,8 +67,8 @@
        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);
        File fontFile = new File("src/main/resources/MiSans-Normal.ttf"); // 替换为你的字体文件路径
//        File fontFile = new File("/usr/share/fonts/MiSans-Normal.ttf"); // 替换为你的字体文件路径
        File fontFile = new File("/usr/share/fonts/MiSans-Normal.ttf"); // 替换为你的字体文件路径
//        File fontFile = new File("src/main/resources/MiSans-Normal.ttf"); // 替换为你的字体文件路径
        Font customFont = Font.createFont(Font.TRUETYPE_FONT, fontFile).deriveFont(36.00F);
        // 注册字体
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();