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.fdkankan.common.constant.CommonOperStatus; import com.fdkankan.common.constant.ErrorCode; import com.fdkankan.common.constant.SceneConstant; import com.fdkankan.common.exception.BusinessException; import com.fdkankan.common.util.DateExtUtil; import com.fdkankan.common.util.FileUtils; import com.fdkankan.model.constants.ConstantFilePath; 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.oss.OssUtil; 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.apache.commons.lang3.StringUtils; 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 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.*; import java.util.List; @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 OssUtil ossUtil; @Autowired private IScenePlusService scenePlusService; @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)){ 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"; ossUtil.uploadFile(targetOssImgPath, imgPath, false); 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 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); } @Override public void delete(String sceneNum,Long userId) { if(StringUtils.isEmpty(sceneNum)){ throw new BusinessException(ErrorCode.FAILURE_CODE_3001); } String[] nums = sceneNum.split(","); List numList = Arrays.asList(nums); List plusList = scenePlusService.getListByNums(numList); scenePlusService.deleteByList(plusList,userId); } @Override public void copyScene(String sceneNum,String userName) throws Exception { if(StringUtils.isEmpty(sceneNum)){ throw new BusinessException(ErrorCode.FAILURE_CODE_3001); } ScenePlus scenePlus = scenePlusService.getScenePlusByNum(sceneNum); if(scenePlus== null){ throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005); } String newNum = scene3dNumService.generateSceneNum(detailEntity.getType()); Long sceneId = scenePlus.getId(); Long newSceneId = null; if(scenePlus != null){ //v4场景复制 log.info("场景复制--V4--OldNum:{},oldTitle:{},newNum:{}", scenePlus.getNum(),scenePlus.getTitle(),newNum); newSceneId = scenePlusService.copyV4Scene(scenePlus,newNum,detailEntity); } log.info("场景复制--完成--sceneId:{}",newSceneId); } }