|
@@ -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 :
|