package com.ycl.service.video.impl; 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.*; import com.ycl.entity.video.VideoAlarmReport; import com.ycl.mapper.video.VideoAlarmReportMapper; import com.ycl.service.caseHandler.IViolationsService; import com.ycl.service.video.IVideoAlarmReportService; import com.ycl.utils.MediaFileUtil; 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; import java.time.Instant; import java.time.ZoneId; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * VideoAlarmReportServiceImpl * * @version V1.0 * @author: AI * @date: 2022-09-20 17:36 **/ @Service public class VideoAlarmReportServiceImpl extends ServiceImpl implements IVideoAlarmReportService { private MediaFileUtil mediaFileUtil; private IViolationsService violationsService; @Autowired public void setMediaFileUtil(MediaFileUtil mediaFileUtil) { this.mediaFileUtil = mediaFileUtil; } @Autowired public void setViolationsService(IViolationsService violationsService) { this.violationsService = violationsService; } @Override public void save(AlarmParam alarmParam) { if (alarmParam != null && alarmParam.getAlarmData() != null) { CameraInfParam cameraInf = alarmParam.getCameraInf(); 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 extension = "jpg"; 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 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 videoAlarmReports = new ArrayList<>(); AlarmDataParam alarmData = alarmParam.getAlarmData(); List 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()) .channel(cameraInf.getChannel()).platResourceId(cameraInf.getPlatResourceID()) .picData(picData).alarmTime(instant.atZone(zoneId).toLocalDateTime()) .alarmId(alarmData.getAlarmID()).algoType(alarmData.getAlgoType()) .algoName(alarmData.getAlgoName()).alarmObj(JSON.toJSONString(alarmData.getAlarmObject())) .build(); 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(); } } } private List getByAlarmId(String alarmId) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper().eq(VideoAlarmReport::getAlarmId, alarmId); return this.list(queryWrapper); } }