package com.fdkankan.scene.service.impl; import cn.hutool.core.img.ImgUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.lang.UUID; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.fdkankan.common.constant.CommonOperStatus; import com.fdkankan.common.constant.ErrorCode; import com.fdkankan.common.exception.BusinessException; import com.fdkankan.common.util.DateExtUtil; import com.fdkankan.common.util.FileUtils; import com.fdkankan.fyun.config.FYunFileConfig; import com.fdkankan.fyun.face.FYunFileServiceInterface; import com.fdkankan.model.constants.ConstantFilePath; import com.fdkankan.rabbitmq.util.RabbitMqProducer; import com.fdkankan.redis.constant.RedisKey; import com.fdkankan.redis.util.RedisUtil; import com.fdkankan.scene.bean.BodySegmentStatusBean; import com.fdkankan.scene.entity.ScenePlus; import com.fdkankan.scene.entity.ScenePlusExt; import com.fdkankan.scene.service.IScenePlusExtService; import com.fdkankan.scene.service.IScenePlusService; import com.fdkankan.scene.service.ISceneService; import com.fdkankan.scene.util.OssBodySegmentUtil; import com.fdkankan.web.response.ResultData; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import sun.rmi.runtime.Log; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.math.BigDecimal; import java.util.Calendar; import java.util.HashMap; import java.util.Map; import java.util.Objects; @Slf4j @Service("sceneService") public class SceneServiceImpl implements ISceneService { @Value("${queue.bodySegment:body-segment}") private String queueName; @Value("${oss.bodySegment.bucket:4dkankan-huadong}") private String bodySegmentBucket; @Value("${oss.bodySegment.point:oss-cn-shanghai.aliyuncs.com}") private String bodySegmentHost; @Autowired private OssBodySegmentUtil ossBodySegmentUtil; @Autowired private RedisUtil redisUtil; @Autowired private RabbitMqProducer rabbitMqProducer; @Autowired private FYunFileServiceInterface fYunFileService; @Autowired public FYunFileConfig fYunFileConfig; @Autowired public IScenePlusService scenePlusService; @Autowired public IScenePlusExtService scenePlusExtService; @Override public ResultData uploadBodySegment(MultipartFile file, Integer rotate) throws Exception { if(!FileUtils.checkFileSizeIsLimit(file.getSize(), 10, "M")){ throw new BusinessException(ErrorCode.FAILURE_CODE_4003, "10M"); } String uuid = UUID.randomUUID().toString(); String fileName = file.getOriginalFilename(); String extName = fileName.substring(fileName.lastIndexOf(".")); File tempFile = File.createTempFile(uuid, extName); file.transferTo(tempFile); //判断是否需要旋转 if(Objects.nonNull(rotate) && rotate != 0){ Image rotateImg = ImgUtil.rotate(ImageIO.read(tempFile), rotate); File tempRotateFile = File.createTempFile(uuid + "-rotate", extName); ImgUtil.write(rotateImg, tempRotateFile); tempFile = tempRotateFile; } //校验像素 BufferedImage bufferedImage = ImgUtil.read(tempFile.getPath()); Float scale = 1F; Float widthScale = 1F; Float heightScale = 1F; int width = bufferedImage.getWidth(); int height = bufferedImage.getHeight(); if(width > 2000){ widthScale = new BigDecimal(2000).divide(new BigDecimal(width),5, BigDecimal.ROUND_DOWN).floatValue(); } if(height > 2000){ heightScale = new BigDecimal(2000).divide(new BigDecimal(height),5, BigDecimal.ROUND_DOWN).floatValue(); } scale = widthScale > heightScale ? heightScale : widthScale; ImgUtil.scale(new File(tempFile.getPath()), new File(tempFile.getPath()), scale); String orgImgOssPath = "body_segment/original/" + tempFile.getName(); ossBodySegmentUtil.uploadOss(tempFile.getPath(), orgImgOssPath); // fYunFileService.uploadFile(bodySegmentBucket, tempFile.getPath(), orgImgOssPath); BodySegmentStatusBean bodySegmentStatusBean = BodySegmentStatusBean.builder().uuid(uuid).status(CommonOperStatus.WAITING.code()).build(); redisUtil.set(String.format(RedisKey.SCENE_BODY_SEGMENT, uuid), JSON.toJSONString(bodySegmentStatusBean), RedisKey.CAMERA_EXPIRE_7_TIME); Map map = new HashMap<>(); map.put("uuid", uuid); map.put("imgUrl", "https://" + bodySegmentBucket + "." + bodySegmentHost + "/" + orgImgOssPath); rabbitMqProducer.sendByWorkQueue(queueName, map); return ResultData.ok(uuid); } public static void main(String[] args) throws IOException { Image rotateImg = ImgUtil.rotate(ImageIO.read(FileUtil.file("C:\\Users\\dsx\\Desktop\\IMG_6633.HEIC.JPG")), 90); ImgUtil.write(rotateImg, FileUtil.file("C:\\Users\\dsx\\Desktop\\IMG_6633.HEIC_2.JPG")); } @Override public void bodySegmentHandler(String imgUrl, String uuid) { String progress = redisUtil.hget(RedisKey.SCENE_BODY_SEGMENT, uuid); BodySegmentStatusBean bodySegmentStatusBean = null; try { if(StrUtil.isEmpty(progress)){ bodySegmentStatusBean = JSON.parseObject(progress, BodySegmentStatusBean.class); } if(Objects.isNull(bodySegmentStatusBean)){ bodySegmentStatusBean = new BodySegmentStatusBean(); bodySegmentStatusBean.setUuid(uuid); } String dir = ConstantFilePath.BASE_PATH + "/bodySegment/" + DateExtUtil.format(Calendar.getInstance().getTime(), DateExtUtil.dateStyle6); String fileName = uuid + ".png"; String imgPath = dir + "/" + fileName; ossBodySegmentUtil.extracted(imgUrl, dir, fileName); if(!FileUtil.exist(imgPath)){ throw new Exception("提取图片失败"); } String targetOssImgPath = "body_segment/segment/" + uuid + ".png"; fYunFileService.uploadFile(imgPath, targetOssImgPath); bodySegmentStatusBean.setStatus(CommonOperStatus.SUCCESS.code()); bodySegmentStatusBean.setImageUrl(fYunFileConfig.getHost() + targetOssImgPath); redisUtil.set(String.format(RedisKey.SCENE_BODY_SEGMENT, uuid), JSON.toJSONString(bodySegmentStatusBean), RedisKey.CAMERA_EXPIRE_7_TIME); } catch (Exception e) { bodySegmentStatusBean.setStatus(CommonOperStatus.FAILD.code()); redisUtil.set(String.format(RedisKey.SCENE_BODY_SEGMENT, uuid), JSON.toJSONString(bodySegmentStatusBean), RedisKey.CAMERA_EXPIRE_7_TIME); }finally { try { //免费版qps不能大于2,故休眠一秒 Thread.sleep(1000L); } catch (InterruptedException e) { e.printStackTrace(); } } } @Override public void saveSceneOientation(Map map) { String num = (String) map.get("num"); String orientation = (String)map.get("orientation"); Integer status = (Integer)map.get("status"); ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num); if(Objects.isNull(scenePlus)){ return; } ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId()); if(status == 0){ scenePlusExtService.update(new LambdaUpdateWrapper().eq(ScenePlusExt::getId, scenePlusExt.getId()).setSql("orientation = null")); }else{ scenePlusExt.setOrientation(orientation); scenePlusExtService.updateById(scenePlusExt); } } @Override public ResultData getBodySegmentStatus(String uuid) { String progress = redisUtil.get(String.format(RedisKey.SCENE_BODY_SEGMENT, uuid)); if(StrUtil.isEmpty(progress)){ throw new BusinessException(ErrorCode.FAILURE_CODE_5038); } BodySegmentStatusBean bodySegmentStatusBean = JSON.parseObject(progress, BodySegmentStatusBean.class); return ResultData.ok(bodySegmentStatusBean); } }