| | |
| | | package com.dji.sample.territory.utils; |
| | | |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import java.awt.*; |
| | | import java.awt.geom.AffineTransform; |
| | |
| | | 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, FontFormatException { |
| | | 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 时间戳(以秒为单位) |
| | |
| | | g2d.drawString(watermarkText, x1, y1); |
| | | alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f); |
| | | g2d.setComposite(alphaComposite); |
| | | String extraInfo = String.format("Lon:%.9f Lat:%.6f\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("src/main/java/com/dji/sample/territory/utils/MiSans-Normal.ttf"); // 替换为你的字体文件路径 |
| | | File fontFile = new File("/tmp/jave/MiSans-Normal.ttf"); // 替换为你的字体文件路径 |
| | | Font customFont = Font.createFont(Font.TRUETYPE_FONT, fontFile).deriveFont(36.00F); |
| | | // 注册字体 |
| | | GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
| | |
| | | int y2 = originalImage.getHeight() - textHeight - 30; // 最底部位置 y 坐标 |
| | | |
| | | |
| | | int rectX = originalImage.getWidth() - textWidth +530; |
| | | 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 +450 ; |
| | | int rectXs = originalImage.getWidth() - textWidth +420 ; |
| | | int rectYs = originalImage.getHeight() - textHeight -62; |
| | | // 绘制半透明黑色背景矩形 |
| | | g2d.setColor(semiTransparentColor); |
| | |
| | | // 释放 Graphics2D 对象 |
| | | g2d.dispose(); |
| | | // 保存添加水印后的图片 |
| | | File outputFile = new File("src/main/copy.jpeg"); |
| | | File outputFile = File.createTempFile("mark", ".jpg"); |
| | | |
| | | try { |
| | | ImageIO.write(originalImage, "jpg", outputFile); // 将图像写入临时文件 |
| | | } catch (IOException e) { |