Jelajahi Sumber

使用convert工具压缩图片

wuweihao 4 tahun lalu
induk
melakukan
8aabdf0705

+ 5 - 1
720yun_fd_manage/gis_common/src/main/java/com/gis/common/constant/CmdConstant.java

@@ -13,5 +13,9 @@ public class CmdConstant {
 
 //    public final static String OSSUTIL_UPLOAD_DIR = "/opt/ossutil/ossutil64 cp -r /mnt/720yun_fd_manage_data/@sceneCode oss://oss-xiaoan/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";
 }

+ 3 - 1
720yun_fd_manage/gis_common/src/main/java/com/gis/common/util/CmdUtils.java

@@ -2,6 +2,7 @@ package com.gis.common.util;
 
 import cn.hutool.core.util.RuntimeUtil;
 import lombok.extern.log4j.Log4j2;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 
 import java.io.BufferedReader;
@@ -11,7 +12,7 @@ import java.io.InputStreamReader;
 /**
  * Created by owen on 2021/1/4 0004 14:53
  */
-@Log4j2
+@Slf4j
 public class CmdUtils {
 
 
@@ -111,6 +112,7 @@ public class CmdUtils {
      * @param command
      */
     public static void callShell(String command){
+        log.info("cmd: {}", command);
         try {
             Process process = Runtime.getRuntime().exec(command);
             StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");

+ 34 - 0
720yun_fd_manage/gis_common/src/main/java/com/gis/common/util/FileUtils.java

@@ -640,5 +640,39 @@ public class FileUtils {
     }
 
 
+    /**
+     * 2021-04-27
+     * 压缩图片 使用convert 工具
+     * 固定名称: 名称固定是xxx.jpg
+     * @param inputFilePath
+     * @param ossBasePath
+     * @param ossDomain
+     * @param width 宽
+     * @param height 高
+     * @return 完整的oss访问地址
+     */
+    public  String convertAndUploadOss(String inputFilePath, String ossBasePath, String ossDomain, Integer width, Integer height, String imgName){
+        String serverBasePath = StringUtils.substringBeforeLast(inputFilePath, "/");
+
+        // 保存图片位置
+        String saveCompressImgPath = serverBasePath + imgName;
+        String ossUrl = null;
+
+//        Img.from(new File(inputFilePath)).scale(width, height).write(new File(saveCompressImgPath));
+//        log.info("图片压缩成功: " + saveCompressImgPath);
+
+        if (FileUtil.isFile(saveCompressImgPath)) {
+            // 上传oss
+            String ossPath = ossBasePath + imgName;
+            log.info("ossPath: " + ossPath);
+            aliyunOssUtil.upload(saveCompressImgPath, ossPath);
+            ossUrl = ossDomain + ossPath;
+            log.info("图片上传成功: " + ossUrl);
+        } else {
+            log.error("缩略图不存在: " + saveCompressImgPath);
+        }
+
+        return ossUrl;
+    }
 
 }

+ 229 - 55
720yun_fd_manage/gis_service/src/main/java/com/gis/service/impl/FodderServiceImpl.java

@@ -4,6 +4,7 @@ import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.img.ImgUtil;
 import cn.hutool.core.io.FileUtil;
 import com.gis.common.config.RabbitConfig;
+import com.gis.common.constant.CmdConstant;
 import com.gis.common.constant.ConfigConstant;
 import com.gis.common.constant.MsgCode;
 import com.gis.common.exception.BaseRuntimeException;
@@ -67,8 +68,7 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
 
 
     @Override
-    public Result upload2(MultipartFile file, String type)  {
-        log.info("now time: " + new Date());
+    public Result upload(MultipartFile file, String type, String tempId)  {
 
         long start = System.currentTimeMillis();
         // 检查非法文件上传
@@ -111,29 +111,22 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
                 savePath =savePath + sceneCode + "/" + newName;
 
 
-                log.info("准备写入全景图: " +  new Date());
-                Thread.sleep(10000);
 
                 FileUtil.writeFromStream(file.getInputStream(), savePath);
 
                 log.info("pano serverPath: " + savePath);
                 log.info("全景图片写入完成");
 
-                log.info("睡眠10s: " +  new Date());
-                Thread.sleep(10000);
-
-                log.info("准备执行压缩图片: " +  new Date());
-                // 压缩图片并上传oss
-                iconPath = fileUtils.compressImgAndUploadOss2(
-                        savePath, configConstant.ossBasePath + sceneCode , configConstant.ossDomain, 600, 300, "/thumb_"+sceneCode+".jpg");
-
+                // 压缩图片并上传oss, 以后内存不够,或者内存溢出改用算法提供的切图
+//                iconPath = fileUtils.compressImgAndUploadOss2(
+//                        savePath, configConstant.ossBasePath + sceneCode , configConstant.ossDomain, 600, 300, "/thumb_"+sceneCode+".jpg");
 
-                log.info("压缩图片2: " +  new Date());
                 // 全景图的预览图
-                ossPreviewIcon = fileUtils.compressImgAndUploadOss2(
+                ossPreviewIcon = convertAndUploadOss(
                         savePath, configConstant.ossBasePath + sceneCode , configConstant.ossDomain, 1920, 960, "/preview.jpg");
 
-
+                // 使用oss截取缩略图
+                iconPath = ossPreviewIcon ;
                 ossUrl = configConstant.ossDomain+configConstant.ossBasePath + sceneCode;
 
 
@@ -147,13 +140,15 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
 
                 if (type.equals("image")) {
                     // 图片大于1MB生成缩略图, 小于1MB使用原图作为缩略图
-                    if (size > 1024) {
-                        savePath = savePath + dirType + newName;
-                        FileUtil.writeFromStream(file.getInputStream(), savePath);
-                        iconPath = fileUtils.scaleImgAndUploadOss(savePath, configConstant.ossBasePath + dirType , configConstant.ossDomain);
-                    } else {
-                        iconPath = ossUrl;
-                    }
+//                    if (size > 1024) {
+////                        savePath = savePath + dirType + newName;
+////                        FileUtil.writeFromStream(file.getInputStream(), savePath);
+////                        iconPath = fileUtils.scaleImgAndUploadOss(savePath, configConstant.ossBasePath + dirType , configConstant.ossDomain);
+//                        // 使用oss做缩略图
+//                        iconPath = ossUrl  ;
+//                    } else {
+//                    }
+                    iconPath = ossUrl;
 
                     dpi = ImageUtil.dpi(file);
 
@@ -172,18 +167,19 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
 
             entity.setOssPath(ossUrl);
             entity.setIcon(iconPath);
-            entity.setUserId("15015980188");
+            entity.setUserId(getUserNameForToken());
             entity.setFileSize(size+"");
             entity.setDpi(dpi);
             entity.setPreviewIcon(ossPreviewIcon);
+            entity.setTempId(tempId);
 
-//            save(entity);
+            save(entity);
 
-//            if (type.equals("pano")) {
-//                //发消息到mq
-//                rabbitTemplate.convertAndSend(RabbitConfig.PANO_EXCHANGE, RabbitConfig.PANO_QUEUE_ROUTING, entity.getId());
-//                log.info("发送消息到队列完成: " + entity.getId());
-//            }
+            if (type.equals("pano")) {
+                //发消息到mq
+                rabbitTemplate.convertAndSend(RabbitConfig.PANO_EXCHANGE, RabbitConfig.PANO_QUEUE_ROUTING, entity.getId());
+                log.info("发送消息到队列完成: " + entity.getId());
+            }
             long end = System.currentTimeMillis();
             log.info("上传完成,耗时: {} s" , (end-start)/1000);
         } catch (Exception e) {
@@ -198,8 +194,55 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
 
 
 
+    /**
+     * 2021-04-27
+     * 压缩图片 使用convert 工具
+     * 固定名称: 名称固定是xxx.jpg
+     * @param inputFilePath
+     * @param ossBasePath
+     * @param ossDomain
+     * @param width 宽
+     * @param height 高
+     * @return 完整的oss访问地址
+     */
+    public  String convertAndUploadOss(String inputFilePath, String ossBasePath, String ossDomain, Integer width, Integer height, String imgName){
+        String serverBasePath = StringUtils.substringBeforeLast(inputFilePath, "/");
+
+        // 保存图片位置
+        String saveCompressImgPath = serverBasePath + imgName;
+        String ossUrl = null;
+
+//        Img.from(new File(inputFilePath)).scale(width, height).write(new File(saveCompressImgPath));
+//        log.info("图片压缩成功: " + 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);
+
+
+        if (FileUtil.isFile(saveCompressImgPath)) {
+            // 上传oss
+            String ossPath = ossBasePath + imgName;
+            log.info("ossPath: " + ossPath);
+            aliyunOssUtil.upload(saveCompressImgPath, ossPath);
+            ossUrl = ossDomain + ossPath;
+            log.info("图片上传成功: " + ossUrl);
+        } else {
+            log.error("缩略图不存在: " + saveCompressImgPath);
+        }
+
+        return ossUrl;
+    }
+
+
     @Override
-    public Result upload(MultipartFile file, String type, String tempId)  {
+    public Result upload2(MultipartFile file, String type)  {
+        log.info("now time: " + new Date());
 
         long start = System.currentTimeMillis();
         // 检查非法文件上传
@@ -231,42 +274,50 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
 
         FodderEntity entity = new FodderEntity();
         try {
-        if (type.equals("pano")) {
+            if (type.equals("pano")) {
 
-            if ((size/1024) >= 120) {
-              return Result.failure(MsgCode.e3004, "全景图文件不能超过120MB");
-            }
+                if ((size/1024) >= 120) {
+                    return Result.failure(MsgCode.e3004, "全景图文件不能超过120MB");
+                }
 
-            String sceneCode = RandomUtils.getSceneCode("fd720_");
-            newName = sceneCode+"." + suffix;
-            savePath =savePath + sceneCode + "/" + newName;
+                String sceneCode = RandomUtils.getSceneCode("fd720_");
+                newName = sceneCode+"." + suffix;
+                savePath =savePath + sceneCode + "/" + newName;
 
 
+                log.info("准备写入全景图: " +  new Date());
+                Thread.sleep(10000);
 
-            FileUtil.writeFromStream(file.getInputStream(), savePath);
+                FileUtil.writeFromStream(file.getInputStream(), savePath);
 
-            log.info("pano serverPath: " + savePath);
+                log.info("pano serverPath: " + savePath);
                 log.info("全景图片写入完成");
 
-                // 压缩图片并上传oss, 以后内存不够,或者内存溢出改用算法提供的切图
+                log.info("睡眠10s: " +  new Date());
+                Thread.sleep(10000);
+
+                log.info("准备执行压缩图片: " +  new Date());
+                // 压缩图片并上传oss
                 iconPath = fileUtils.compressImgAndUploadOss2(
                         savePath, configConstant.ossBasePath + sceneCode , configConstant.ossDomain, 600, 300, "/thumb_"+sceneCode+".jpg");
 
+
+                log.info("压缩图片2: " +  new Date());
                 // 全景图的预览图
                 ossPreviewIcon = fileUtils.compressImgAndUploadOss2(
-                       savePath, configConstant.ossBasePath + sceneCode , configConstant.ossDomain, 1920, 960, "/preview.jpg");
+                        savePath, configConstant.ossBasePath + sceneCode , configConstant.ossDomain, 1920, 960, "/preview.jpg");
 
 
                 ossUrl = configConstant.ossDomain+configConstant.ossBasePath + sceneCode;
 
 
 
-            entity.setSceneCode(sceneCode);
-            entity.setFilePath(savePath);
-            entity.setStatus(1);
+                entity.setSceneCode(sceneCode);
+                entity.setFilePath(savePath);
+                entity.setStatus(1);
 
-        } else {
-            ossUrl = configConstant.ossDomain+ossPath;
+            } else {
+                ossUrl = configConstant.ossDomain+ossPath;
 
                 if (type.equals("image")) {
                     // 图片大于1MB生成缩略图, 小于1MB使用原图作为缩略图
@@ -285,7 +336,7 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
                 aliyunOssUtil.upload(file.getBytes(), ossPath);
 
 
-        }
+            }
             entity.setType(type);
             // 用前缀做名称
             entity.setName(prefix);
@@ -295,19 +346,18 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
 
             entity.setOssPath(ossUrl);
             entity.setIcon(iconPath);
-            entity.setUserId(getUserNameForToken());
+            entity.setUserId("15015980188");
             entity.setFileSize(size+"");
             entity.setDpi(dpi);
             entity.setPreviewIcon(ossPreviewIcon);
-            entity.setTempId(tempId);
 
-            save(entity);
+//            save(entity);
 
-            if (type.equals("pano")) {
-            //发消息到mq
-            rabbitTemplate.convertAndSend(RabbitConfig.PANO_EXCHANGE, RabbitConfig.PANO_QUEUE_ROUTING, entity.getId());
-            log.info("发送消息到队列完成: " + entity.getId());
-        }
+//            if (type.equals("pano")) {
+//                //发消息到mq
+//                rabbitTemplate.convertAndSend(RabbitConfig.PANO_EXCHANGE, RabbitConfig.PANO_QUEUE_ROUTING, entity.getId());
+//                log.info("发送消息到队列完成: " + entity.getId());
+//            }
             long end = System.currentTimeMillis();
             log.info("上传完成,耗时: {} s" , (end-start)/1000);
         } catch (Exception e) {
@@ -322,6 +372,130 @@ public class FodderServiceImpl extends IBaseServiceImpl<FodderEntity, Long> impl
 
 
 
+//    @Override
+//    public Result upload(MultipartFile file, String type, String tempId)  {
+//
+//        long start = System.currentTimeMillis();
+//        // 检查非法文件上传
+//        boolean checkFile = FileUtils.checkFile(file);
+//        if (!checkFile) {
+//            return Result.failure("上传文件格式有误, 请重新上传");
+//        }
+//
+//        String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
+//        String fileName = file.getOriginalFilename();
+//        // 文件名校验长度
+//        String prefix = StringUtils.substringBeforeLast(fileName, ".");
+//        if (prefix.length() > 50) {
+//            return Result.failure(3003, "文件名称不允许超过50个字节");
+//        }
+//        String suffix = StringUtils.substringAfterLast(fileName, ".");
+//        String newName = time  + "."  +suffix;
+//        String dirType = "fodder/";
+//        long size = file.getSize();
+//        size = size/1024;
+//        log.info("fileSize: " + size);
+//
+//        String ossUrl = null;
+//        String iconPath = "0";
+//        String dpi = "0";
+//        String ossPath = configConstant.ossBasePath+dirType+newName;
+//        String savePath = configConstant.serverBasePath;
+//        String ossPreviewIcon = null;
+//
+//        FodderEntity entity = new FodderEntity();
+//        try {
+//        if (type.equals("pano")) {
+//
+//            if ((size/1024) >= 120) {
+//              return Result.failure(MsgCode.e3004, "全景图文件不能超过120MB");
+//            }
+//
+//            String sceneCode = RandomUtils.getSceneCode("fd720_");
+//            newName = sceneCode+"." + suffix;
+//            savePath =savePath + sceneCode + "/" + newName;
+//
+//
+//
+//            FileUtil.writeFromStream(file.getInputStream(), savePath);
+//
+//            log.info("pano serverPath: " + savePath);
+//                log.info("全景图片写入完成");
+//
+//                // 压缩图片并上传oss, 以后内存不够,或者内存溢出改用算法提供的切图
+//                iconPath = fileUtils.compressImgAndUploadOss2(
+//                        savePath, configConstant.ossBasePath + sceneCode , configConstant.ossDomain, 600, 300, "/thumb_"+sceneCode+".jpg");
+//
+//                // 全景图的预览图
+//                ossPreviewIcon = fileUtils.compressImgAndUploadOss2(
+//                       savePath, configConstant.ossBasePath + sceneCode , configConstant.ossDomain, 1920, 960, "/preview.jpg");
+//
+//
+//                ossUrl = configConstant.ossDomain+configConstant.ossBasePath + sceneCode;
+//
+//
+//
+//            entity.setSceneCode(sceneCode);
+//            entity.setFilePath(savePath);
+//            entity.setStatus(1);
+//
+//        } else {
+//            ossUrl = configConstant.ossDomain+ossPath;
+//
+//                if (type.equals("image")) {
+//                    // 图片大于1MB生成缩略图, 小于1MB使用原图作为缩略图
+//                    if (size > 1024) {
+//                        savePath = savePath + dirType + newName;
+//                        FileUtil.writeFromStream(file.getInputStream(), savePath);
+//                        iconPath = fileUtils.scaleImgAndUploadOss(savePath, configConstant.ossBasePath + dirType , configConstant.ossDomain);
+//                    } else {
+//                        iconPath = ossUrl;
+//                    }
+//
+//                    dpi = ImageUtil.dpi(file);
+//
+//                }
+//                // 普通素材直接上传oss, 服务器不保留文件
+//                aliyunOssUtil.upload(file.getBytes(), ossPath);
+//
+//
+//        }
+//            entity.setType(type);
+//            // 用前缀做名称
+//            entity.setName(prefix);
+//            entity.setFileName(newName);
+//            log.info("ossUrl: " + ossUrl);
+//            log.info("iconPath:" + iconPath);
+//
+//            entity.setOssPath(ossUrl);
+//            entity.setIcon(iconPath);
+//            entity.setUserId(getUserNameForToken());
+//            entity.setFileSize(size+"");
+//            entity.setDpi(dpi);
+//            entity.setPreviewIcon(ossPreviewIcon);
+//            entity.setTempId(tempId);
+//
+//            save(entity);
+//
+//            if (type.equals("pano")) {
+//            //发消息到mq
+//            rabbitTemplate.convertAndSend(RabbitConfig.PANO_EXCHANGE, RabbitConfig.PANO_QUEUE_ROUTING, entity.getId());
+//            log.info("发送消息到队列完成: " + entity.getId());
+//        }
+//            long end = System.currentTimeMillis();
+//            log.info("上传完成,耗时: {} s" , (end-start)/1000);
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//            log.error(e.getMessage());
+//            throw new BaseRuntimeException(MsgCode.e5003, "不支持此图片");
+//        }
+//
+//
+//        return Result.success(entity);
+//    }
+
+
+
     @Override
     public Result search(FodderPageDto param) {
         startPage(param);

+ 0 - 6
720yun_fd_manage/gis_web/src/main/java/com/gis/web/controller/FodderController.java

@@ -84,12 +84,6 @@ public class FodderController extends BaseController {
 
         // 检查全景图是否被引用
         if ("pano".equals(entity.getType())) {
-//            List<SceneEntity> list = sceneService.findBySceneCode(entity.getSceneCode());
-//            if (list.size() > 0) {
-//                log.error("场景已使用,不能删除");
-//                return Result.failure(MsgCode.e3001, MsgCode.m3001);
-//            }
-
 
             if (entity.getStatus() == 1) {
                 return Result.failure("计算中的场景不能删除");

+ 9 - 0
720yun_fd_manage/remark-m.md

@@ -30,6 +30,15 @@
     用户测试token:
     eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMzExMjMxMTE3OCIsInVzZXJOYW1lIjoiMTMxMTIzMTExNzgiLCJpYXQiOjE2MTM4MDU0NDQsImp0aSI6IjI4MzU0Yzk5LWI0MGItNDIyMy04NDk1LTkwODNhYzE1NGUwNyJ9.jRrXM_7nr1C3CZsOkffQgXV15wDNEkLp02P4O91_ee4
 
+#deploy必备工具
+1. jdk1.8
+2. mysql
+3. redis
+4. ossUtil
+5. krpano 切全景图
+6. convert 切缩略图
+7. rabbit MQ
+
 #dev    
  ## rabbit MQ
     rul: http://8.135.106.227:15672/