Parcourir la source

途宝报单新需求 09点09分

wangzhuo il y a 1 an
Parent
commit
8e97fbc8c9

+ 79 - 5
ruoyi-common/src/main/java/com/ruoyi/common/utils/AddWatermarkUtil.java

@@ -14,6 +14,8 @@ import java.awt.font.TextLayout;
 import java.awt.geom.AffineTransform;
 import java.awt.image.BufferedImage;
 import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * @ProjectName: test
@@ -25,6 +27,31 @@ import java.io.*;
  * @Version: 1.0
  */
 public class AddWatermarkUtil {
+
+    static String simsunPath = "/usr/local/project/DaoHe/simsun.ttc";
+
+    public static Font loadStyleFont(int style, float fontSize) {
+        try {
+            // File file = new File(fontFileName);
+            File file = new File(new String(simsunPath.getBytes("utf-8"), "utf-8"));
+            if (!file.exists()) {
+                //文件不存在
+                return new java.awt.Font("宋体", Font.PLAIN, 33);
+            } else {
+                //文件存在!
+            }
+            FileInputStream in = new FileInputStream(file);
+            Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, in);
+            Font dynamicFontPt = dynamicFont.deriveFont(style, fontSize);
+            in.close();
+            return dynamicFontPt;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return new java.awt.Font("宋体", Font.PLAIN, 20);
+        }
+    }
+
+
     public static void waterPress(File srcImgFile, File outputFile,
                                   Color markContentColor, int fontSize, String waterMarkContent) {
         try {
@@ -39,7 +66,10 @@ public class AddWatermarkUtil {
             Graphics2D g = bufImg.createGraphics();
             // 设置起点
             g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
-            Font font = new Font("Default", Font.PLAIN, fontSize);
+//            Font font = new Font("宋体", Font.PLAIN, fontSize);
+
+            Font font = loadStyleFont(Font.PLAIN, fontSize);
+
             // 水印透明度
             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5F));
             // 根据图片的背景设置水印颜色
@@ -57,9 +87,11 @@ public class AddWatermarkUtil {
                 }
             }
 
+
+            List<String> waterMarkContentsWrap = new ArrayList<>();
+
             for (int j = 0; j < contentLength; j++) {
                 waterMarkContent = waterMarkContents[j];
-                int tempX = 10;
                 int tempY = fontSize;
                 // 单字符长度
                 int tempCharLen = 0;
@@ -72,7 +104,7 @@ public class AddWatermarkUtil {
                     tempLineLen += tempCharLen;
                     if (tempLineLen >= srcImgWidth) {
                         // 长度已经满一行,进行文字叠加
-                        g.drawString(sb.toString(), tempX, tempY);
+                        waterMarkContentsWrap.add(sb.toString());
                         // 清空内容,重新追加
                         sb.delete(0, sb.length());
                         tempLineLen = 0;
@@ -82,9 +114,49 @@ public class AddWatermarkUtil {
                 }
                 // 通过设置后两个输入参数给水印定位
                 //右下角
-                g.drawString(sb.toString(), srcImgWidth - maxLength, srcImgHeight - (contentLength - j - 1) * tempY - 50);
-                // g.drawString(sb.toString(), 20, srcImgHeight - (contentLength - j - 1) * tempY - 50);
+                waterMarkContentsWrap.add(sb.toString());
+            }
+
+            for (int j = 0; j < waterMarkContentsWrap.size(); j++) {
+
+                int fontlen = getWatermarkLength(waterMarkContentsWrap.get(j), g);
+                //右下角
+                String s = waterMarkContentsWrap.get(j);
+                g.drawString(s, srcImgWidth - fontlen - 20, srcImgHeight - (contentLength - j) * fontSize - 70);
             }
+
+//            for (int j = 0; j < contentLength; j++) {
+//                waterMarkContent = waterMarkContents[j];
+//                int tempX = 10;
+//                int tempY = fontSize;
+//                // 单字符长度
+//                int tempCharLen = 0;
+//                // 单行字符总长度临时计算
+//                int tempLineLen = 0;
+//                StringBuffer sb = new StringBuffer();
+//                for (int i = 0; i < waterMarkContent.length(); i++) {
+//                    char tempChar = waterMarkContent.charAt(i);
+//                    tempCharLen = getCharLen(tempChar, g);
+//                    tempLineLen += tempCharLen;
+//                    if (tempLineLen >= srcImgWidth) {
+//                        // 长度已经满一行,进行文字叠加
+////                        wrapY += fontSize;
+////                        g.drawString(sb.toString(), tempX, tempY);
+//                        g.drawString(sb.toString(), 20, srcImgHeight - (contentLength - j - 1) * tempY-50);
+//                        // 清空内容,重新追加
+//                        sb.delete(0, sb.length());
+//                        tempLineLen = 0;
+//                    }
+//                    // 追加字符
+//                    sb.append(tempChar);
+//                }
+//                // 通过设置后两个输入参数给水印定位
+//                //右下角
+//                g.drawString(sb.toString(), 20, srcImgHeight - (contentLength - j - 1) * tempY-50);
+////                g.drawString(sb.toString(), srcImgWidth - maxLength, srcImgHeight - (contentLength - j - 1) * tempY - 50);
+////                g.drawString(sb.toString(), srcImgWidth - maxLength, srcImgHeight - (contentLength - j - 1) * tempY);
+//                // g.drawString(sb.toString(), 20, srcImgHeight - (contentLength - j - 1) * tempY - 50);
+//            }
             g.dispose();
 
             // 输出图片
@@ -180,6 +252,7 @@ public class AddWatermarkUtil {
 
     /**
      * MultipartFile 转 File
+     *
      * @param multipartFile
      * @return
      */
@@ -200,6 +273,7 @@ public class AddWatermarkUtil {
 
     /**
      * File 转 MultipartFile
+     *
      * @param file
      * @return
      */