Browse Source

no message

FengChaoYu 3 months ago
parent
commit
b78ef51ddc

+ 0 - 6
mall-miniapp-service/pom.xml

@@ -361,12 +361,6 @@
                 </exclusion>
             </exclusions>
         </dependency>
-        <!-- 阿里云OCR识别 -->
-        <dependency>
-            <groupId>com.aliyun</groupId>
-            <artifactId>alibabacloud-ocr_api20210707</artifactId>
-            <version>3.0.3</version>
-        </dependency>
     </dependencies>
 
 

+ 2 - 1
mall-miniapp-service/src/main/java/com/gree/mall/miniapp/controller/user/WorkerController.java

@@ -148,9 +148,10 @@ public class WorkerController {
     public ResponseHelper<Boolean> ocrHandwriting(
             @ApiParam(required = true, value = "拼接附件") @RequestParam MultipartFile file,
             @ApiParam(required = true, value = "签名附件") @RequestParam MultipartFile signFile,
+            @ApiParam(required = true, value = "签名附件base64") @RequestParam String signFile2,
             @ApiParam(required = true, value = "地区 gz=广州 fs=佛山") @RequestParam String type
     ) throws Exception {
-        Boolean result = workerLogic.ocrHandwriting(file, signFile, type);
+        Boolean result = workerLogic.ocrHandwriting(file, signFile, signFile2, type);
         return ResponseHelper.success(result);
     }
 

+ 18 - 18
mall-miniapp-service/src/main/java/com/gree/mall/miniapp/logic/policy/WorkerLogic.java

@@ -25,7 +25,6 @@ import com.gree.mall.miniapp.utils.IpUtil;
 import com.gree.mall.miniapp.utils.StringUtil;
 import com.gree.mall.miniapp.utils.ocr.OCRUtil;
 import lombok.extern.slf4j.Slf4j;
-import net.coobird.thumbnailator.Thumbnails;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
@@ -33,7 +32,7 @@ import org.springframework.util.FileCopyUtils;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletRequest;
-import java.io.*;
+import java.io.File;
 import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Date;
@@ -616,7 +615,7 @@ public class WorkerLogic {
         return new ArrayList<>();
     }
 
-    public Boolean ocrHandwriting(MultipartFile joinFile, MultipartFile signFile, String type) throws Exception {
+    public Boolean ocrHandwriting(MultipartFile joinFile, MultipartFile signFile, String signFile2, String type) throws Exception {
         CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat();
         final User user = currentCompanyWechat.getUser();
         if (!user.getType().equals(UserTypeEnum.WORKER.getKey())) {
@@ -632,27 +631,28 @@ public class WorkerLogic {
 
         // 创建文件
         final String signFilePath = mkdir + signFile.getOriginalFilename();
-        File outputFile = new File(mkdir + "r" +signFile.getOriginalFilename());
+//        File outputFile = new File(mkdir + "r" +signFile.getOriginalFilename());
         File uploadFile = new File(signFilePath);
         uploadFile.createNewFile();
 
         // 将文件写入磁盘
         FileCopyUtils.copy(signFile.getBytes(), uploadFile);
 
-        Thumbnails.of(uploadFile).scale(1).rotate(-90).toFile(outputFile);
-
-        final InputStream inputStream = new FileInputStream(outputFile);
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        byte[] buffer = new byte[1024];
-        int length = 0;
-        while ((length = inputStream.read(buffer)) != -1) {
-            baos.write(buffer, 0, length);
-        }
-        inputStream.close();
-        baos.close();
-
-
-        final String name = ocrUtil.handwritingORC(new ByteArrayInputStream(baos.toByteArray()));
+//        Thumbnails.of(uploadFile).scale(1).rotate(-90).toFile(outputFile);
+//
+//        final InputStream inputStream = new FileInputStream(outputFile);
+//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+//        byte[] buffer = new byte[1024];
+//        int length = 0;
+//        while ((length = inputStream.read(buffer)) != -1) {
+//            baos.write(buffer, 0, length);
+//        }
+//        inputStream.close();
+//        baos.close();
+
+
+//        final String name = ocrUtil.handwritingORC(new ByteArrayInputStream(baos.toByteArray()));
+        String name = ocrUtil.handwritingORC(signFile2);
         log.info("OCR识别名称:" + name + ", 师傅名称:" + user.getNickName());
         if (!StrUtil.equals(name, user.getNickName().replace(" ", ""))) {
             return false;

+ 111 - 57
mall-miniapp-service/src/main/java/com/gree/mall/miniapp/utils/ocr/OCRUtil.java

@@ -1,76 +1,130 @@
 package com.gree.mall.miniapp.utils.ocr;
 
-import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONObject;
-import cn.hutool.json.JSONUtil;
-import com.aliyun.auth.credentials.Credential;
-import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
-import com.aliyun.sdk.service.ocr_api20210707.AsyncClient;
-import com.aliyun.sdk.service.ocr_api20210707.models.RecognizeHandwritingRequest;
-import com.aliyun.sdk.service.ocr_api20210707.models.RecognizeHandwritingResponse;
-import com.aliyun.sdk.service.ocr_api20210707.models.RecognizeHandwritingResponseBody;
-import com.gree.mall.miniapp.exception.RemoteServiceException;
-import darabonba.core.client.ClientOverrideConfiguration;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Value;
+import okhttp3.*;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Service;
 
-import java.io.ByteArrayInputStream;
-import java.util.concurrent.CompletableFuture;
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Base64;
 
 @Service
 @Slf4j
 public class OCRUtil {
+//    阿里OCR手写识别
+//    @Value("${ali.access.key.id}")
+//    private String ACCESS_KEY_ID;
+//    @Value("${ali.access.key.secert}")
+//    private String ACCESS_KEY_SECERT;
+//
+//    public AsyncClient buildClient() {
+//        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
+//                .accessKeyId(ACCESS_KEY_ID)
+//                .accessKeySecret(ACCESS_KEY_SECERT)
+//                .build());
+//        return AsyncClient.builder()
+//                //.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
+//                .credentialsProvider(provider)
+//                //.serviceConfiguration(Configuration.create()) // Service-level configuration
+//                // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
+//                .overrideConfiguration(
+//                        ClientOverrideConfiguration.create()
+//                                // Endpoint 请参考 https://api.aliyun.com/product/ocr-api
+//                                // 服务地址 ocr-api.cn-hangzhou.aliyuncs.com
+//                                // VPC地址 ocr-api-vpc.cn-hangzhou.aliyuncs.com
+//                                .setEndpointOverride("ocr-api.cn-hangzhou.aliyuncs.com")
+//                        //.setConnectTimeout(Duration.ofSeconds(30))
+//                )
+//                .build();
+//    }
+//
+//    public String handwritingORC(ByteArrayInputStream byteArrayInputStream) {
+//        AsyncClient client = buildClient();
+//        // Parameter settings for API request
+//        RecognizeHandwritingRequest recognizeHandwritingRequest = RecognizeHandwritingRequest.builder()
+//                .body(byteArrayInputStream)
+//                .needRotate(false)
+//                .paragraph(true)
+//                // Request-level configuration rewrite, can set Http request parameters, etc.
+//                // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
+//                .build();
+//        CompletableFuture<RecognizeHandwritingResponse> response = client.recognizeHandwriting(recognizeHandwritingRequest);
+//        String content = "";
+//        try {
+//            final RecognizeHandwritingResponse resp = response.get();
+//            final RecognizeHandwritingResponseBody body = resp.getBody();
+//            final JSONObject data = JSONUtil.parseObj(body.getData());
+//            log.info(body.getData());
+//            content = StrUtil.replace(data.get("content", String.class), " ", "");
+//        } catch (Exception e) {
+//            throw new RemoteServiceException("OCR服务异常" + e.getMessage());
+//        } finally {
+//            client.close();
+//        }
+//
+//        return content;
+//    }
 
-    @Value("${ali.access.key.id}")
-    private String ACCESS_KEY_ID;
-    @Value("${ali.access.key.secert}")
-    private String ACCESS_KEY_SECERT;
+    private static final String API_KEY = "rQX5f1IdlpKGFHypnyLetH80";
+    private static final String SECRET_KEY = "H38soq4UuIjUJ7eeZPPxv1ZR6e23jKDx";
+    private static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
 
-    public AsyncClient buildClient() {
-        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
-                .accessKeyId(ACCESS_KEY_ID)
-                .accessKeySecret(ACCESS_KEY_SECERT)
-                .build());
-        return AsyncClient.builder()
-                //.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
-                .credentialsProvider(provider)
-                //.serviceConfiguration(Configuration.create()) // Service-level configuration
-                // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
-                .overrideConfiguration(
-                        ClientOverrideConfiguration.create()
-                                // Endpoint 请参考 https://api.aliyun.com/product/ocr-api
-                                // 服务地址 ocr-api.cn-hangzhou.aliyuncs.com
-                                // VPC地址 ocr-api-vpc.cn-hangzhou.aliyuncs.com
-                                .setEndpointOverride("ocr-api.cn-hangzhou.aliyuncs.com")
-                        //.setConnectTimeout(Duration.ofSeconds(30))
-                )
+    /**
+     * 从用户的AK,SK生成鉴权签名(Access Token)
+     *
+     * @return 鉴权签名(Access Token)
+     * @throws IOException IO异常
+     */
+    private static String getAccessToken() throws IOException {
+        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
+        RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
+                + "&client_secret=" + SECRET_KEY);
+        Request request = new Request.Builder()
+                .url("https://aip.baidubce.com/oauth/2.0/token")
+                .method("POST", body)
+                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                 .build();
+        Response response = HTTP_CLIENT.newCall(request).execute();
+        return new JSONObject(response.body().string()).getStr("access_token");
     }
 
-    public String handwritingORC(ByteArrayInputStream byteArrayInputStream) {
-        AsyncClient client = buildClient();
-        // Parameter settings for API request
-        RecognizeHandwritingRequest recognizeHandwritingRequest = RecognizeHandwritingRequest.builder()
-                .body(byteArrayInputStream)
-                .needRotate(false)
-                // Request-level configuration rewrite, can set Http request parameters, etc.
-                // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
-                .build();
-        CompletableFuture<RecognizeHandwritingResponse> response = client.recognizeHandwriting(recognizeHandwritingRequest);
-        String content = "";
-        try {
-            final RecognizeHandwritingResponse resp = response.get();
-            final RecognizeHandwritingResponseBody body = resp.getBody();
-            final JSONObject data = JSONUtil.parseObj(body.getData());
-            log.info(body.getData());
-            content = StrUtil.replace(data.get("content", String.class), " ", "");
-        } catch (Exception e) {
-            throw new RemoteServiceException("OCR服务异常" + e.getMessage());
-        } finally {
-            client.close();
+    /**
+     * 获取文件base64编码
+     *
+     * @param path      文件路径
+     * @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true
+     * @return base64编码信息,不带文件头
+     * @throws IOException IO异常
+     */
+    private static String getFileContentAsBase64(String path, String imageBase64, boolean urlEncode) throws IOException {
+        String base64 = imageBase64;
+        if (StringUtils.isBlank(path)) {
+            byte[] b = Files.readAllBytes(Paths.get(path));
+            base64 = Base64.getEncoder().encodeToString(b);
         }
 
-        return content;
+        if (urlEncode) {
+            base64 = URLEncoder.encode(base64, "utf-8");
+        }
+        return base64;
+    }
+
+    public String handwritingORC(String imageBase64) throws IOException{
+        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
+        // image 可以通过 getFileContentAsBase64("C:\fakepath\1735789516918.png") 方法获取,如果Content-Type是application/x-www-form-urlencoded时,第二个参数传true
+        RequestBody body = RequestBody.create(mediaType, "image=" + getFileContentAsBase64("", imageBase64,true) + "&detect_direction=true&probability=false&detect_alteration=false");
+        Request request = new Request.Builder()
+                .url("https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting?access_token=" + getAccessToken())
+                .method("POST", body)
+                .addHeader("Content-Type", "application/x-www-form-urlencoded")
+                .addHeader("Accept", "application/json")
+                .build();
+        Response response = HTTP_CLIENT.newCall(request).execute();
+        log.info("百度OCR返回结果:" + response.body().string());
+        return new JSONObject(response.body().string()).getJSONArray("words_result").getJSONObject(0).getStr("words");
     }
 }