Browse Source

更新:
使用covert压缩图片

wuweihao 4 years ago
parent
commit
2a931b7dd9

+ 8 - 2
cms_pano_fcb/gis_common/src/main/java/com/gis/common/constant/CmdConstant.java

@@ -8,8 +8,14 @@ package com.gis.common.constant;
 public class CmdConstant {
 public class CmdConstant {
 
 
 
 
-    /** krpano 全景矢量切图 */
-    public final static String PANO_KRPANO = "krpanotools makepano -config=templates/vtour-multires.config ";
+//    /** krpano 全景矢量切图 */
+//    public final static String PANO_KRPANO = "krpanotools makepano -config=templates/vtour-multires.config ";
+
+    /**
+     * convert 压缩图片
+     * convert -resize 800x400 /home/720yun/test.jpg /home/720yun/aa.jpg
+     * */
+    public final static String CONVERT = "convert -resize @size @input @output";
 
 
 
 
 }
 }

+ 15 - 92
cms_pano_fcb/gis_common/src/main/java/com/gis/common/util/CmdUtils.java

@@ -1,107 +1,30 @@
 package com.gis.common.util;
 package com.gis.common.util;
 
 
-import cn.hutool.core.util.RuntimeUtil;
-import lombok.extern.log4j.Log4j2;
-import org.apache.commons.lang3.StringUtils;
+import lombok.extern.slf4j.Slf4j;
 
 
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
 
 
 /**
 /**
  * Created by owen on 2021/1/4 0004 14:53
  * Created by owen on 2021/1/4 0004 14:53
  */
  */
-@Log4j2
+@Slf4j
 public class CmdUtils {
 public class CmdUtils {
 
 
-
-
     /**
     /**
-     * 调用opencv 生成缩略图
-     * 100m的图java处理不了。目前使用算法部郭晨露的脚本
-     * @param cmd
-     * @return
-     * @throws IOException
-     * @throws InterruptedException
+     * 调用算法 xx.sh 脚本
+     * @param command
      */
      */
-    public static int cmdThumb(String cmd) throws IOException, InterruptedException {
-        Process exec = RuntimeUtil.exec(cmd);
-        log.info("run cmdThumb");
-
-        BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
-        BufferedReader errorBuf = new BufferedReader(new InputStreamReader(exec.getErrorStream()));
-
-        String errorLine;
-        while ((errorLine = errorBuf.readLine()) != null) {
-            log.error("errorLine: {}", errorLine);
-        }
-
-        // success ,没有获取到信息
-        String line;
-        int i = 1;
-        while ((line = br.readLine()) != null) {
-
-            // 查看执行日志
-            if (i % 100 == 0) {
-                log.info("line, i=" + i +", " +line);
-            }
-
-            if (line.contains("done.")) {
-                log.info("line: {}", line);
-                log.info("cmdThumb完成 ");
-            }
-            i++;
+    public static void callShell(String command){
+        log.info("cmd: {}", command);
+        try {
+            Process process = Runtime.getRuntime().exec(command);
+            StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");
+            errorGobbler.start();
+            StreamGobbler outGobbler = new StreamGobbler(process.getInputStream(), "STDOUT");
+            outGobbler.start();
+            process.waitFor();
+        } catch (Exception e) {
+            e.printStackTrace();
         }
         }
-        log.info("执行总行数:" +i);
-
-        // 结束命令行
-        int isCmd = exec.waitFor();
-
-        // 关闭流
-        br.close();
-        errorBuf.close();
-
-        return isCmd;
-
     }
     }
 
 
-    /**
-     * 命令运行结果 1:失败, 0:成功
-     * @param cmd
-     * @return
-     * @throws IOException
-     * @throws InterruptedException
-     */
-    public static int cmdPano(String cmd) throws IOException, InterruptedException {
-        Process exec = RuntimeUtil.exec(cmd);
-        log.info("run cmdPano");
-
-        BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
-        BufferedReader errorBuf = new BufferedReader(new InputStreamReader(exec.getErrorStream()));
-
-        String errorLine;
-        while ((errorLine = errorBuf.readLine()) != null) {
-            log.error("errorLine: {}", errorLine);
-        }
-
-        // success ,没有获取到信息
-        String line;
-        while ((line = br.readLine()) != null) {
-            log.debug("line: {}", line);
-            if (line.contains("done.")) {
-                log.info("line: {}", line);
-                log.info("cmdPano完成 ");
-            }
-        }
-
-        // 结束命令行
-        int isCmd = exec.waitFor();
-
-        // 关闭流
-        br.close();
-        errorBuf.close();
-
-        return isCmd;
-
-    }
 }
 }

+ 5 - 33
cms_pano_fcb/gis_common/src/main/java/com/gis/common/util/FileUtils.java

@@ -299,39 +299,7 @@ public class FileUtils {
         return  "fcb_" +sceneCode;
         return  "fcb_" +sceneCode;
     }
     }
 
 
-    /**
-     * 使用opencv 生成缩略图
-     * 压缩图片
-     * 需要图片比例是2:1,否则图片压出来会变形
-     */
-    public  String compressImgAndUploadOss(String inputFilePath, String ossBasePath, String ossDomain) throws IOException, InterruptedException {
-        String serverBasePath = StringUtils.substringBeforeLast(inputFilePath, "/");
-
-        String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
-        String suffix = StringUtils.substringAfterLast(inputFilePath, ".");
-        String fileName ="/thumb_"+ time  + "."  +suffix;
-
-        // 保存图片位置
-        String saveCompressImgPath = serverBasePath + fileName;
-
-//        Img.from(new File(inputFilePath)).scale(300, 150).write(new File(saveCompressImgPath));
-        // ./DAGE_Downsample 100m.jpg out.jpg 300 150
-        String cmd = "/home/720yun/100m/DAGE_Downsample " + inputFilePath + " " +  saveCompressImgPath + " 300 150";
-        log.info("cmd: " + cmd);
-        long start = System.currentTimeMillis();
-        CmdUtils.cmdThumb(cmd);
-        long end = System.currentTimeMillis();
-        log.info("openCV耗时: " + (end-start)/1000 + " s");
-        log.info("图片压缩成功: " + saveCompressImgPath);
 
 
-        // 上传oss
-        String ossPath = ossBasePath + "image/" + fileName;
-        log.info("ossPath: " + ossPath);
-        asyncTask.uploadOss(saveCompressImgPath, ossPath);
-        String ossUrl = ossDomain + ossPath;
-        log.info("图片上传成功: " + ossUrl);
-        return ossUrl;
-    }
 
 
 
 
     /**
     /**
@@ -553,11 +521,14 @@ public class FileUtils {
     }
     }
 
 
     /**
     /**
+     * 2021-04-28
+     * 这个可能比较耗性能
      * 检查出图比较是否2:1
      * 检查出图比较是否2:1
      * @return
      * @return
      * @throws IOException
      * @throws IOException
      */
      */
     public static Boolean checkScale(InputStream input) throws IOException {
     public static Boolean checkScale(InputStream input) throws IOException {
+//        ImageIO.setUseCache(false);
         BufferedImage image = ImageIO.read(input);
         BufferedImage image = ImageIO.read(input);
         int srcWidth = image .getWidth();      // 源图宽度
         int srcWidth = image .getWidth();      // 源图宽度
         int srcHeight = image .getHeight();    // 源图高度
         int srcHeight = image .getHeight();    // 源图高度
@@ -575,7 +546,8 @@ public class FileUtils {
 
 
     @Test
     @Test
     public void testScale() throws IOException {
     public void testScale() throws IOException {
-        String path = "C:\\Users\\Administrator\\Desktop\\33\\28\\2.jpg";
+        String path = "C:\\Users\\Administrator\\Desktop\\33\\test.jpg";
+        ImageIO.setUseCache(false);
         BufferedImage image = ImageIO.read(new File(path));
         BufferedImage image = ImageIO.read(new File(path));
         int srcWidth = image .getWidth();      // 源图宽度
         int srcWidth = image .getWidth();      // 源图宽度
         int srcHeight = image .getHeight();    // 源图高度
         int srcHeight = image .getHeight();    // 源图高度

+ 61 - 0
cms_pano_fcb/gis_common/src/main/java/com/gis/common/util/StreamGobbler.java

@@ -0,0 +1,61 @@
+package com.gis.common.util;
+
+import java.io.*;
+
+public class StreamGobbler extends Thread {
+
+	InputStream is;  
+    String type;  
+    OutputStream os;  
+
+    public StreamGobbler(InputStream is, String type) {  
+        this(is, type, null);  
+    }  
+
+    StreamGobbler(InputStream is, String type, OutputStream redirect) {  
+        this.is = is;  
+        this.type = type;  
+        this.os = redirect;  
+    }  
+
+    public void run() {  
+        InputStreamReader isr = null;  
+        BufferedReader br = null;  
+        PrintWriter pw = null;  
+        try {  
+            if (os != null)  
+                pw = new PrintWriter(os);  
+
+            isr = new InputStreamReader(is);  
+            br = new BufferedReader(isr);  
+            String line=null;  
+            while ( (line = br.readLine()) != null) {  
+                if (pw != null)  
+                    pw.println(line);  
+                System.out.println(type + ">" + line);
+            }  
+
+            if (pw != null)  
+                pw.flush();  
+        } catch (IOException ioe) {  
+            ioe.printStackTrace();    
+        } finally{  
+            try {  
+            	if(pw!=null)
+            	{
+            		 pw.close();  
+            	}
+            	if(br!=null)
+            	{
+            		br.close();  
+            	}
+            	if(isr!=null)
+            	{
+            		isr.close();  
+            	}
+            } catch (IOException e) {  
+                e.printStackTrace();  
+            }  
+        }  
+    }  
+}

+ 55 - 4
cms_pano_fcb/gis_service/src/main/java/com/gis/service/impl/SceneServiceImpl.java

@@ -1,9 +1,11 @@
 package com.gis.service.impl;
 package com.gis.service.impl;
 
 
+import cn.hutool.core.img.Img;
 import cn.hutool.core.io.FileTypeUtil;
 import cn.hutool.core.io.FileTypeUtil;
 import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.io.FileUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.JSONObject;
+import com.gis.common.constant.CmdConstant;
 import com.gis.common.constant.ConfigConstant;
 import com.gis.common.constant.ConfigConstant;
 import com.gis.common.constant.RabbitConfig;
 import com.gis.common.constant.RabbitConfig;
 import com.gis.common.task.AsyncTask;
 import com.gis.common.task.AsyncTask;
@@ -114,19 +116,18 @@ public class SceneServiceImpl extends IBaseServiceImpl<SceneEntity, String> impl
         SceneEntity entity = new SceneEntity();
         SceneEntity entity = new SceneEntity();
 
 
 
 
-        InputStream inputStream = null;
         String basePath = null;
         String basePath = null;
 
 
         try {
         try {
-            inputStream = file.getInputStream();
 
 
 
 
-            String type = FileTypeUtil.getType(inputStream);
+            String type = FileTypeUtil.getType(file.getInputStream());
             if (!FileUtils.getType(type).equals("image")) {
             if (!FileUtils.getType(type).equals("image")) {
                 log.error("非图片类型");
                 log.error("非图片类型");
                 return Result.failure(7001, "非图片类型");
                 return Result.failure(7001, "非图片类型");
             }
             }
 
 
+            // todo 下次发版,建议前端判断图片大小比例,后端耗性能,不处理
             if (!FileUtils.checkScale(file.getInputStream())) {
             if (!FileUtils.checkScale(file.getInputStream())) {
                 log.error("图片比例非2:1");
                 log.error("图片比例非2:1");
                 return Result.failure(7007, "图片比例非2:1");
                 return Result.failure(7007, "图片比例非2:1");
@@ -164,7 +165,10 @@ public class SceneServiceImpl extends IBaseServiceImpl<SceneEntity, String> impl
             // 缩略图,使用切图那个thumb.jpg
             // 缩略图,使用切图那个thumb.jpg
             // 压缩图片并上传oss
             // 压缩图片并上传oss
             // 全景图缩略图统一命名规则: http:// oss/cms_pano_fcb/image/thumb_sceneCode.jpg
             // 全景图缩略图统一命名规则: http:// oss/cms_pano_fcb/image/thumb_sceneCode.jpg
-            String iconPath = fileUtils.compressImgAndUploadOss2(
+//            String iconPath = fileUtils.compressImgAndUploadOss2(
+//                    panoPath, configConstant.filePath, configConstant.ossBasePath, configConstant.ossDomain, 600, 300, "image/thumb_"+sceneCode+".jpg");
+
+            String iconPath = convertAndUploadOss(
                     panoPath, configConstant.filePath, configConstant.ossBasePath, configConstant.ossDomain, 600, 300, "image/thumb_"+sceneCode+".jpg");
                     panoPath, configConstant.filePath, configConstant.ossBasePath, configConstant.ossDomain, 600, 300, "image/thumb_"+sceneCode+".jpg");
             log.info("iconPath:" + iconPath);
             log.info("iconPath:" + iconPath);
             entity.setIcon(iconPath);
             entity.setIcon(iconPath);
@@ -196,6 +200,53 @@ public class SceneServiceImpl extends IBaseServiceImpl<SceneEntity, String> impl
     }
     }
 
 
 
 
+    /**
+     * 2021-04-27
+     * 使用convert 生成缩略图
+     * @param inputFilePath
+     * @param ossBasePath
+     * @param ossDomain
+     * @return
+     */
+    public  String convertAndUploadOss(
+            String inputFilePath, String serverBasePath, String ossBasePath, String ossDomain, Integer width, Integer height, String fileName){
+
+        // 保存图片位置
+        String saveCompressImgPath = serverBasePath + fileName;
+        log.info("saveCompressImgPath: " + saveCompressImgPath);
+        String ossUrl = null;
+        try {
+//            Img.from(new File(inputFilePath)).scale(width, height).write(new File(saveCompressImgPath));
+            // 使用convert压缩图片
+            String cmd = CmdConstant.CONVERT;
+            String size = width + "x" + height;
+            cmd = cmd.replace("@size", size);
+            cmd = cmd.replace("@input", inputFilePath);
+            cmd = cmd.replace("@output", saveCompressImgPath);
+            CmdUtils.callShell(cmd);
+            log.info("图片压缩成功: " + saveCompressImgPath);
+
+            if (FileUtil.isFile(saveCompressImgPath)) {
+                // 上传oss
+                String ossPath = ossBasePath  + fileName;
+                log.info("ossPath: " + ossPath);
+                asyncTask.uploadOss(saveCompressImgPath, ossPath);
+                ossUrl = ossDomain + ossPath;
+                log.info("图片上传成功: " + ossUrl);
+            } else {
+                log.error("缩略图不存在: " + saveCompressImgPath);
+            }
+
+        } catch (Exception e) {
+            log.error("图片格式有误,不支持此图片");
+            e.printStackTrace();
+        }
+
+
+        return ossUrl;
+    }
+
+
 
 
     /**
     /**
      * 查询VR项目状态
      * 查询VR项目状态

+ 9 - 0
cms_pano_fcb/remark.md

@@ -189,6 +189,15 @@ sit:
     5. 预览是H5打开的首页(H5调用免token的list接口去匹配那个是初始场景,H5可以获取很多数据)
     5. 预览是H5打开的首页(H5调用免token的list接口去匹配那个是初始场景,H5可以获取很多数据)
     6. 先保存VR项目,才能添加场景
     6. 先保存VR项目,才能添加场景
     7. 
     7. 
+    
+# deploy必备工具
+1. jdk1.8
+2. mysql
+3. redis
+4. ossUtil
+5. krpano 切全景图
+6. convert 切缩略图
+7. rabbit MQ    
 
 
 # sit 更新日志
 # sit 更新日志
     20210322 大版本更新
     20210322 大版本更新