Procházet zdrojové kódy

裂解炉炉管测压 - 图片识别

wangggziwen před 11 měsíci
rodič
revize
29be04fe78

+ 16 - 2
master/pom.xml

@@ -445,8 +445,22 @@
             <artifactId>pdfbox</artifactId>
             <version>2.0.4</version>
         </dependency>
-
-
+        <!--http-->
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpcore</artifactId>
+            <version>4.4.9</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+            <version>4.5.13</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpmime</artifactId>
+            <version>4.5.12</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 160 - 4
master/src/main/java/com/ruoyi/project/production/controller/TFurnancePressureController.java

@@ -1,14 +1,15 @@
 package com.ruoyi.project.production.controller;
 
-import java.io.IOException;
+import java.io.*;
+import java.nio.charset.StandardCharsets;
 import java.text.DecimalFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.*;
 
+import com.alibaba.fastjson.JSONObject;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.file.FileUploadUtils;
-import com.ruoyi.common.utils.file.FileUtils;
 import com.ruoyi.framework.config.RuoYiConfig;
 import com.ruoyi.project.common.domain.TCommonfile;
 import com.ruoyi.project.common.service.ITCommonfileService;
@@ -16,8 +17,18 @@ import com.ruoyi.project.production.controller.vo.FurnancePressureFvpVO;
 import com.ruoyi.project.production.controller.vo.FurnancePressureVO;
 import com.ruoyi.project.production.controller.vo.FurnanceSummaryVO;
 import com.ruoyi.project.production.controller.vo.FvpAnalysisQueryVO;
-import com.ruoyi.project.production.domain.TFurnanceTemperature;
 import com.ruoyi.project.production.mapper.TFurnancePressureMapper;
+import org.apache.http.HttpEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.mime.HttpMultipartMode;
+import org.apache.http.entity.mime.MultipartEntityBuilder;
+import org.apache.http.entity.mime.content.FileBody;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
@@ -31,7 +42,6 @@ import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.framework.web.page.TableDataInfo;
 import org.springframework.web.multipart.MultipartFile;
 
-
 /**
  * 裂解炉炉管测压Controller
  *
@@ -51,6 +61,152 @@ public class TFurnancePressureController extends BaseController
     @Autowired
     private ITCommonfileService tCommonfileService;
 
+    /**
+     * 裂解炉测压图片识别(APP)
+     */
+    @PostMapping("/scan")
+    public AjaxResult scan(@RequestParam("file") MultipartFile file,
+                           @RequestParam("furnanceName") String furnanceName,
+                           @RequestParam("pass") int pass) throws IOException {
+        // 判断图片中的压力仪表数量
+        int num = 0;
+        switch (furnanceName) {
+            case "H109": num = 15; break;
+            case "H111":
+            case "H112":
+            case "H113":
+            case "H114":
+            case "H115":
+            case "H116":
+            case "H117":
+            case "H110": num = 11; break;
+            case "H130": num = 7; break;
+        }
+//        // 调用图片识别接口
+        String goodsUrl = "http://cpms.v6.idcfengye.com/detection_web";
+        //本地文件位置
+//        String fileName = "E:\\image.jpg";
+        String str = null;
+        try {
+            //添加请求头
+            HashMap<String, String> map = new HashMap<>();
+            //map.put("token", CommonConstant.token);
+            File f = this.multipartFileToFile(file);
+            str = doPostUploadFile(goodsUrl + "", map, f, num);
+            if(f.exists()) {
+                boolean delete = f.delete();
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        JSONObject json = JSONObject.parseObject(str);
+        return AjaxResult.success(json.get("data"));
+    }
+
+    /**
+     * MultipartFile转File
+     * @param file
+     * @return
+     * @throws IOException
+     */
+    public File multipartFileToFile(MultipartFile file) throws IOException {
+        File convFile = null;
+        if (file.getSize() > 0) {
+            convFile = new File(file.getOriginalFilename());
+            convFile.createNewFile();
+            FileOutputStream fos = new FileOutputStream(convFile);
+            fos.write(file.getBytes());
+            fos.close();
+        }
+        return convFile;
+    }
+
+    /**
+     * post请求提交form-data上传文件
+     *
+     * @param url 上传地址
+     * @param headers 请求头
+     * @param file 上传文件
+     * @return
+     */
+    public String doPostUploadFile(String url, Map<String, String> headers, File file, int num) {
+        HttpPost httpPost = new HttpPost(url);
+        packageHeader(headers, httpPost);
+        String fileName = file.getName();
+
+        CloseableHttpResponse response = null;
+
+        String respContent = null;
+
+        long startTime = System.currentTimeMillis();
+
+        // 设置请求头 boundary边界不可重复,重复会导致提交失败
+        String boundary = "-------------------------" + UUID.randomUUID().toString();
+        httpPost.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
+
+        // 创建MultipartEntityBuilder
+        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
+        // 设置字符编码
+        builder.setCharset(StandardCharsets.UTF_8);
+        // 模拟浏览器
+        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
+        // 设置边界
+        builder.setBoundary(boundary);
+        // 设置multipart/form-data流文件
+        builder.addPart("image", new FileBody(file));
+        builder.addTextBody("num", num+"");
+        // application/octet-stream代表不知道是什么格式的文件
+        builder.addBinaryBody("media", file, ContentType.create("application/octet-stream"), fileName);
+
+        HttpEntity entity = builder.build();
+        httpPost.setEntity(entity);
+        CloseableHttpClient httpClient = HttpClients.createDefault();
+        try {
+            response = httpClient.execute(httpPost);
+            if (response != null && response.getStatusLine() != null && response.getStatusLine().getStatusCode() < 400) {
+                HttpEntity he = response.getEntity();
+                if (he != null) {
+                    respContent = EntityUtils.toString(he, "UTF-8");
+                }
+            } else {
+                logger.error("对方响应的状态码不在符合的范围内!");
+                throw new RuntimeException();
+            }
+            return respContent;
+        } catch (Exception e) {
+            logger.error("网络访问异常,请求url地址={},响应体={},error={}", url, response, e);
+            throw new RuntimeException();
+        } finally {
+            logger.info("统一外网请求参数打印,post请求url地址={},响应={},耗时={}毫秒", url, respContent, (System.currentTimeMillis() - startTime));
+            try {
+                if (response != null) {
+                    response.close();
+                }
+                if(null != httpClient){
+                    httpClient.close();
+                }
+            } catch (IOException e) {
+                logger.error("请求链接释放异常", e);
+            }
+        }
+    }
+
+    /**
+     * 封装请求头
+     *
+     * @param paramsHeads
+     * @param httpMethod
+     */
+    private static void packageHeader(Map<String, String> paramsHeads, HttpRequestBase httpMethod) {
+        if (null!= paramsHeads && paramsHeads.size()>0) {
+            Set<Map.Entry<String, String>> entrySet = paramsHeads.entrySet();
+            for (Map.Entry<String, String> entry : entrySet) {
+                httpMethod.setHeader(entry.getKey(), entry.getValue());
+            }
+        }
+    }
+
     /**
      * 裂解炉测压列表Summary(APP)
      */