Ver Fonte

添加手动上传图片功能

yang há 3 anos atrás
pai
commit
0801ce20c9

+ 80 - 0
huimv-video/huimv-video-process/src/main/java/com/huimv/process/controller/ImageController.java

@@ -3,6 +3,7 @@ package com.huimv.process.controller;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.huimv.process.service.IImageService;
+import com.huimv.process.utils.Result;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.ByteArrayResource;
@@ -10,6 +11,8 @@ import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.MultiValueMap;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.client.RestTemplate;
+import org.springframework.web.multipart.MultipartFile;
+import sun.misc.BASE64Encoder;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -28,10 +31,83 @@ import java.util.Date;
 @RestController
 @RequestMapping("/image")
 @Slf4j
+@CrossOrigin
 public class ImageController {
     @Autowired
     private IImageService imageService;
 
+
+    /**
+     * 杨迪--手动上传图片并且保存
+     * @param name  文件名字
+     * @param file 文件
+     * @return
+     * @throws Exception
+     */
+    @ResponseBody
+    @RequestMapping(value = "/uploadbyhand", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
+    public Result uploadImageByhand(String name, @RequestParam(required = true)MultipartFile file   ) throws Exception {
+       //log.info("# 输入参数name>>" + paramJo.getString("name"));
+        log.info("# 输入参数name>>" + name );
+       //log.info("# 输入参数image>>" + paramJo.getString("image"));
+       // String name = paramJo.getString("name");
+        if (name == null) {
+            name ="手动上传";
+            log.error("出错:参数name为null-->>自动置为手动上传");
+        }
+       // String image = paramJo.getString("image");
+        //将二进制文件转换成为base64
+        System.out.println("开始转换");
+        String image1 = convertFliesToBase64(file);
+        String[] sourceStrArray = image1.split(",");
+        String image = sourceStrArray[1];
+        if (image == null) {
+            log.error("出错:参数image为null");
+            return new Result(1000,"提交照片不能为空");
+        }
+//        if(imageService.saveImage(imageBase64,name)){
+        // 保存图片
+        JSONObject saveResultJo = imageService.saveImage(image, name);
+        if (saveResultJo.getBoolean("result")) {
+            log.info("保存图片成功");
+            String imgPath = saveResultJo.getString("imgPath");
+            String relativePath = saveResultJo.getString("relativePath");
+            String fileName = saveResultJo.getString("fileName");
+            // AI识别程序的结果
+            JSONObject aiResultJo = imageService.getAiResult(imgPath,fileName);
+            String aiResult = aiResultJo.getString("result");
+            if(!aiResult.trim().equalsIgnoreCase("success")){
+                log.error("AI识别结果出错: "+aiResultJo);
+                return new Result(1000,"AI识别结果出错");
+            }else{
+                log.info("AI正常识别.");
+            }
+            int aiTimeUsed = aiResultJo.getInteger("timeUsed");
+            JSONArray aiPiggyJa = aiResultJo.getJSONArray("piggy");
+            int pigQuantity = aiPiggyJa.size();
+            Timestamp createTime = new Timestamp(new Date().getTime());
+            // 图片入库
+            if(imageService.addImage(name, imgPath, relativePath, fileName,aiResult,aiTimeUsed,aiPiggyJa.toString(),createTime,pigQuantity)){
+                log.info("图片入库成功.");
+            }else{
+                log.info("图片入库出错.");
+                return new Result(1000,"图片入库出错");
+            }
+        } else {
+            log.error("保存图片出错");
+            return new Result(1000,"保存图片出错");
+        }
+        return new Result(2000,"添加成功");
+    }
+
+    //file二进制  转   base64
+    public String convertFliesToBase64(MultipartFile file) throws Exception{
+        BASE64Encoder base64Encoder =new BASE64Encoder();
+        String base64EncoderImg = file.getOriginalFilename()+","+ base64Encoder.encode(file.getBytes());
+        return base64EncoderImg;
+    }
+
+    //接图
     @ResponseBody
     @RequestMapping(value = "/upload", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
     public void uploadImage(@RequestBody JSONObject paramJo) throws IOException {
@@ -76,6 +152,10 @@ public class ImageController {
         }
     }
 
+
+
+
+
     /**
      * @Method      : uploadPicture
      * @Description : 

+ 2 - 0
huimv-video/huimv-video-process/src/main/java/com/huimv/process/service/impl/ImageServiceImpl.java

@@ -83,6 +83,8 @@ public class ImageServiceImpl implements IImageService {
             imageCatalogEntity.setCatalogPath(cameraName);
             imageCatalogRepo.saveAndFlush(imageCatalogEntity);
         }
+
+
         // type1
 //        String imgPath = uploadPath + name + "_"+ timeMillis + ".jpg";
         // type2

+ 28 - 0
huimv-video/huimv-video-process/src/main/java/com/huimv/process/utils/Result.java

@@ -0,0 +1,28 @@
+package com.huimv.process.utils;
+
+import lombok.Data;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.face.result
+ * @Description : TODO
+ * @Author : yuxuexuan
+ * @Create : 2020/12/29 0029 14:46
+ **/
+@Data
+public class Result {
+    private Integer  status;
+    private String msg;
+    private Object data;
+
+    public Result(Integer status, String msg, Object data) {
+        this.status = status;
+        this.msg = msg;
+        this.data = data;
+    }
+
+    public Result(Integer status, String msg) {
+        this.status = status;
+        this.msg = msg;
+    }
+}

+ 1 - 1
huimv-video/huimv-video-process/src/main/resources/application-dev.yml

@@ -3,7 +3,7 @@ server:
 
 spring:
   datasource:
-    url: jdbc:mysql://192.168.1.7:3306/huimv-video_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&serverTimezone=Asia/Shanghai
+    url: jdbc:mysql://192.168.1.7:3306/huimv-video2_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&serverTimezone=Asia/Shanghai
     username: root
     password: hm123456
     driver-class-name: com.mysql.cj.jdbc.Driver

+ 3 - 0
huimv-video/huimv-video-process/src/test/java/com/huimv/process/service/PicUtil.java

@@ -13,6 +13,8 @@ import java.io.*;
  * @Author : ZhuoNing
  * @Create : 2020-12-25
  **/
+
+
 public class PicUtil {
     /**
      * @param imgStr base64编码字符串
@@ -68,6 +70,7 @@ public class PicUtil {
         return encoder.encode(data);
     }
 
+
     /**
      * 示例
      */