Pārlūkot izejas kodu

预览图在队列执行

wuweihao 3 gadi atpakaļ
vecāks
revīzija
876a667f72

+ 6 - 0
720yun_local_consumer/src/main/java/com/gis/constant/CmdConstant.java

@@ -32,4 +32,10 @@ public class CmdConstant {
 //    public final static String OSSUTIL_UPLOAD_DIR = "bash /opt/ossutil/ossupload.sh /mnt/720yun_fd_manage_data/@sceneCode 720yun_fd_manage/@sceneCode";
     public final static String OSSUTIL_UPLOAD_DIR = "bash /root/user/java/720yun_fd_consumer_8002/ossupload.sh /mnt/720yun_fd_manage_data/@sceneCode 720yun_fd_manage/@sceneCode";
 
+
+    /**
+     * convert 压缩图片
+     * convert -resize 800x400 /root/user/owen_test/test.jpg /root/user/owen_test/aa.jpg
+     * */
+    public final static String CONVERT = "convert -resize @size @input @output";
 }

+ 7 - 0
720yun_local_consumer/src/main/java/com/gis/listener/Local720Listener.java

@@ -7,6 +7,7 @@ import com.gis.constant.RabbitConfig;
 import com.gis.entity.FodderEntity;
 import com.gis.mapper.FodderMapper;
 import com.gis.util.CmdUtils;
+import com.gis.util.ImgUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.Test;
 import org.slf4j.MDC;
@@ -84,6 +85,12 @@ public class Local720Listener {
             @Override
             public String call() throws Exception {
                 MDC.put("TRACE_ID", traceId);
+
+                //生成预览图、固定命名
+                ImgUtil.compressImg(panoPath, "/preview.jpg", 1920, 960);
+                //生成缩略图
+                ImgUtil.compressImg(panoPath, "/thumb.jpg", 100, 100);
+                //切图
                 CmdUtils.callLine(cmd, 200);
                 long end = System.currentTimeMillis();
                 log.info("切图完成耗时: {} s" ,(end-start)/1000);

+ 94 - 0
720yun_local_consumer/src/main/java/com/gis/util/BaseRuntimeException.java

@@ -0,0 +1,94 @@
+package com.gis.util;
+
+import cn.hutool.core.util.StrUtil;
+import org.springframework.util.CollectionUtils;
+
+import java.util.List;
+
+public class BaseRuntimeException extends RuntimeException{
+
+    private static final long serialVersionUID = -1518945670203783450L;
+    private Integer code;
+    private String msg;
+
+    public BaseRuntimeException(String msg){
+        super(msg);
+        this.code = -1;
+        this.msg = msg;
+    }
+
+    /**
+     *
+     * @param code 允许为null
+     * @param msg
+     */
+    public BaseRuntimeException(Integer code, String msg){
+        super(msg);
+        this.code = code == null? -1 : code;
+        this.msg = msg;
+    }
+
+
+
+    public Integer getCode() {
+        return code;
+    }
+
+    public void setCode(Integer code) {
+        this.code = code;
+    }
+
+    public String getMsg() {
+        return msg;
+    }
+
+    public void setMsg(String msg) {
+        this.msg = msg;
+    }
+
+
+    public static void isNull(Object obj, Integer code, String msg){
+        if (obj == null){
+            getExc(code, msg);
+        }
+    }
+
+    public static void isBlank(Object obj, Integer code, String msg){
+        if (obj == null){
+            getExc(code, msg);
+        }
+
+        if (obj instanceof String && StrUtil.isBlank(obj.toString())){
+            getExc(code, msg);
+        }
+
+    }
+
+
+
+
+    /**
+     *
+     * @param obj 存在抛异常
+     * @param code 允许为null
+     * @param msg
+     */
+    public static void isHas(boolean obj, Integer code, String msg){
+        if (obj){
+            getExc(code, msg);
+        }
+    }
+
+    public static void  getExc(Integer code, String msg){
+        throw new BaseRuntimeException(code, msg);
+    }
+
+
+
+    public static void isEmpty(List obj, Integer code, String msg){
+        if (CollectionUtils.isEmpty(obj)){
+            getExc(code, msg);
+        }
+    }
+
+}

+ 15 - 16
720yun_local_consumer/src/main/java/com/gis/util/CmdUtils.java

@@ -85,25 +85,24 @@ public class CmdUtils {
     }
 
 
+
     /**
-     * 调用算法 xx.sh 脚本
+     * 直接运行命令
      * @param command
      */
-//    public static void callPano(String command){
-//        log.info("cmd: " + command);
-//        try {
-//            Process process = Runtime.getRuntime().exec(command);
-//            StreamGobblerLine errorGobbler = new StreamGobblerLine(process.getErrorStream(), "ERROR");
-//            errorGobbler.start();
-//            // 200行打印一次日志
-//            StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT", 200);
-//            outGobbler.start();
-//            process.waitFor();
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//        }
-//    }
-
+    public static void callCmd(String command){
+        log.info("cmd: {}", command);
+        try {
+            Process process = Runtime.getRuntime().exec(command);
+            StreamGobblerLine errorGobbler = new StreamGobblerLine(process.getErrorStream(), "ERROR");
+            errorGobbler.start();
+            StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT");
+            outGobbler.start();
+            process.waitFor();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
 
     public static void callLine(String command){
         callLine(command, null);

+ 68 - 0
720yun_local_consumer/src/main/java/com/gis/util/ImgUtil.java

@@ -0,0 +1,68 @@
+package com.gis.util;
+
+import cn.hutool.core.io.FileUtil;
+import com.gis.constant.CmdConstant;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+
+/**
+ * Created by owen on 2022/3/23 0023 17:48
+ *
+ * 图片工具类
+ */
+@Slf4j
+public class ImgUtil {
+
+    /**
+     * 202-03-27
+     * 压缩图片 使用convert 工具
+     * 固定名称: 名称固定是xxx.jpg
+     * @param inPath
+     * @param outFileName /aa/xxx.jpg
+     * @param width 宽
+     * @param height 高
+     * @return 完整的oss访问地址
+     */
+    public static void compressImg(String inPath, String outFileName, int width, int height){
+        String basePath = StringUtils.substringBeforeLast(inPath, "/");
+        // 保存图片位置
+        String saveCompressImgPath = basePath + outFileName;
+
+        // 使用convert压缩图片
+        String cmd = CmdConstant.CONVERT;
+        String size = width + "x" + height;
+        cmd = cmd.replace("@size", size);
+        cmd = cmd.replace("@input", inPath);
+        cmd = cmd.replace("@output", saveCompressImgPath);
+
+        // 开始压缩
+        CmdUtils.callCmd(cmd);
+        log.info("压缩图片保存位置: {}", saveCompressImgPath);
+        boolean file = FileUtil.isFile(saveCompressImgPath);
+        BaseRuntimeException.isHas(!file, null, "压缩图片不存在:" + saveCompressImgPath);
+        log.info("压缩图片完成");
+
+    }
+
+    /**
+     * 检查2:1图片
+     * @param file
+     */
+    public static void checkImgRatio(MultipartFile file){
+        try {
+            BufferedImage read = cn.hutool.core.img.ImgUtil.read(file.getInputStream());
+            int width = read.getWidth();
+            int height = read.getHeight();
+            BaseRuntimeException.isHas(!(width/height == 2), null, "非2:1图片");
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+    }
+
+
+}

+ 3 - 3
720yun_local_manage/gis_common/src/main/java/com/gis/common/util/ImgUtil.java

@@ -42,10 +42,10 @@ public class ImgUtil {
 
         // 开始压缩
         CmdUtils.callCmd(cmd);
-        log.info("压缩图片完成: {}", saveCompressImgPath);
-
+        log.info("压缩图片保存位置: {}", saveCompressImgPath);
         boolean file = FileUtil.isFile(saveCompressImgPath);
-        BaseRuntimeException.isHas(!file, null, "预览图不存在");
+        BaseRuntimeException.isHas(!file, null, "压缩图片不存在:" + saveCompressImgPath);
+        log.info("压缩图片完成");
 
     }
 

+ 2 - 0
720yun_local_manage/gis_pano_producer/src/main/java/com/gis/cms/service/impl/ProduceServiceImpl.java

@@ -47,6 +47,7 @@ public class ProduceServiceImpl implements ProducerService {
 
     @Override
     public Result upload(MultipartFile file, String tempId) throws IOException {
+        log.info("上传图片名称: {}", file.getOriginalFilename());
         Long userId = JwtUtil.getUserId(request.getHeader("token"));
         long start = System.currentTimeMillis();
         // 全景图只支持jpg图片格式
@@ -124,6 +125,7 @@ public class ProduceServiceImpl implements ProducerService {
             int width = read.getWidth();
             int height = read.getHeight();
             BaseRuntimeException.isHas(!(width/height == 2), null, "非2:1图片");
+            log.info("校验图片完成");
 
         } catch (IOException e) {
             e.printStackTrace();

+ 2 - 1
README.md

@@ -8,5 +8,6 @@
    login:http://project.4dage.com:8021/panorama/material.html
    
    
-   
+# 变更
+    1. 2022-05-26 预览图、100*100 图放到队列里面去, 应项目测试服务器只有4核的cup, 压缩图片执行一次会占一核