deason 6 years ago
parent
commit
26a575c066

+ 31 - 2
examcloud-core-questions-base/src/main/java/cn/com/qmth/examcloud/core/questions/base/ImageUtils.java

@@ -12,12 +12,17 @@ import org.jsoup.nodes.Document;
 import org.jsoup.nodes.Element;
 import org.jsoup.nodes.Element;
 import org.jsoup.select.Elements;
 import org.jsoup.select.Elements;
 
 
+import java.util.HashMap;
+import java.util.Map;
+
 public class ImageUtils {
 public class ImageUtils {
     private static final String TAG_IMG = "img";
     private static final String TAG_IMG = "img";
     private static final String UNIT = "px";
     private static final String UNIT = "px";
     private static final int MAX_WIDTH = 350;
     private static final int MAX_WIDTH = 350;
     private static final String STYLE_WIDTH = "width:%spx;";
     private static final String STYLE_WIDTH = "width:%spx;";
     private static final String STYLE_HEIGHT = "height:%spx;";
     private static final String STYLE_HEIGHT = "height:%spx;";
+    public static final String ATTR_WIDTH = "width";
+    public static final String ATTR_HEIGHT = "height";
 
 
     /**
     /**
      * 如果图片尺寸过大,则按比例缩放图片的宽度高度
      * 如果图片尺寸过大,则按比例缩放图片的宽度高度
@@ -90,6 +95,27 @@ public class ImageUtils {
         return 0;
         return 0;
     }
     }
 
 
+    public static Map<String, Integer> parseImageWidthAndHeight(String imgTagContent) {
+        Map<String, Integer> result = new HashMap<>();
+        if (imgTagContent == null) {
+            return result;
+        }
+
+        Document document = Jsoup.parse(imgTagContent);
+        Elements elements = document.getElementsByTag(TAG_IMG);
+        if (elements == null || elements.isEmpty()) {
+            return result;
+        }
+
+        //只处理第一个图片元素
+        Element element = elements.first();
+        String widthContent = element.attr(ATTR_WIDTH);
+        String heightContent = element.attr(ATTR_HEIGHT);
+        result.put(ATTR_WIDTH, parseValue(widthContent));
+        result.put(ATTR_HEIGHT, parseValue(heightContent));
+        return result;
+    }
+
     public static int parseValue(String attrValue) {
     public static int parseValue(String attrValue) {
         if (attrValue == null || "".equals(attrValue)) {
         if (attrValue == null || "".equals(attrValue)) {
             return 0;
             return 0;
@@ -103,9 +129,12 @@ public class ImageUtils {
         }
         }
     }
     }
 
 
-    public static void main(String[] args) {
-        String html = "<p><IMG src=\"abc\" style=\"width: 600PX; height:300PX;\"/><img src=\"xyz\" style=\"width:300px;\"/></p>";
+    public static void mainTest(String[] args) {
+        String html = "<p><IMG src=\"abc\" width=\"800px\" height=\"400px\" style=\"width: 600PX; height:300PX;\"/><img src=\"xyz\" style=\"width:300px;\"/></p>";
         System.out.println(ImageUtils.reSizeImg(html));
         System.out.println(ImageUtils.reSizeImg(html));
+
+        Map<String, Integer> data = parseImageWidthAndHeight(html);
+        System.out.println(data.get(ATTR_WIDTH) + " - " + data.get(ATTR_HEIGHT));
     }
     }
 
 
 }
 }