Kaynağa Gözat

v4.7.1人体分割抠图 图片旋转

dsx 2 yıl önce
ebeveyn
işleme
fede05127d

+ 3 - 2
src/main/java/com/fdkankan/scene/controller/SceneController.java

@@ -86,8 +86,9 @@ public class SceneController extends BaseController {
      * @throws Exception
      */
     @PostMapping(value = "/uploadBodySegment")
-    public ResultData uploadBodySegment(@RequestParam("file") MultipartFile file) throws Exception {
-        return sceneService.uploadBodySegment(file);
+    public ResultData uploadBodySegment(@RequestParam("file") MultipartFile file,
+                                        @RequestParam(value = "rotate", required = false) Integer rotate) throws Exception {
+        return sceneService.uploadBodySegment(file, rotate);
     }
 
     /**

+ 1 - 1
src/main/java/com/fdkankan/scene/service/ISceneService.java

@@ -7,7 +7,7 @@ import org.springframework.web.multipart.MultipartFile;
 
 public interface ISceneService {
 
-    ResultData uploadBodySegment(MultipartFile file) throws Exception;
+    ResultData uploadBodySegment(MultipartFile file,Integer rotate) throws Exception;
 
     void bodySegmentHandler(String imgUrl, String uuid);
 

+ 18 - 2
src/main/java/com/fdkankan/scene/service/impl/SceneServiceImpl.java

@@ -27,8 +27,11 @@ 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;
@@ -58,7 +61,7 @@ public class SceneServiceImpl implements ISceneService {
     public FYunFileConfig fYunFileConfig;
 
     @Override
-    public ResultData uploadBodySegment(MultipartFile file) throws Exception {
+    public ResultData uploadBodySegment(MultipartFile file, Integer rotate) throws Exception {
 
         if(!FileUtils.checkFileSizeIsLimit(file.getSize(), 10, "M")){
             throw new BusinessException(ErrorCode.FAILURE_CODE_4003, "10M");
@@ -68,9 +71,17 @@ public class SceneServiceImpl implements ISceneService {
 
         String fileName = file.getOriginalFilename();
         String extName = fileName.substring(fileName.lastIndexOf("."));
-        File tempFile = File.createTempFile(java.util.UUID.randomUUID().toString(), extName);
+        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;
@@ -102,6 +113,11 @@ public class SceneServiceImpl implements ISceneService {
         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);