Jelajahi Sumber

新增自动更新不合格图片名称的功能。

zhuoning 4 tahun lalu
induk
melakukan
dee7d1195d

+ 7 - 0
huimv-video/huimv-video-manager/src/main/java/com/huimv/manager/controller/ImageManagerController.java

@@ -50,6 +50,13 @@ public class ImageManagerController {
     private DateUtil dateUtil;
     protected HttpServletResponse response;
 
+    @RequestMapping(value = "/autoUpdate", method = RequestMethod.POST)
+    public void autoUpdate(Integer id,String type){
+        //
+        boolean updateResult = imageService.autoUpdateImage(id,type);
+        System.out.println("updateResult>>"+updateResult);
+    }
+
     /**
      * @Method : getLastImageAiResult
      * @Description : 读取最新的图像AI识别记录

+ 6 - 0
huimv-video/huimv-video-manager/src/main/java/com/huimv/manager/dao/repo/ImagePathRepo.java

@@ -154,4 +154,10 @@ public interface ImagePathRepo extends JpaRepository<ImagePathEntity, Integer>,
      */
     @Query(nativeQuery = true, value = "SELECT * FROM image_path WHERE name=:catalog AND id<:imageId ORDER BY id DESC LIMIT 1")
     ImagePathEntity getPreviousImageById(int imageId, String catalog);
+
+    @Query(nativeQuery = true, value = "SELECT * FROM image_path WHERE  id>=:id")
+    List<ImagePathEntity> getAllUpdateImage(Integer id);
+
+    @Query(nativeQuery = true, value = "SELECT * FROM image_path WHERE  id=:id")
+    List<ImagePathEntity> getOneUpdateImage(Integer id);
 }

+ 12 - 0
huimv-video/huimv-video-manager/src/main/java/com/huimv/manager/service/IImageService.java

@@ -94,4 +94,16 @@ public interface IImageService {
 
 
     int getTotal(String startDate, String endDate);
+
+    /**
+     * @Method      : autoUpdateImage
+     * @Description : 自动更新图像名称
+     * @Params      : []
+     * @Return      : boolean
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2021/1/28       
+     * @Time        : 15:23
+     */
+    boolean autoUpdateImage(Integer id,String type);
 }

+ 119 - 0
huimv-video/huimv-video-manager/src/main/java/com/huimv/manager/service/impl/ImageServiceImpl.java

@@ -1,11 +1,14 @@
 package com.huimv.manager.service.impl;
 
+import com.alibaba.fastjson.JSONObject;
 import com.huimv.manager.dao.entity.ImagePathEntity;
 import com.huimv.manager.dao.repo.ImagePathRepo;
 import com.huimv.manager.service.IImageService;
+import com.huimv.manager.utils.DateUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -21,6 +24,122 @@ import java.util.List;
 public class ImageServiceImpl implements IImageService {
     @Autowired
     private ImagePathRepo imagePathRepo;
+    @Autowired
+    private DateUtil dateUtil;
+
+    @Override
+    public boolean autoUpdateImage(Integer id,String type) {
+        List<ImagePathEntity> imageEntityList = new ArrayList<>();
+        if(type !=null && type.trim().equalsIgnoreCase("one")){
+            // 读取更新图片
+            imageEntityList = imagePathRepo.getOneUpdateImage(id);
+        }else{
+            // 读取更新图片
+            imageEntityList = imagePathRepo.getAllUpdateImage(id);
+        }
+        for(ImagePathEntity imageEntity:imageEntityList){
+            // 更新图片名称
+            if(!updateImage(imageEntity)){
+                System.out.println("【图片更新名称出错】");
+            }else{
+                // 更新图片表
+                System.out.println(" 【更新后的图片信息】"+imageEntity);
+                imagePathRepo.saveAndFlush(imageEntity);
+            }
+        }
+        return true;
+    }
+
+    private boolean updateImage(ImagePathEntity imageEntity) {
+        String fileName = imageEntity.getFileName();
+        String relativePath = imageEntity.getRelativePath();
+        String imgPath = imageEntity.getImgPath();
+        String camera = imageEntity.getName();
+
+        System.out.println("【fileName】"+fileName);
+        System.out.println("【relativePath】"+relativePath);
+        System.out.println("【imgPath】"+imgPath);
+        System.out.println("【camera】"+camera);
+
+        if(imgPath.indexOf("/"+camera+"/") == -1){
+            System.out.println("----------------------【未分摄像头保存的图片】----------------------");
+            String[] fileArray = fileName.split("_");
+            if(fileArray.length==2){
+                String dateName = fileArray[1];
+                System.out.println("【日期名称(包含图片扩展名)】"+dateName);
+                String[] dateNameArray = dateName.split("\\.");
+                System.out.println("dateNameArray.length>>"+dateNameArray.length);
+                String timeName = dateNameArray[0];
+                System.out.println("【日期名称(不包含图片扩展名)】"+timeName);
+                String newDateName = dateUtil.formatLongTimeToImageName(Long.parseLong(timeName));
+                System.out.println("【新时间名称】"+newDateName);
+                String newFileName = camera+"_"+newDateName+".jpg";
+                System.out.println("【新文件名称】"+newFileName);
+                String newImgPath = imgPath.replace(fileName,"")+camera+"/"+newFileName;
+                System.out.println("【fileName】 "+fileName);
+                System.out.println("[newImgPath] "+newImgPath+" 【imgPath】"+imgPath);
+                String newRelativePath = relativePath.replace(fileName,"")+camera+"/"+newFileName;
+                System.out.println("[newRelativePath] "+newRelativePath+" 【relativePath】"+relativePath);
+
+                File file = new File(imgPath);
+                if(file.exists()){
+                    System.out.println("该文件存在路径是:"+imgPath);
+                    // 对图片重命名
+                    file.renameTo(new File(newImgPath));
+                    // 重置属性
+                    imageEntity.setFileName(newFileName);
+                    imageEntity.setImgPath(newImgPath);
+                    imageEntity.setRelativePath(newRelativePath);
+                    return true;
+                }else{
+                    System.out.println("【"+fileName+"】该文件不存在");
+                    return false;
+                }
+            }else{
+                System.out.println("【"+fileName+"】该文件名不需要更新");
+                return false;
+            }
+
+        }else{
+            // 已经分摄像头保存的图片
+            String[] fileArray = fileName.split("_");
+            if(fileArray.length==2){
+                String dateName = fileArray[1];
+                System.out.println("【日期名称(包含图片扩展名)】"+dateName);
+                String[] dateNameArray = dateName.split("\\.");
+                System.out.println("dateNameArray.length>>"+dateNameArray.length);
+                String timeName = dateNameArray[0];
+                System.out.println("【日期名称(不包含图片扩展名)】"+timeName);
+                String newDateName = dateUtil.formatLongTimeToImageName(Long.parseLong(timeName));
+                System.out.println("【新时间名称】"+newDateName);
+                String newFileName = camera+"_"+newDateName+".jpg";
+                System.out.println("【新文件名称】"+newFileName);
+                String newImgPath = imgPath.replace(fileName,newFileName);
+                System.out.println("【fileName】 "+fileName);
+                System.out.println("[newImgPath] "+newImgPath+" 【imgPath】"+imgPath);
+                String newRelativePath = relativePath.replace(fileName,newFileName);
+                System.out.println("[newRelativePath] "+newRelativePath+" 【relativePath】"+relativePath);
+
+                File file = new File(imgPath);
+                if(file.exists()){
+                    System.out.println("该文件存在路径是:"+imgPath);
+                    // 对图片重命名
+                    file.renameTo(new File(newImgPath));
+                    // 重置属性
+                    imageEntity.setFileName(newFileName);
+                    imageEntity.setImgPath(newImgPath);
+                    imageEntity.setRelativePath(newRelativePath);
+                    return true;
+                }else{
+                    System.out.println("【"+fileName+"】该文件不存在");
+                    return false;
+                }
+            }else{
+                System.out.println("【"+fileName+"】该文件名不需要更新");
+                return false;
+            }
+        }
+    }
 
     /**
      * @Method : getLastImage

+ 8 - 0
huimv-video/huimv-video-manager/src/main/java/com/huimv/manager/utils/DateUtil.java

@@ -39,4 +39,12 @@ public class DateUtil {
         //方法一
         return sdf.format(timestamp);
     }
+
+    public String formatLongTimeToImageName(Long dateLong){
+        Date date =new Date(dateLong);//以1429339937748为bai毫秒数du实例化zhi一个Date对象dao
+        SimpleDateFormat sdf=new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");//设置转化格式
+        String time=sdf.format(date);//将Date对象转化为yyyy-MM-dd形式的字符串zhuan
+        System.out.println(time);//输出字符串
+        return time;
+    }
 }

+ 5 - 1
huimv-video/huimv-video-process/src/main/java/com/huimv/process/service/impl/ImageServiceImpl.java

@@ -6,6 +6,7 @@ import com.huimv.process.dao.entity.ImagePathEntity;
 import com.huimv.process.dao.repo.ImageCatalogRepo;
 import com.huimv.process.dao.repo.ImagePathEntityRepository;
 import com.huimv.process.service.IImageService;
+import com.huimv.process.utils.DateUtil;
 import com.huimv.process.utils.PictureUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -42,6 +43,8 @@ public class ImageServiceImpl implements IImageService {
     private com.huimv.process.utils.utils utils;
     @Autowired
     private ImageCatalogRepo imageCatalogRepo;
+    @Autowired
+    private DateUtil dateUtil;
 
     /**
      * @Method : saveImage
@@ -87,7 +90,8 @@ public class ImageServiceImpl implements IImageService {
 //        String imgPath = imgBuild.append(uploadPath).append(name).append("_").append(timeMillis).append(".jpg").toString();
         // type 3
         StringBuilder fileNameBuild = new StringBuilder();
-        String fileName = fileNameBuild.append(cameraName).append("_").append(timeMillis).append(".jpg").toString();
+//        String fileName = fileNameBuild.append(cameraName).append("_").append(timeMillis).append(".jpg").toString();
+        String fileName = fileNameBuild.append(cameraName).append("_").append(dateUtil.formatLongTimeToImageName(timeMillis)).append(".jpg").toString();
         String relativePath = uploadPath + fileName;
         String imgPath = userDir + relativePath;
         JSONObject resultJo = new JSONObject();

+ 50 - 0
huimv-video/huimv-video-process/src/main/java/com/huimv/process/utils/DateUtil.java

@@ -0,0 +1,50 @@
+package com.huimv.process.utils;
+
+import org.springframework.stereotype.Component;
+
+import java.sql.Timestamp;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Component
+public class DateUtil {
+//1429339937748l
+    public String formatLongTime(Long dateLong){
+        Date date =new Date(dateLong);//以1429339937748为bai毫秒数du实例化zhi一个Date对象dao
+        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//设置转化格式
+        String time=sdf.format(date);//将Date对象转化为yyyy-MM-dd形式的字符串zhuan
+        System.out.println(time);//输出字符串
+        return time;
+    }
+
+    public String formatLongTimeForTime(Long dateLong){
+        Date date =new Date(dateLong);//以1429339937748为bai毫秒数du实例化zhi一个Date对象dao
+        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");//设置转化格式
+        String time=sdf.format(date);//将Date对象转化为yyyy-MM-dd形式的字符串zhuan
+        System.out.println(time);//输出字符串
+        return time;
+    }
+
+    public String formatLongTimeToImageName(Long dateLong){
+        Date date =new Date(dateLong);//以1429339937748为bai毫秒数du实例化zhi一个Date对象dao
+        SimpleDateFormat sdf=new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");//设置转化格式
+        String time=sdf.format(date);//将Date对象转化为yyyy-MM-dd形式的字符串zhuan
+        System.out.println(time);//输出字符串
+        return time;
+    }
+
+    public String formatTimestamp(Timestamp timestamp) {
+        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        //方法一
+        return sdf.format(timestamp);
+    }
+}