|  | @@ -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;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  }
 |