Ver código fonte

人脸对比、手写签名接口优化

gaoxinguang 2 anos atrás
pai
commit
b3a000a6a2

+ 2 - 7
eladmin-system/src/main/java/me/zhengjie/application/bank/controller/BankAuthController.java

@@ -17,6 +17,7 @@ import me.zhengjie.base.ResponseDTO;
 import me.zhengjie.base.ResultCode;
 import me.zhengjie.base.util.BaiduServiceUtil;
 import me.zhengjie.base.util.FileUploadUtil;
+import me.zhengjie.base.util.HandwritingUtil;
 import me.zhengjie.base.util.StatusEnum.StepStatusEnum;
 import me.zhengjie.base.util.TencentServiceUtil;
 import me.zhengjie.dao.mybatis.UserAXQInfoRepository;
@@ -134,14 +135,8 @@ public class BankAuthController extends BaseController {
         }
 
         if ("4".equals(image.getStep())) {
-            String ocrName = "";
             String type = environment.getProperty("fqgz.bank.handwriting");
-            if ("baidu".equals(type)) {
-                ocrName = BaiduServiceUtil.handwriting(image.getBase64());
-            }
-            if ("tencent".equals(type)) {
-                ocrName = TencentServiceUtil.getHandwritingText(image.getBase64());
-            }
+            String ocrName = HandwritingUtil.getHandwritingText(type, image.getBase64());
             UserAXQInfoEntity entity = userAXQInfoRepository.getUserAXQInfoWithIdcard(image.getIdCard());
             if (!ocrName.equals(entity.getUserName())) {
                 return ResponseDTO.error(ResultCode.HANDWRITING_FAIL);

Diferenças do arquivo suprimidas por serem muito extensas
+ 27 - 23
eladmin-system/src/main/java/me/zhengjie/base/util/BaiduServiceUtil.java


Diferenças do arquivo suprimidas por serem muito extensas
+ 20 - 0
eladmin-system/src/main/java/me/zhengjie/base/util/HandwritingUtil.java


+ 32 - 11
eladmin-system/src/main/java/me/zhengjie/base/util/JuheServiceUtil.java

@@ -1,5 +1,7 @@
 package me.zhengjie.base.util;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.uinid.util.HttpRequestUtil;
 
 import java.io.File;
@@ -26,6 +28,10 @@ public class JuheServiceUtil {
     private static final String EMPTY = "";
     private static final String JuHeURl = "http://apis.juhe.cn/verifyface/verify";
 
+    private static final String KEY_CODE = "6db21a71bee88af1eac38480c1574e2a";
+
+    private static final String SUCCESS_CODE = "1";
+
     private static MultiThreadedHttpConnectionManager connectionManager = null;
 
     private static int connectionTimeOut = 5000;
@@ -36,6 +42,7 @@ public class JuheServiceUtil {
 
     private static int maxTotalConnections = 500;
 
+
     private static HttpClient client;
 
     static {
@@ -50,21 +57,24 @@ public class JuheServiceUtil {
     /**
      * 聚合人脸比对接口
      *
-     * @param url    请求地址
      * @param params 业务参数
      * @return String response
      */
-    public static String URLPost(String url, Map<String, Object> params) {
+    public static boolean URLPost(Map<String, Object> params) {
         String response = EMPTY;
         PostMethod postMethod = null;
         try {
-            postMethod = new PostMethod(url);
+            postMethod = new PostMethod(JuHeURl);
             // 将表单的值放入postMethod中
             if (params != null) {
                 Set<String> keySet = params.keySet();
                 for (String key : keySet) {
-                    Object value = params.get(key);
-                    postMethod.addParameter(key, String.valueOf(value));
+                    if ("key".equals(key)) {
+                        postMethod.addParameter(key, KEY_CODE);
+                    } else {
+                        Object value = params.get(key);
+                        postMethod.addParameter(key, String.valueOf(value));
+                    }
                 }
             }
             postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, UTF8);
@@ -72,6 +82,16 @@ public class JuheServiceUtil {
             int statusCode = client.executeMethod(postMethod);
             if (statusCode == HttpStatus.SC_OK) {
                 response = postMethod.getResponseBodyAsString();
+                JSONObject jsonObj = JSON.parseObject(response);
+                JSONObject result = jsonObj.getJSONObject("result");
+                String res = result.getString("res");
+                String score = result.getString("score");
+                if (!SUCCESS_CODE.equals(res) || Integer.parseInt(score) < 45) {
+                    return false;
+                }
+                if (SUCCESS_CODE.equals(res) && Integer.parseInt(score) >= 45) {
+                    return true;
+                }
             } else {
                 logger.error("响应状态码=" + postMethod.getStatusCode());
             }
@@ -85,14 +105,15 @@ public class JuheServiceUtil {
                 postMethod = null;
             }
         }
-        return response;
+        return false;
     }
 
     public static void main(String[] args) throws Exception {
-        String imgFile = "";// 图片地址
+        //String imgFile = "D:\\repository\\fqhz-server-nt-2022-12-27\\fqgz-server\\eladmin-system\\src\\main\\resources\\gxg.jpg";// 图片地址
+        String imgFile = "D:\\repository\\fqhz-server-nt-2022-12-27\\fqgz-server\\eladmin-system\\src\\main\\resources\\adh.jpg";
         File file = new File(imgFile);
         String realName = "xxx";// 姓名
-        String idCard = "xxxxxxxxxxxxxxxx";// 身份证号
+        String idCard = "xxxxxxxxxxx";// 身份证号
         FileInputStream fin = null;
         try {
             fin = new FileInputStream(file);
@@ -104,9 +125,9 @@ public class JuheServiceUtil {
             params.put("idcard", idCard);
             params.put("realname", realName);
             params.put("image", base64);
-            params.put("key", "6db21a71bee88af1eac38480c1574e2a");
-            String data = JuheServiceUtil.URLPost(JuHeURl, params);
-            System.out.println(data);
+            params.put("key", KEY_CODE);
+            boolean isMatch = JuheServiceUtil.URLPost(params);
+            System.out.println(isMatch);
         } catch (Exception e) {
             e.printStackTrace();
         } finally {

+ 0 - 65
eladmin-system/src/main/java/me/zhengjie/base/util/baidu/Base64Util.java

@@ -1,65 +0,0 @@
-package me.zhengjie.base.util.baidu;
-
-/**
- * Base64 工具类
- */
-public class Base64Util {
-    private static final char last2byte = (char) Integer.parseInt("00000011", 2);
-    private static final char last4byte = (char) Integer.parseInt("00001111", 2);
-    private static final char last6byte = (char) Integer.parseInt("00111111", 2);
-    private static final char lead6byte = (char) Integer.parseInt("11111100", 2);
-    private static final char lead4byte = (char) Integer.parseInt("11110000", 2);
-    private static final char lead2byte = (char) Integer.parseInt("11000000", 2);
-    private static final char[] encodeTable = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
-
-    public Base64Util() {
-    }
-
-    public static String encode(byte[] from) {
-        StringBuilder to = new StringBuilder((int) ((double) from.length * 1.34D) + 3);
-        int num = 0;
-        char currentByte = 0;
-
-        int i;
-        for (i = 0; i < from.length; ++i) {
-            for (num %= 8; num < 8; num += 6) {
-                switch (num) {
-                    case 0:
-                        currentByte = (char) (from[i] & lead6byte);
-                        currentByte = (char) (currentByte >>> 2);
-                    case 1:
-                    case 3:
-                    case 5:
-                    default:
-                        break;
-                    case 2:
-                        currentByte = (char) (from[i] & last6byte);
-                        break;
-                    case 4:
-                        currentByte = (char) (from[i] & last4byte);
-                        currentByte = (char) (currentByte << 2);
-                        if (i + 1 < from.length) {
-                            currentByte = (char) (currentByte | (from[i + 1] & lead2byte) >>> 6);
-                        }
-                        break;
-                    case 6:
-                        currentByte = (char) (from[i] & last2byte);
-                        currentByte = (char) (currentByte << 4);
-                        if (i + 1 < from.length) {
-                            currentByte = (char) (currentByte | (from[i + 1] & lead4byte) >>> 4);
-                        }
-                }
-
-                to.append(encodeTable[currentByte]);
-            }
-        }
-
-        if (to.length() % 4 != 0) {
-            for (i = 4 - to.length() % 4; i > 0; --i) {
-                to.append("=");
-            }
-        }
-
-        return to.toString();
-    }
-}

+ 0 - 72
eladmin-system/src/main/java/me/zhengjie/base/util/baidu/FileUtil.java

@@ -1,72 +0,0 @@
-package me.zhengjie.base.util.baidu;
-
-import java.io.*;
-
-/**
- * 文件读取工具类
- */
-public class FileUtil {
-
-    /**
-     * 读取文件内容,作为字符串返回
-     */
-    public static String readFileAsString(String filePath) throws IOException {
-        File file = new File(filePath);
-        if (!file.exists()) {
-            throw new FileNotFoundException(filePath);
-        } 
-
-        if (file.length() > 1024 * 1024 * 1024) {
-            throw new IOException("File is too large");
-        } 
-
-        StringBuilder sb = new StringBuilder((int) (file.length()));
-        // 创建字节输入流  
-        FileInputStream fis = new FileInputStream(filePath);  
-        // 创建一个长度为10240的Buffer
-        byte[] bbuf = new byte[10240];  
-        // 用于保存实际读取的字节数  
-        int hasRead = 0;  
-        while ( (hasRead = fis.read(bbuf)) > 0 ) {  
-            sb.append(new String(bbuf, 0, hasRead));  
-        }  
-        fis.close();  
-        return sb.toString();
-    }
-
-    /**
-     * 根据文件路径读取byte[] 数组
-     */
-    public static byte[] readFileByBytes(String filePath) throws IOException {
-        File file = new File(filePath);
-        if (!file.exists()) {
-            throw new FileNotFoundException(filePath);
-        } else {
-            ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
-            BufferedInputStream in = null;
-
-            try {
-                in = new BufferedInputStream(new FileInputStream(file));
-                short bufSize = 1024;
-                byte[] buffer = new byte[bufSize];
-                int len1;
-                while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
-                    bos.write(buffer, 0, len1);
-                }
-
-                byte[] var7 = bos.toByteArray();
-                return var7;
-            } finally {
-                try {
-                    if (in != null) {
-                        in.close();
-                    }
-                } catch (IOException var14) {
-                    var14.printStackTrace();
-                }
-
-                bos.close();
-            }
-        }
-    }
-}

+ 0 - 29
eladmin-system/src/main/java/me/zhengjie/base/util/baidu/GsonUtils.java

@@ -1,29 +0,0 @@
-/*
- * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
- */
-package me.zhengjie.base.util.baidu;
-
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-import com.google.gson.JsonParseException;
-
-import java.lang.reflect.Type;
-
-/**
- * Json工具类.
- */
-public class GsonUtils {
-    private static Gson gson = new GsonBuilder().create();
-
-    public static String toJson(Object value) {
-        return gson.toJson(value);
-    }
-
-    public static <T> T fromJson(String json, Class<T> classOfT) throws JsonParseException {
-        return gson.fromJson(json, classOfT);
-    }
-
-    public static <T> T fromJson(String json, Type typeOfT) throws JsonParseException {
-        return (T) gson.fromJson(json, typeOfT);
-    }
-}