zrj
2024-06-25 a929d99e6b90e426a5e1bd2eee1d8820986b6c61
skjcmanager/skjcmanager-service/skjcmanager-sm/src/main/java/cn/gistack/sm/hk/service/impl/HkStaffGaugeMonServiceImpl.java
@@ -1,7 +1,9 @@
package cn.gistack.sm.hk.service.impl;
import cn.gistack.common.utils.IdUtils;
import cn.gistack.sm.hk.entity.HkEvent;
import cn.gistack.common.utils.SpringContextUtil;
import cn.gistack.resource.feign.IOssClient;
import cn.gistack.sm.hk.constant.HkEventConstant;
import cn.gistack.sm.hk.entity.HkStaffGaugeMon;
import cn.gistack.sm.hk.mapper.HkStaffGaugeMonMapper;
import cn.gistack.sm.hk.service.HkStaffGaugeMonService;
@@ -13,9 +15,21 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -29,6 +43,10 @@
@Service
@Slf4j
public class HkStaffGaugeMonServiceImpl extends ServiceImpl<HkStaffGaugeMonMapper, HkStaffGaugeMon> implements HkStaffGaugeMonService {
   @Autowired
   private IOssClient ossClient;
   /**
    * 自定义分页列表查询
@@ -96,10 +114,165 @@
               hkStaffGaugeMon.setRemark4(jsonArray.getJSONObject(i).getString("remark4"));
               hkStaffGaugeMon.setRemark5(jsonArray.getJSONObject(i).getString("remark5"));
               hkStaffGaugeMon.setSysCreateTime(new Date());
               // 设置标记图片
               hkStaffGaugeMon.setTaggedPic(setTaggedUrl(hkStaffGaugeMon));
               // 保存
               save(hkStaffGaugeMon);
            }
         }
      }
   }
   /**
    * 设置标记图片
    * @param hkStaffGaugeMon
    * @return
    */
   private String setTaggedUrl(HkStaffGaugeMon hkStaffGaugeMon) {
      String url = null;
      if (!Strings.isBlank(hkStaffGaugeMon.getMarkPic()) &&
      !Strings.isBlank(hkStaffGaugeMon.getMarkData())){
         String tagGedUrl = hkStaffGaugeMon.getMarkPic();
         // 获取环境
         String activeProfile = SpringContextUtil.getActiveProfile();
         String preUrl = HkEventConstant.picUrlMap.get(activeProfile);
         // 替换url
         tagGedUrl = tagGedUrl.replace("http://10.42.7.245:6120",preUrl);
         // url 转换
         url = drawAndGetPictureUrl(tagGedUrl,hkStaffGaugeMon.getMarkData(),hkStaffGaugeMon.getAlarmHeight(),Color.RED);
      }
      return url;
   }
   /**
    * 图片处理
    * @param url 原始图片地址
    * @param data:图片数据
    * @param drawColor:画框的颜色
    * @return
    * @throws Exception
    */
   public String drawAndGetPictureUrl(String url,String data,Double alarmHeight, Color drawColor) {
      String tagGedUrl = null;
      BufferedImage bufferedImage = null;
      // 读取图片文件,得到BufferedImage对象
      try {
         InputStream inputStream = null;
         HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
         connection.setRequestMethod("GET");
         if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            inputStream = connection.getInputStream();
         }
         bufferedImage = ImageIO.read(inputStream);
         // 得到Graphics2D 对象
         Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();
         // 设置颜色、画笔粗细
         g2D.setColor(drawColor);
         g2D.setStroke(new BasicStroke(2f));
         // 绘制线
         drawLine(g2D,data,bufferedImage);
         // 标记点位文字
         drawWord(g2D,data,bufferedImage,alarmHeight,24);
         // 释放图形上下文
         g2D.dispose();
         // 将处理后的图片转换为输入流
         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
         ImageIO.write(bufferedImage, "jpg", new File("F:\\out.jpg"));
         // 写入
         ImageIO.write(bufferedImage, "jpg", byteArrayOutputStream);
         ByteArrayInputStream stream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
         String fileName = System.currentTimeMillis()  + ".jpg";
         // 使用MinIO客户端上传图片
         MockMultipartFile multipartFile
            = new MockMultipartFile("__init__.py", fileName, "application/octet-stream", stream);
         // 上传
         tagGedUrl = ossClient.uploadFile(fileName, multipartFile);
         // 返回
         return tagGedUrl;
      } catch (Exception e) {
         log.error("绘制图片失败:,原图片:{},error:{}", url, e);
      }
      // 返回
      return tagGedUrl;
   }
   /**
    * 画矩形
    * @param x:该参数用于返回检测框左上角位置的横坐标(x)所在的像素位置,结合剩余参数可唯一确定检测框的大小和位置。
    * @param y:该参数用于返回检测框左上角位置的纵坐标(y)所在的像素位置,结合剩余参数可唯一确定检测框的大小和位置。
    * @param width: 该参数用于返回检测框的宽度(由左上角出发在x轴向右延伸的长度),结合剩余参数可唯一确定检测框的大小和位置。
    * @param height:该参数用于返回检测框的高度(由左上角出发在y轴向下延伸的长度),结合剩余参数可唯一确定检测框的大小和位置。
    * @param g2D
    */
   public void drawRectangle(int x, int y, int width, int height, Graphics2D g2D) {
      //绘制矩形
      g2D.drawRect(x, y, width, height);
   }
   /**
    * 画线
    * @param g2D
    * @param data
    * @param bufferedImage
    */
   public void drawLine(Graphics2D g2D,String data,BufferedImage bufferedImage) {
      int width = bufferedImage.getWidth();
      int height = bufferedImage.getHeight();
      // 解析data
      JSONObject jsonObject = JSONObject.parseObject(data);
      // 转换为Map
      Map<String, Double> map = new HashMap<>();
      for (String key : jsonObject.keySet()) {
         map.put(key, jsonObject.getDoubleValue(key));
      }
      // 打印转换后的Map
      Line2D.Double line = new Line2D.Double(map.get("x1")*width, map.get("y1")*height,
         map.get("x2")*width, map.get("y2")*height);
      g2D.draw(line);
      Line2D.Double line1 = new Line2D.Double(map.get("x2")*width, map.get("y2")*height,
         map.get("x3")*width, map.get("y3")*height);
      g2D.draw(line1);
   }
   /**
    * 画线--int 类型
    * @param g2D
    * @param data
    */
   public void drawLineInt(Graphics2D g2D,String data) {
      //绘制矩形
      // 定义多个点,每个点由x和y坐标组成
      int[] xPoints = {10, 100, 50, 150, 20};
      int[] yPoints = {10, 150, 100, 50, 10};
      // 绘制多点划线
      g2D.drawPolyline(xPoints, yPoints, xPoints.length);
   }
   /**
    * 画文字
    * @param g2D
    * @param data
    * @param data 数据
    * @param bufferedImage
    * @param size 文字大小
    */
   public void drawWord(Graphics2D g2D, String data,BufferedImage bufferedImage,Double alarmHeight,int size) {
      g2D.setFont(new Font("", Font.BOLD, size));
      int width = bufferedImage.getWidth();
      int height = bufferedImage.getHeight();
      // 解析data
      JSONObject jsonObject = JSONObject.parseObject(data);
      // 转换为Map
      Map<String, Double> map = new HashMap<>();
      for (String key : jsonObject.keySet()) {
         map.put(key, jsonObject.getDoubleValue(key));
      }
      g2D.drawString("A:" + map.get("h3") + "米", (float)(map.get("x3")*width-5), (float) (map.get("y3")*height-10));
      g2D.drawString("B:" + map.get("h2") + "米", (float)(map.get("x2")*width-5), (float) (map.get("y2")*height-10));
      g2D.drawString("C:" + map.get("h1") + "米", (float)(map.get("x1")*width-5), (float) (map.get("y1")*height-10));
      g2D.setColor(Color.BLUE);
      g2D.drawString("当前水位:" + alarmHeight + "米", (float)(map.get("x2")*width-100), (float) (map.get("y2")*height+60));
   }
}