Prechádzať zdrojové kódy

添加图例填充色和对比度

DESKTOP-2K9OVK9\siwei 5 mesiacov pred
rodič
commit
b85dd5ca22

+ 16 - 6
onemap-modules/onemap-spatial/src/main/java/com/onemap/spatial/domain/WktsVo.java

@@ -18,19 +18,29 @@ public class WktsVo {
         private String borderColor = "#ff0000";
         private float borderOpacity = 1;
         //图例名称
-        private String borderLegendText = "";
+        private String legendText = "";
         //图例是否显示
-        private boolean borderLegendDisplay = false;
+        private boolean legendDisplay = false;
+        private String legendColor = "";
+        private Float legendColorOpacity;
 
         private String fillColor = "#ffffff";
         private float fillOpacity = 0.3F;
 
-        public boolean getBorderLegendDisplay() {
-            return borderLegendDisplay;
+        public String getLegendText() {
+            return legendText;
         }
 
-        public void setBorderLegendDisplay(boolean borderLegendDisplay) {
-            this.borderLegendDisplay = borderLegendDisplay;
+        public void setLegendText(String legendText) {
+            this.legendText = legendText;
+        }
+
+        public boolean getLegendDisplay() {
+            return legendDisplay;
+        }
+
+        public void setLegendDisplay(boolean legendDisplay) {
+            this.legendDisplay = legendDisplay;
         }
     }
 }

+ 32 - 8
onemap-modules/onemap-spatial/src/main/java/com/onemap/spatial/service/impl/ImageServiceImpl.java

@@ -160,7 +160,7 @@ public class ImageServiceImpl implements IImageService {
             g2d.dispose();
             mapContent.dispose();
             //添加图例
-            addLegend(filePath, wktsVo);
+            filePath = addLegend(filePath, wktsVo);
         } catch (IOException ex) {
             System.out.println("create image fail");
             System.out.println(ex);
@@ -172,7 +172,15 @@ public class ImageServiceImpl implements IImageService {
         return filePath;
     }
 
-    private void addLegend(String filePath, WktsVo wktsVo) throws IOException {
+    private String addLegend(String filePath, WktsVo wktsVo) throws IOException {
+        // 图片保存路径
+        String fileDir = rootPath + "/images/" + StringUtils.getUUID() + "/";
+        File dir = new File(fileDir);
+        if (!dir.exists()) {
+            dir.mkdirs();
+        }
+        String filePathNow = fileDir + System.currentTimeMillis() + ".png";
+
         //添加图例
         File file_legend = new File(filePath);
         if (file_legend.exists()) {
@@ -191,12 +199,25 @@ public class ImageServiceImpl implements IImageService {
             Graphics2D g2d = (Graphics2D) originalImage.getGraphics();
             int legend_i = 0;
             for (WktsVo.WktInfo wktInfo : wktsVo.getWktInfos()) {
-                if (wktInfo.getBorderLegendDisplay()) {
-                    Color colorBorder = Color.decode(wktInfo.getBorderColor());
-                    g2d.setBackground(colorBorder);
+                if (wktInfo.getLegendDisplay()) {
+                    String legendColor = wktInfo.getLegendColor();
+                    if (StringUtils.isEmpty(legendColor)) {
+                        legendColor = wktInfo.getFillColor();
+                    }
+                    Float legendColorOpacity = wktInfo.getLegendColorOpacity();
+                    if (legendColorOpacity == null) {
+                        legendColorOpacity = wktInfo.getFillOpacity();
+                    }
+                    legendColorOpacity = legendColorOpacity * 255;
+                    Color color = Color.decode(legendColor);
+                    int red = color.getRed();
+                    int green = color.getGreen();
+                    int blue = color.getBlue();
+                    Color colorFill = new Color(red, green, blue, legendColorOpacity.intValue());
+                    g2d.setBackground(colorFill);
                     g2d.clearRect(width_1, height_1 + height_2 * legend_i, legend_width, legend_height); // 清除原有
 
-                    g2d.setColor(colorBorder);
+                    g2d.setColor(colorFill);
                     // 设置画笔字体样式为微软雅黑,斜体,文字大小为20px
                     if (height_2 > 20) {
                         g2d.setFont(new Font("微软雅黑", Font.ITALIC, 20));
@@ -204,15 +225,18 @@ public class ImageServiceImpl implements IImageService {
                         g2d.setFont(new Font("微软雅黑", Font.ITALIC, height_2));
                     }
                     // 写上水印文字和坐标
-                    g2d.drawString(wktInfo.getBorderLegendText(), width_1 - legend_height, height_1 + height_2 * (legend_i) + height_2 / 2);
+                    g2d.drawString(wktInfo.getLegendText(), width_1 - legend_height, height_1 + height_2 * (legend_i) + height_2 / 2);
                 }
                 legend_i++;
             }
 
             // 保存新图片
-            ImageIO.write(originalImage, "png", new File("D:\\onemapfile\\analyse\\images\\ce2dc2baae4449e3957eb0a4beb66f69\\" + System.currentTimeMillis() + ".png"));
+            ImageIO.write(originalImage, "png", new File(filePathNow));
             // 释放资源
             g2d.dispose();
+            return filePathNow;
+        } else {
+            return null;
         }
     }