baizonghao
2023-02-24 c4aa721a61edb5ff4502d621897c266b98ef3d1c
ycl-platform/src/main/java/com/ycl/service/video/impl/VideoAlarmReportServiceImpl.java
@@ -2,10 +2,10 @@
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.dto.video.AlarmDataParam;
import com.ycl.dto.video.AlarmParam;
import com.ycl.dto.video.CameraInfParam;
import com.ycl.dto.video.*;
import com.ycl.entity.video.VideoAlarmReport;
import com.ycl.mapper.video.VideoAlarmReportMapper;
import com.ycl.service.caseHandler.IViolationsService;
@@ -14,8 +14,13 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.imageio.ImageIO;
import javax.xml.bind.DatatypeConverter;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@@ -55,26 +60,45 @@
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
            String picData = "";
            if (!StringUtils.isEmpty(alarmParam.getPicData())) {
                String[] strings = alarmParam.getPicData().split(",");
//                String[] strings = alarmParam.getPicData().split(",");
                String extension = "jpg";
                byte[] data = DatatypeConverter.parseBase64Binary(strings[1]);
                byte[] data = DatatypeConverter.parseBase64Binary(alarmParam.getPicData());
                InputStream inputStream = new ByteArrayInputStream(data);
                try {
                    if (!StringUtils.isEmpty(alarmParam.getAlarmData().getAlarmObject().toString())) {
                        String alarmObject = JSON.toJSONString(alarmParam.getAlarmData().getAlarmObject());
                        if (!StringUtils.isEmpty(alarmObject)) {
                            BufferedImage image = ImageIO.read(inputStream);
                            Graphics2D g2 = (Graphics2D)image.getGraphics();
                            g2.setColor(Color.RED);
                            g2.setStroke(new BasicStroke(3.0f));
                            List<AlarmObject> alarmObjects = JSON.parseArray(alarmObject, AlarmObject.class);
                            alarmObjects.forEach(o -> {
                                o.getObjectRect().forEach(rect -> {
                                    g2.drawRect(rect.getX(), rect.getY(), rect.getW(), rect.getH());
                                });
                            });
                            ByteArrayOutputStream os = new ByteArrayOutputStream();
                            ImageIO.write(image, extension, os);
                            inputStream = new ByteArrayInputStream(os.toByteArray());
                        }
                    }
                    picData = mediaFileUtil.savePicture(inputStream, extension);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            List<VideoAlarmReport> videoAlarmReports = new ArrayList<>();
//                for (int i = 0; i < alarmParam.getAlarmData().size(); i++) {
            AlarmDataParam alarmData = alarmParam.getAlarmData();
            /******************** 处理图片 **********************/
            List<VideoAlarmReport> list = getByAlarmId(alarmData.getAlarmID());
            try {
                Date date = sdf.parse(alarmData.getAlarmTime());
                Instant instant = date.toInstant();
                ZoneId zoneId = ZoneId.systemDefault();
                VideoAlarmReport videoAlarmReport = VideoAlarmReport.builder()
                        .ip(cameraInf.getIp()).port(cameraInf.getPort())
                        .user(cameraInf.getUser()).password(cameraInf.getPassword())
@@ -83,16 +107,26 @@
                        .alarmId(alarmData.getAlarmID()).algoType(alarmData.getAlgoType())
                        .algoName(alarmData.getAlgoName()).alarmObj(JSON.toJSONString(alarmData.getAlarmObject()))
                        .build();
                videoAlarmReports.add(videoAlarmReport);
                if (list.size() == 0) {
                    videoAlarmReports.add(videoAlarmReport);
                    this.saveBatch(videoAlarmReports, videoAlarmReports.size());
                    violationsService.saveFromVideo(videoAlarmReports);
                } else {
                    videoAlarmReport.setId(list.get(0).getId());
                    this.updateById(videoAlarmReport);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
//                }
            this.saveBatch(videoAlarmReports, videoAlarmReports.size());
            violationsService.saveFromVideo(videoAlarmReports);
        }
    }
    private List<VideoAlarmReport> getByAlarmId(String alarmId) {
        LambdaQueryWrapper<VideoAlarmReport> queryWrapper = new LambdaQueryWrapper<VideoAlarmReport>().eq(VideoAlarmReport::getAlarmId, alarmId);
        return this.list(queryWrapper);
    }
}