|
@@ -33,6 +33,7 @@ import org.springframework.stereotype.Service;
|
|
|
import javax.imageio.ImageIO;
|
|
|
import javax.imageio.stream.ImageOutputStream;
|
|
|
import java.awt.*;
|
|
|
+import java.awt.Font;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.File;
|
|
|
import java.io.FileOutputStream;
|
|
@@ -92,7 +93,7 @@ public class ImageServiceImpl implements IImageService {
|
|
|
ReferencedEnvelope combinedBounds = new ReferencedEnvelope(DefaultGeographicCRS.WGS84);
|
|
|
|
|
|
for (WktsVo.WktInfo wktInfo : wktsVo.getWktInfos()) {
|
|
|
- if(wktInfo.getWkt() == null || wktInfo.getWkt().isEmpty()){
|
|
|
+ if (wktInfo.getWkt() == null || wktInfo.getWkt().isEmpty()) {
|
|
|
continue;
|
|
|
}
|
|
|
// 将 WKT 转换为几何对象并添加到图层
|
|
@@ -158,15 +159,63 @@ public class ImageServiceImpl implements IImageService {
|
|
|
// Clean up
|
|
|
g2d.dispose();
|
|
|
mapContent.dispose();
|
|
|
+ //添加图例
|
|
|
+ addLegend(filePath, wktsVo);
|
|
|
} catch (IOException ex) {
|
|
|
System.out.println("create image fail");
|
|
|
System.out.println(ex);
|
|
|
ex.printStackTrace();
|
|
|
}
|
|
|
System.out.println("create image success: " + filePath);
|
|
|
+
|
|
|
+
|
|
|
return filePath;
|
|
|
}
|
|
|
|
|
|
+ private void addLegend(String filePath, WktsVo wktsVo) throws IOException {
|
|
|
+ //添加图例
|
|
|
+ File file_legend = new File(filePath);
|
|
|
+ if (file_legend.exists()) {
|
|
|
+ // 将文件对象转化为图片对象
|
|
|
+ BufferedImage originalImage = ImageIO.read(file_legend);
|
|
|
+ int width = originalImage.getWidth();
|
|
|
+ int height = originalImage.getHeight();
|
|
|
+ int legend_width = (int) (width * 0.1);
|
|
|
+ int legend_height = (int) (height * 0.1);
|
|
|
+ int width_1 = width - legend_width;
|
|
|
+ int height_1 = height - legend_height;
|
|
|
+ int legend_count = wktsVo.getWktInfos().size();
|
|
|
+ int height_2 = legend_height / legend_count;
|
|
|
+
|
|
|
+ // 创建一个Graphics2D对象
|
|
|
+ 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);
|
|
|
+ g2d.clearRect(width_1, height_1 + height_2 * legend_i, legend_width, legend_height); // 清除原有
|
|
|
+
|
|
|
+ g2d.setColor(colorBorder);
|
|
|
+ // 设置画笔字体样式为微软雅黑,斜体,文字大小为20px
|
|
|
+ if (height_2 > 20) {
|
|
|
+ g2d.setFont(new Font("微软雅黑", Font.ITALIC, 20));
|
|
|
+ } else {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ legend_i++;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存新图片
|
|
|
+ ImageIO.write(originalImage, "png", new File("D:\\onemapfile\\analyse\\images\\ce2dc2baae4449e3957eb0a4beb66f69\\" + System.currentTimeMillis() + ".png"));
|
|
|
+ // 释放资源
|
|
|
+ g2d.dispose();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private Style createStyle(String borderColor, float borderOpacity, String fillColor, float fillOpacity) {
|
|
|
// 创建样式工厂和过滤工厂
|
|
|
StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();
|