Bläddra i källkod

调整照片的分辨率的判断

houweiyu 4 år sedan
förälder
incheckning
7a38d2c06f

+ 23 - 19
fdkanfang-common/src/main/java/com/fdkanfang/common/enums/ImageResolutionRate.java

@@ -9,28 +9,38 @@ import org.apache.commons.lang3.StringUtils;
  * @desciption
  */
 public enum ImageResolutionRate {
-      TWO_K(2048,"2K分辨率" , 1),
-      FOUR_K(4096,"4K分辨率" , 2),
+      //最长的边不超过了8K=8192
+      ONE_K(0 ,8192,"1K分辨率" , 1),
+      //最长的边像素点不超过16K=16384,但是超过了4K=8192
+      TWO_K(8192 ,16384,"2K分辨率" , 2),
+      //最长的边像素点超过16K=16384
+      FOUR_K(16384 , 999999999,"4K分辨率" , 3),
     ;
 
-    private Integer rate;
+    private Integer rateStart;
+    private Integer rateEnd;
     private  String desc;
     private Integer order;
 
-    ImageResolutionRate(Integer rate, String desc, Integer order) {
-        this.rate = rate;
+    ImageResolutionRate(Integer rateStart, Integer rateEnd, String desc, Integer order) {
+        this.rateStart = rateStart;
+        this.rateEnd = rateEnd;
         this.desc = desc;
         this.order = order;
     }
+
     public static ImageResolutionRate getResolutionRateByRate(Integer rate){
         if(null == rate){
             return  null;
         }
-        if(rate.compareTo(4096) < 0){
+        if(rate.compareTo(ONE_K.getRateStart()) >= 0 && rate.compareTo(ONE_K.getRateEnd()) < 0){
+            return ONE_K;
+        }else if(rate.compareTo(TWO_K.getRateStart()) >= 0 && rate.compareTo(TWO_K.getRateEnd()) < 0){
             return TWO_K;
-        }else{
+        }else if(rate.compareTo(FOUR_K.getRateStart()) >= 0 && rate.compareTo(FOUR_K.getRateEnd()) < 0){
             return FOUR_K;
         }
+        return null;
     }
 
     public static ImageResolutionRate getResolutionByName(String name){
@@ -45,19 +55,12 @@ public enum ImageResolutionRate {
         return null;
     }
 
-    public static Integer getSortByRate(Integer rate){
-        if(null == rate){
-            return  -1;
-        }
-        if(rate.compareTo(2048*1024) <= 0){
-            return TWO_K.getOrder();
-        }else{
-            return FOUR_K.getOrder();
-        }
-    }
 
-    public Integer getRate() {
-        return rate;
+    public Integer getRateStart() {
+        return rateStart;
+    }
+    public Integer getRateEnd() {
+        return rateEnd;
     }
 
     public String getDesc() {
@@ -67,4 +70,5 @@ public enum ImageResolutionRate {
     public Integer getOrder() {
         return order;
     }
+
 }

+ 18 - 73
fdkanfang-web/src/main/java/com/fdkanfang/web/backend/HouseController.java

@@ -282,10 +282,13 @@ public class HouseController extends BaseController {
                 }
                 ImageResolutionRate maxResolutionRate = ImageResolutionRate.getResolutionRateByRate(totalResolutinRate);
                 log.info("照片{}的像素为:{}", newFilePath, totalResolutinRate);
+                if(null == maxResolutionRate){
+                    throw new CommonBaseException(ResultCodeEnum.D101 , "照片分辨率超过识别范围");
+                }
                 if (null == maxTotalResolutionRate) {
                     maxTotalResolutionRate = maxResolutionRate;
                 } else {
-                    if (null != maxResolutionRate && maxResolutionRate.getRate() > maxTotalResolutionRate.getRate()) {
+                    if (null != maxResolutionRate && maxResolutionRate.getOrder() > maxTotalResolutionRate.getOrder()) {
                         maxTotalResolutionRate = maxResolutionRate;
                     }
                 }
@@ -393,9 +396,12 @@ public class HouseController extends BaseController {
         if (ImageResolutionRate.TWO_K.name().equals(maxResolutionRate)) {
             //2k
             sceneProEntity.setSceneScheme(11);
-        } else {
+        }else if(ImageResolutionRate.FOUR_K.name().equals(maxResolutionRate)){
             //4k
             sceneProEntity.setSceneScheme(10);
+        }else{
+            //1k
+            sceneProEntity.setSceneScheme(12);
         }
         sceneProEntity.setNum(house.getSceneCode());
         //TODO:不知道为何数据库设置的默认不生效
@@ -514,72 +520,6 @@ public class HouseController extends BaseController {
     }
 
 
-    @Transactional(rollbackFor = Exception.class)
-//    public ImageResolutionRate addAndGetResolutionRate(MultipartFile file , String filename,String directoryName ,
-    public Map<String, Object> addAndGetResolutionRate(CommonsMultipartFile file, String filename,
-                                                       String directoryName,
-                                                       String ossVerticalHighPath,
-                                                       Long houseId) throws IOException {
-        if (null == file || null == houseId) {
-            log.error("房源id或者文件为空,无法生成image记录");
-            return new HashMap<>();
-        }
-        File dir = new File(directoryName);
-        if (!dir.exists()) {
-            dir.mkdirs();
-        }
-        // 本地图片保存位置: /root/data/kanfang/场景码/input_img/xxxx.jpg
-        String newFileName = generalRandFileName(filename);
-        log.info("修改后的文件名称为:{}", newFileName);
-        String fileFullPath = directoryName + newFileName;
-        String oldFileFullPath = directoryName + filename;
-        ossVerticalHighPath = ossVerticalHighPath + newFileName;
-        ImageEntity image = new ImageEntity();
-        image.setVerticalPath(ossVerticalHighPath);
-        image.setFileName(newFileName);
-        // 这个参数给前端用,只要有文件名就可以了
-        image.setPath(fileFullPath);
-        image.setLocalPath(fileFullPath);
-
-        image.setHouseId(houseId);
-        // 默认所有图片都是一楼
-        image.setFloor(1);
-//        image.setType(imageType);
-        //将图片保存到本地指定目录filePath eg: /root/data/kanfang/d_9iRDUgn3l/input_img/xxx.jpg
-        saveFileToLocal(file, fileFullPath);
-        boolean delResult = FileUtils.deleteFile(oldFileFullPath);
-        if (!delResult) {
-            log.error("删除旧文件[{}]失败", oldFileFullPath);
-        }
-        File localFile = new File(fileFullPath);
-        //生成缩略图,并上传到oss
-        String scalImageFullPath = directoryName + "scal_" + newFileName;
-        Thumbnails.of(localFile).size(128, 256).outputQuality(0.8f).toFile(scalImageFullPath);
-        String imageOssPath = ossPath + "scal_" + newFileName;
-        log.info("生成的照片上传oss的路径:{}", imageOssPath);
-        ossCheckPointUploadUtil.upload2(scalImageFullPath, imageOssPath);
-        String imageTotalOssUrl = ossQueryUrl + imageOssPath;
-        //删除缩略图的本地文件
-        boolean delScalResult = FileUtils.deleteFile(scalImageFullPath);
-        if (!delScalResult) {
-            log.error("删除本地缩略图[{}]失败", scalImageFullPath);
-        }
-        //照片分辨率
-        int totalResolutinRate = getImageResolutinoRate(localFile);
-        ImageResolutionRate maxResolutionRate = ImageResolutionRate.getResolutionRateByRate(totalResolutinRate);
-        log.info("照片{}的像素为:{}", fileFullPath, totalResolutinRate);
-        image.setResolutionRate(null != maxResolutionRate ? maxResolutionRate.name() : "TWO_K");
-        int insert = imageService2.save(image);
-        if (insert != 1) {
-            throw new CommonBaseException(ResultCodeEnum.D101, "新增image记录失败");
-        }
-        Map<String, Object> result = new HashMap<>();
-        result.put("rate", maxResolutionRate.name());
-        result.put("fileName", newFileName);
-        result.put("ossUrl", imageTotalOssUrl);
-        return result;
-    }
-
     private void addNewImage(long houseId,
                              String imageFileName, String imageLocalPath,
                              String ossVerticalHighPath,
@@ -598,7 +538,7 @@ public class HouseController extends BaseController {
         // 默认所有图片都是一楼
         image.setFloor(1);
 
-        image.setResolutionRate(null != maxResolutionRate ? maxResolutionRate.name() : "TWO_K");
+        image.setResolutionRate(null != maxResolutionRate ? maxResolutionRate.name() : null);
         int insert = imageService2.save(image);
         if (insert != 1) {
             throw new CommonBaseException(ResultCodeEnum.D101, "新增image记录失败");
@@ -1097,9 +1037,14 @@ public class HouseController extends BaseController {
             if (ImageResolutionRate.TWO_K.name().equals(imageResolution)) {
                 //2 K 照片
                 dataJson.put("skybox_type", "SKYBOX_V7");
-            } else {
+            } else if(ImageResolutionRate.FOUR_K.name().equals(imageResolution)){
                 //4 k照片
                 dataJson.put("skybox_type", "SKYBOX_V6");
+            }else if(ImageResolutionRate.ONE_K.name().equals(imageResolution)){
+                //1 k照片
+                dataJson.put("skybox_type", "SKYBOX_V9");
+            }else{
+                throw new CommonBaseException(ResultCodeEnum.D101 , "照片的分辨率非法,无法决策模型类别");
             }
             dataJson.put("extras", null);
             JSONObject extrasJson = new JSONObject();
@@ -1228,8 +1173,8 @@ public class HouseController extends BaseController {
         }
         List<ImageEntity> imageEntities = imageService2.findByHouseId(houseEntity.getId());
         if (CollectionUtils.isEmpty(imageEntities)) {
-            log.error("房源[{}]下面无照片,默认返回2k的分辨率");
-            return ImageResolutionRate.TWO_K.name();
+            log.error("房源[{}]下面无照片");
+            return null;
         }
         int sort = 0;
         ImageResolutionRate maxResolution = null;
@@ -1247,7 +1192,7 @@ public class HouseController extends BaseController {
             }
         }
         log.info("房源[{}]的照片最大的分辨率为:{}", houseEntity.getId(), null != maxResolution ? maxResolution.name() : "空值");
-        return null != maxResolution ? maxResolution.name() : ImageResolutionRate.TWO_K.name();
+        return null != maxResolution ? maxResolution.name() : null;
     }