Преглед на файлове

coding:添加签名图片

zhangqin преди 4 дни
родител
ревизия
cb675f0cb1

+ 21 - 0
fs-common/src/main/java/com/fs/common/utils/AbsolutePosition.java

@@ -0,0 +1,21 @@
+package com.fs.common.utils;
+
+import net.coobird.thumbnailator.geometry.Position;
+
+import java.awt.*;
+
+public class AbsolutePosition implements Position {
+    private final int x;
+    private final int y;
+
+    public AbsolutePosition(int x, int y) {
+        this.x = x;
+        this.y = y;
+    }
+
+    @Override
+    public Point calculate(int enclosingWidth, int enclosingHeight, int width, int height, int insetLeft, int insetRight, int insetTop, int insetBottom) {
+        // 直接返回指定的坐标点,作为签名图片左上角的位置
+        return new Point(this.x, this.y);
+    }
+}

+ 62 - 3
fs-common/src/main/java/com/fs/common/utils/ThumbnailatorWatermark.java

@@ -4,6 +4,7 @@ import net.coobird.thumbnailator.Thumbnails;
 import net.coobird.thumbnailator.geometry.Positions;
 
 import javax.imageio.ImageIO;
+import java.awt.*;
 import java.awt.image.BufferedImage;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
@@ -15,14 +16,14 @@ import java.net.URL;
 public class ThumbnailatorWatermark {
 
     /**
-     * 对网络图片添加水印并返回字节数组
+     * 对处方图片添加水印并返回字节数组
      *
-     * @param imageUrl           网络图片的URL
+     * @param imageUrl           处方图片的URL
      * @param watermarkImageUrl 本地水印图片路径
      * @return 添加水印后的图片字节数组
      * @throws IOException
      */
-    public static byte[] addWatermarkToNetworkImage(String imageUrl, String watermarkImageUrl) throws IOException {
+    public static byte[] addWatermarkToNetworkImage(String imageUrl, String watermarkImageUrl,String doctorName) throws IOException {
         try {
             // 读取原图片
             URL url = new URL(imageUrl);
@@ -33,11 +34,19 @@ public class ThumbnailatorWatermark {
             InputStream watermarkInputStream = watermarkURL.openStream();
             BufferedImage watermarkImage = ImageIO.read(watermarkInputStream);
 
+            // 创建签名图片
+            BufferedImage signatureImage = createTextImageWithBorder(doctorName, Color.RED, 36,2);
+
             // 使用Thumbnailator处理并输出到字节数组
             ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
             Thumbnails.of(inputStream)
                     .scale(1)
                     .watermark(Positions.TOP_RIGHT, watermarkImage, 0.5f)
+                    .watermark(
+                            new AbsolutePosition(860, 1340), // 使用自定义坐标位置
+                            signatureImage,
+                            0.7f // 透明度
+                    )
                     .outputQuality(0.9)
                     .outputFormat("jpg")
                     .toOutputStream(outputStream);
@@ -51,4 +60,54 @@ public class ThumbnailatorWatermark {
         }
     }
 
+    private static BufferedImage createTextImageWithBorder(String text, Color color, int fontSize, int borderWidth) {
+        // 创建临时Graphics以计算文字尺寸
+        BufferedImage tempImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
+        Graphics2D g = tempImage.createGraphics();
+        Font font = new Font("微软雅黑", Font.PLAIN, fontSize);
+        g.setFont(font);
+        FontMetrics metrics = g.getFontMetrics();
+
+        // 计算文字宽度和高度
+        int textWidth = metrics.stringWidth(text);
+        int textHeight = metrics.getHeight();
+        g.dispose();
+
+        // 添加边框和内边距
+        int padding = 5; // 文字与边框之间的内边距
+        int totalWidth = textWidth + 2 * (borderWidth + padding);
+        int totalHeight = textHeight + 2 * (borderWidth + padding);
+
+        // 创建正式的文字图片 - 使用透明背景
+        BufferedImage textImage = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_ARGB);
+        Graphics2D g2d = textImage.createGraphics();
+
+        // 设置抗锯齿
+        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+
+        // 绘制圆角矩形边框
+        g2d.setColor(color);
+        g2d.setStroke(new BasicStroke(borderWidth)); // 设置边框粗细
+        g2d.drawRoundRect(
+                borderWidth/2,
+                borderWidth/2,
+                totalWidth - borderWidth,
+                totalHeight - borderWidth,
+                10, 10 // 圆角半径
+        );
+
+        // 设置字体和颜色
+        g2d.setFont(font);
+        g2d.setColor(color);
+
+        // 绘制文字 - 位置居中
+        int textX = (totalWidth - textWidth) / 2;
+        int textY = (totalHeight - textHeight) / 2 + metrics.getAscent();
+        g2d.drawString(text, textX, textY);
+
+        g2d.dispose();
+        return textImage;
+    }
+
 }

+ 1 - 1
fs-service/src/main/java/com/fs/his/service/impl/FsPrescribeServiceImpl.java

@@ -819,7 +819,7 @@ public class FsPrescribeServiceImpl implements IFsPrescribeService
         try {
             // 处方添加水印
             CloudStorageService storage = OSSFactory.build();
-            String url = storage.uploadSuffix(ThumbnailatorWatermark.addWatermarkToNetworkImage(prescribe.getDstFilePath(),configService.selectConfigByKey("his.prescription.url") ), ".jpg");
+            String url = storage.uploadSuffix(ThumbnailatorWatermark.addWatermarkToNetworkImage(prescribe.getDstFilePath(),configService.selectConfigByKey("his.prescription.url"),doctor.getDoctorName()), ".jpg");
             prescribe.setDstFilePath(url);
         } catch (IOException e) {
         }