SceneServiceImpl.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package com.fdkankan.scene.service.impl;
  2. import cn.hutool.core.img.ImgUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.lang.UUID;
  5. import cn.hutool.core.util.StrUtil;
  6. import com.alibaba.fastjson.JSON;
  7. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  8. import com.fdkankan.common.constant.CommonOperStatus;
  9. import com.fdkankan.common.constant.ErrorCode;
  10. import com.fdkankan.common.exception.BusinessException;
  11. import com.fdkankan.common.util.DateExtUtil;
  12. import com.fdkankan.common.util.FileUtils;
  13. import com.fdkankan.fyun.config.FYunFileConfig;
  14. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  15. import com.fdkankan.model.constants.ConstantFilePath;
  16. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  17. import com.fdkankan.redis.constant.RedisKey;
  18. import com.fdkankan.redis.util.RedisUtil;
  19. import com.fdkankan.scene.bean.BodySegmentStatusBean;
  20. import com.fdkankan.scene.entity.ScenePlus;
  21. import com.fdkankan.scene.entity.ScenePlusExt;
  22. import com.fdkankan.scene.service.IScenePlusExtService;
  23. import com.fdkankan.scene.service.IScenePlusService;
  24. import com.fdkankan.scene.service.ISceneService;
  25. import com.fdkankan.scene.util.OssBodySegmentUtil;
  26. import com.fdkankan.web.response.ResultData;
  27. import lombok.extern.slf4j.Slf4j;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.beans.factory.annotation.Value;
  30. import org.springframework.stereotype.Service;
  31. import org.springframework.web.multipart.MultipartFile;
  32. import sun.rmi.runtime.Log;
  33. import javax.imageio.ImageIO;
  34. import java.awt.*;
  35. import java.awt.image.BufferedImage;
  36. import java.io.File;
  37. import java.io.IOException;
  38. import java.math.BigDecimal;
  39. import java.util.Calendar;
  40. import java.util.HashMap;
  41. import java.util.Map;
  42. import java.util.Objects;
  43. @Slf4j
  44. @Service("sceneService")
  45. public class SceneServiceImpl implements ISceneService {
  46. @Value("${queue.bodySegment:body-segment}")
  47. private String queueName;
  48. @Value("${oss.bodySegment.bucket:4dkankan-huadong}")
  49. private String bodySegmentBucket;
  50. @Value("${oss.bodySegment.point:oss-cn-shanghai.aliyuncs.com}")
  51. private String bodySegmentHost;
  52. @Autowired
  53. private OssBodySegmentUtil ossBodySegmentUtil;
  54. @Autowired
  55. private RedisUtil redisUtil;
  56. @Autowired
  57. private RabbitMqProducer rabbitMqProducer;
  58. @Autowired
  59. private FYunFileServiceInterface fYunFileService;
  60. @Autowired
  61. public FYunFileConfig fYunFileConfig;
  62. @Autowired
  63. public IScenePlusService scenePlusService;
  64. @Autowired
  65. public IScenePlusExtService scenePlusExtService;
  66. @Override
  67. public ResultData uploadBodySegment(MultipartFile file, Integer rotate) throws Exception {
  68. if(!FileUtils.checkFileSizeIsLimit(file.getSize(), 10, "M")){
  69. throw new BusinessException(ErrorCode.FAILURE_CODE_4003, "10M");
  70. }
  71. String uuid = UUID.randomUUID().toString();
  72. String fileName = file.getOriginalFilename();
  73. String extName = fileName.substring(fileName.lastIndexOf("."));
  74. File tempFile = File.createTempFile(uuid, extName);
  75. file.transferTo(tempFile);
  76. //判断是否需要旋转
  77. if(Objects.nonNull(rotate) && rotate != 0){
  78. Image rotateImg = ImgUtil.rotate(ImageIO.read(tempFile), rotate);
  79. File tempRotateFile = File.createTempFile(uuid + "-rotate", extName);
  80. ImgUtil.write(rotateImg, tempRotateFile);
  81. tempFile = tempRotateFile;
  82. }
  83. //校验像素
  84. BufferedImage bufferedImage = ImgUtil.read(tempFile.getPath());
  85. Float scale = 1F;
  86. Float widthScale = 1F;
  87. Float heightScale = 1F;
  88. int width = bufferedImage.getWidth();
  89. int height = bufferedImage.getHeight();
  90. if(width > 2000){
  91. widthScale = new BigDecimal(2000).divide(new BigDecimal(width),5, BigDecimal.ROUND_DOWN).floatValue();
  92. }
  93. if(height > 2000){
  94. heightScale = new BigDecimal(2000).divide(new BigDecimal(height),5, BigDecimal.ROUND_DOWN).floatValue();
  95. }
  96. scale = widthScale > heightScale ? heightScale : widthScale;
  97. ImgUtil.scale(new File(tempFile.getPath()), new File(tempFile.getPath()), scale);
  98. String orgImgOssPath = "body_segment/original/" + tempFile.getName();
  99. ossBodySegmentUtil.uploadOss(tempFile.getPath(), orgImgOssPath);
  100. // fYunFileService.uploadFile(bodySegmentBucket, tempFile.getPath(), orgImgOssPath);
  101. BodySegmentStatusBean bodySegmentStatusBean = BodySegmentStatusBean.builder().uuid(uuid).status(CommonOperStatus.WAITING.code()).build();
  102. redisUtil.set(String.format(RedisKey.SCENE_BODY_SEGMENT, uuid), JSON.toJSONString(bodySegmentStatusBean), RedisKey.CAMERA_EXPIRE_7_TIME);
  103. Map<String, String> map = new HashMap<>();
  104. map.put("uuid", uuid);
  105. map.put("imgUrl", "https://" + bodySegmentBucket + "." + bodySegmentHost + "/" + orgImgOssPath);
  106. rabbitMqProducer.sendByWorkQueue(queueName, map);
  107. return ResultData.ok(uuid);
  108. }
  109. public static void main(String[] args) throws IOException {
  110. Image rotateImg = ImgUtil.rotate(ImageIO.read(FileUtil.file("C:\\Users\\dsx\\Desktop\\IMG_6633.HEIC.JPG")), 90);
  111. ImgUtil.write(rotateImg, FileUtil.file("C:\\Users\\dsx\\Desktop\\IMG_6633.HEIC_2.JPG"));
  112. }
  113. @Override
  114. public void bodySegmentHandler(String imgUrl, String uuid) {
  115. String progress = redisUtil.hget(RedisKey.SCENE_BODY_SEGMENT, uuid);
  116. BodySegmentStatusBean bodySegmentStatusBean = null;
  117. try {
  118. if(StrUtil.isEmpty(progress)){
  119. bodySegmentStatusBean = JSON.parseObject(progress, BodySegmentStatusBean.class);
  120. }
  121. if(Objects.isNull(bodySegmentStatusBean)){
  122. bodySegmentStatusBean = new BodySegmentStatusBean();
  123. bodySegmentStatusBean.setUuid(uuid);
  124. }
  125. String dir = ConstantFilePath.BASE_PATH + "/bodySegment/" +
  126. DateExtUtil.format(Calendar.getInstance().getTime(), DateExtUtil.dateStyle6);
  127. String fileName = uuid + ".png";
  128. String imgPath = dir + "/" + fileName;
  129. ossBodySegmentUtil.extracted(imgUrl, dir, fileName);
  130. if(!FileUtil.exist(imgPath)){
  131. throw new Exception("提取图片失败");
  132. }
  133. String targetOssImgPath = "body_segment/segment/" + uuid + ".png";
  134. fYunFileService.uploadFile(imgPath, targetOssImgPath);
  135. bodySegmentStatusBean.setStatus(CommonOperStatus.SUCCESS.code());
  136. bodySegmentStatusBean.setImageUrl(fYunFileConfig.getHost() + targetOssImgPath);
  137. redisUtil.set(String.format(RedisKey.SCENE_BODY_SEGMENT, uuid), JSON.toJSONString(bodySegmentStatusBean), RedisKey.CAMERA_EXPIRE_7_TIME);
  138. } catch (Exception e) {
  139. bodySegmentStatusBean.setStatus(CommonOperStatus.FAILD.code());
  140. redisUtil.set(String.format(RedisKey.SCENE_BODY_SEGMENT, uuid), JSON.toJSONString(bodySegmentStatusBean), RedisKey.CAMERA_EXPIRE_7_TIME);
  141. }finally {
  142. try {
  143. //免费版qps不能大于2,故休眠一秒
  144. Thread.sleep(1000L);
  145. } catch (InterruptedException e) {
  146. e.printStackTrace();
  147. }
  148. }
  149. }
  150. @Override
  151. public void saveSceneOientation(Map<String, Object> map) {
  152. String num = (String) map.get("num");
  153. String orientation = (String)map.get("orientation");
  154. Integer status = (Integer)map.get("status");
  155. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  156. if(Objects.isNull(scenePlus)){
  157. return;
  158. }
  159. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  160. if(status == 0){
  161. scenePlusExtService.update(new LambdaUpdateWrapper<ScenePlusExt>().eq(ScenePlusExt::getId, scenePlusExt.getId()).setSql("orientation = null"));
  162. }else{
  163. scenePlusExt.setOrientation(orientation);
  164. scenePlusExtService.updateById(scenePlusExt);
  165. }
  166. }
  167. @Override
  168. public ResultData getBodySegmentStatus(String uuid) {
  169. String progress = redisUtil.get(String.format(RedisKey.SCENE_BODY_SEGMENT, uuid));
  170. if(StrUtil.isEmpty(progress)){
  171. throw new BusinessException(ErrorCode.FAILURE_CODE_5038);
  172. }
  173. BodySegmentStatusBean bodySegmentStatusBean = JSON.parseObject(progress, BodySegmentStatusBean.class);
  174. return ResultData.ok(bodySegmentStatusBean);
  175. }
  176. }