ke
2024-07-19 08a962d2925baa255207fdf979e6fee794f1773b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package cn.gistack.sm.hk.service.impl;
 
import cn.gistack.common.utils.IdUtils;
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;
import cn.gistack.sm.hk.util.GetCameraUtils;
import cn.gistack.sm.hk.vo.HkStaffGaugeMonVO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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;
 
import static cn.gistack.sm.hk.constant.HkConstant.waterGaugeURLs;
 
/**
 * 水尺监测服务接口实现层
 * @author zhongrj
 * @date 2024-06-05
 */
@Service
@Slf4j
public class HkStaffGaugeMonServiceImpl extends ServiceImpl<HkStaffGaugeMonMapper, HkStaffGaugeMon> implements HkStaffGaugeMonService {
 
    @Autowired
    private IOssClient ossClient;
 
 
    /**
     * 自定义分页列表查询
     * @param page
     * @param hkStaffGaugeMon
     * @return
     */
    @Override
    public IPage<HkStaffGaugeMonVO> selectHkStaffGaugeMonPage(IPage<HkStaffGaugeMonVO> page, HkStaffGaugeMonVO hkStaffGaugeMon) {
        return page.setRecords(baseMapper.selectHkStaffGaugeMonPage(page,hkStaffGaugeMon));
    }
 
    /**
     * 保存数据
     * @return
     */
    @Override
    public void saveHkStaffGaugeMonInfo() {
        // 解析json
        String result = GetCameraUtils.getStaffGaugeMon(3, waterGaugeURLs);
        // 保存
        saveHkStaffGaugeMonInfo(result);
    }
 
    /**
     * 保存数据
     * @param result
     * @return
     */
    @Override
    public void saveHkStaffGaugeMonInfo(String result) {
        // 解析json
        log.info("水尺监测信息:" + result);
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        if (null!= jsonArray && jsonArray.size()>0) {
            for (int i = 0; i < jsonArray.size(); i++) {
                String cameraIndexCode = jsonArray.getJSONObject(i).getString("cameraIndexCode");
                Date updateTime = jsonArray.getJSONObject(i).getDate("updateTime");
                // 查询去重
                QueryWrapper<HkStaffGaugeMon> wrapper = new QueryWrapper<>();
                wrapper.eq("camera_index_code",cameraIndexCode).eq("update_time",updateTime);
                List<HkStaffGaugeMon> list = list(wrapper);
                if (list.size()==0) {
                    HkStaffGaugeMon hkStaffGaugeMon = new HkStaffGaugeMon();
                    // 设置数据
                    hkStaffGaugeMon.setId(IdUtils.getIdBy36());
                    hkStaffGaugeMon.setGaugeMonId(jsonArray.getJSONObject(i).getString("id"));
                    hkStaffGaugeMon.setWaterGaugeName(jsonArray.getJSONObject(i).getString("waterGaugeName"));
                    hkStaffGaugeMon.setWaterGaugeCode(jsonArray.getJSONObject(i).getString("waterGaugeCode"));
                    hkStaffGaugeMon.setCameraIndexCode(jsonArray.getJSONObject(i).getString("cameraIndexCode"));
                    hkStaffGaugeMon.setCameraName(jsonArray.getJSONObject(i).getString("cameraName"));
                    hkStaffGaugeMon.setLocation(jsonArray.getJSONObject(i).getString("location"));
                    hkStaffGaugeMon.setMarkPic(jsonArray.getJSONObject(i).getString("markPic"));
                    hkStaffGaugeMon.setMarkData(jsonArray.getJSONObject(i).getString("markData"));
                    hkStaffGaugeMon.setCreateTime(jsonArray.getJSONObject(i).getDate("createTime"));
                    hkStaffGaugeMon.setConfigUpdateTime(jsonArray.getJSONObject(i).getDate("configUpdateTime"));
                    hkStaffGaugeMon.setUpdateTime(jsonArray.getJSONObject(i).getDate("updateTime"));
                    hkStaffGaugeMon.setDeleteStatus(jsonArray.getJSONObject(i).getInteger("deleteStatus"));
                    hkStaffGaugeMon.setCurrentPic(jsonArray.getJSONObject(i).getString("currentPic"));
                    hkStaffGaugeMon.setAlarmHeight(jsonArray.getJSONObject(i).getDouble("alarmHeight"));
                    hkStaffGaugeMon.setRemark1(jsonArray.getJSONObject(i).getString("remark1"));
                    hkStaffGaugeMon.setRemark2(jsonArray.getJSONObject(i).getString("remark2"));
                    hkStaffGaugeMon.setRemark3(jsonArray.getJSONObject(i).getString("remark3"));
                    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));
    }
}