Ver Fonte

新增签名图片压缩 人脸对比记录

tongfeng há 2 anos atrás
pai
commit
b1b940b82f

+ 45 - 5
eladmin-system/src/main/java/me/zhengjie/base/util/FileUploadUtil.java

@@ -1,10 +1,6 @@
 package me.zhengjie.base.util;
 
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.util.Iterator;
 import java.util.UUID;
 
@@ -12,6 +8,7 @@ import javax.imageio.ImageIO;
 import javax.imageio.ImageReader;
 import javax.imageio.stream.ImageInputStream;
 
+import net.coobird.thumbnailator.Thumbnails;
 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -27,6 +24,7 @@ import me.zhengjie.application.bank.controller.vo.ImageUploadVO;
 import me.zhengjie.base.file.FileHandle;
 import me.zhengjie.base.file.FileHandleVo;
 
+
 /**
  * 将图片转换为Base64<br>
  * 将base64编码字符串解码成img图片
@@ -73,6 +71,7 @@ public class FileUploadUtil {
 		return new String(Base64.encodeBase64(data));
 	}
 
+
 	/**
 	 * 对字节数组字符串进行Base64解码并生成图片,注意处理图片前面是否加了data:image/png;这个
 	 * 
@@ -116,6 +115,27 @@ public class FileUploadUtil {
 			String imagePath = image.getBusinessNo() + "/" + orderType + "/" + fileName;
 			// 这里目前只会说图片的
 			String contentType = "image/" + suffix;
+
+			ByteArrayInputStream byteArrayInputStream = IoUtil.toStream(b);
+
+			long srcSize = b.length;
+			double accuracy = getAccuracy(srcSize / 1024);
+			try {
+				while (b.length > 40L * 1024) {
+					ByteArrayInputStream inputStream = new ByteArrayInputStream(b);
+					ByteArrayOutputStream outputStream = new ByteArrayOutputStream(b.length);
+					Thumbnails.of(inputStream)
+							.scale(accuracy)
+							.outputQuality(accuracy)
+							.toOutputStream(outputStream);
+					b = outputStream.toByteArray();
+				}
+				log.info("图片原大小={}kb | 压缩后大小={}kb",
+						srcSize / 1024, b.length / 1024);
+			} catch (Exception e) {
+				log.error("【图片压缩】msg=图片压缩失败!", e);
+			}
+
 			uploadFile(imagePath, contentType, b);
 			// FileUtils.writeByteArrayToFile(fullPath, b);
 			return imagePath;
@@ -125,6 +145,26 @@ public class FileUploadUtil {
 		}
 	}
 
+	/**
+	 * 自动调节精度(经验数值)
+	 *
+	 * @param size 源图片大小
+	 * @return 图片压缩质量比
+	 */
+	private static double getAccuracy(long size) {
+		double accuracy;
+		if (size < 900) {
+			accuracy = 0.85;
+		} else if (size < 2047) {
+			accuracy = 0.6;
+		} else if (size < 3275) {
+			accuracy = 0.44;
+		} else {
+			accuracy = 0.4;
+		}
+		return accuracy;
+	}
+
 	public static String saveFaceImage(String imgStr) {
 
 		if (imgStr == null) // 图像数据为空

+ 18 - 19
eladmin-system/src/main/java/me/zhengjie/base/util/JuheServiceUtil.java

@@ -65,10 +65,9 @@ public class JuheServiceUtil {
      * @param params 业务参数
      * @return String response
      */
-    public static FaceCompareEntity URLPost(String idCard, String realName, String base64) {
-        FaceCompareEntity thirdPartyInterfaceEntity = new FaceCompareEntity();
-        thirdPartyInterfaceEntity.setName(realName);
-        thirdPartyInterfaceEntity.setIdCard(idCard);
+    public static FaceCompareEntity URLPost(String idCard, String realName, String base64,FaceCompareEntity faceCompareEntity) {
+        faceCompareEntity.setName(realName);
+        faceCompareEntity.setIdCard(idCard);
         String response = EMPTY;
         PostMethod postMethod = null;
         try {
@@ -88,26 +87,26 @@ public class JuheServiceUtil {
                 String reason = jsonObj.getString("reason");
                 JSONObject result = jsonObj.getJSONObject("result");;
                 if (! "成功".equals(reason)){
-                    thirdPartyInterfaceEntity.setMessage(result.getString("message"));
-                    thirdPartyInterfaceEntity.setResult(0);
-                    return thirdPartyInterfaceEntity;
+                    faceCompareEntity.setMessage(result.getString("message"));
+                    faceCompareEntity.setResult(0);
+                    return faceCompareEntity;
                 }
                 String res = result.getString("res");
                 if (!SUCCESS_CODE.equals(res)){
-                    thirdPartyInterfaceEntity.setMessage(result.getString("message"));
-                    thirdPartyInterfaceEntity.setResult(0);
-                    return thirdPartyInterfaceEntity;
+                    faceCompareEntity.setMessage(result.getString("message"));
+                    faceCompareEntity.setResult(0);
+                    return faceCompareEntity;
                 }
                 String score = result.getString("score");
                 if (Integer.parseInt(score) < 75) {
-                    thirdPartyInterfaceEntity.setScore(score);
-                    thirdPartyInterfaceEntity.setMessage("比对分值小于75");
-                    thirdPartyInterfaceEntity.setResult(0);
-                    return thirdPartyInterfaceEntity;
+                    faceCompareEntity.setScore(score);
+                    faceCompareEntity.setMessage("比对分值小于75");
+                    faceCompareEntity.setResult(0);
+                    return faceCompareEntity;
                 }
-                thirdPartyInterfaceEntity.setScore(score);
-                thirdPartyInterfaceEntity.setResult(1);
-                return thirdPartyInterfaceEntity;
+                faceCompareEntity.setScore(score);
+                faceCompareEntity.setResult(1);
+                return faceCompareEntity;
             } else {
                 logger.error("响应状态码=" + postMethod.getStatusCode());
             }
@@ -121,7 +120,7 @@ public class JuheServiceUtil {
                 postMethod = null;
             }
         }
-        return thirdPartyInterfaceEntity;
+        return faceCompareEntity;
     }
 
     public static void main(String[] args) throws Exception {
@@ -142,7 +141,7 @@ public class JuheServiceUtil {
             params.put("realname", realName);
             params.put("image", base64);
             params.put("key", KEY_CODE);
-            FaceCompareEntity thirdPartyInterfaceEntity = JuheServiceUtil.URLPost(idCard,realName,base64);
+            FaceCompareEntity thirdPartyInterfaceEntity = JuheServiceUtil.URLPost(idCard,realName,base64,new FaceCompareEntity());
             System.out.println(thirdPartyInterfaceEntity);
         } catch (Exception e) {
             e.printStackTrace();

+ 54 - 23
eladmin-system/src/main/java/me/zhengjie/domain/notary/impl/NotaryDomainImpl.java

@@ -28,10 +28,9 @@ import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.ObjectUtils;
 
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.util.ArrayList;
+import java.util.Base64;
 import java.util.List;
 import java.util.concurrent.ThreadPoolExecutor;
 
@@ -80,6 +79,7 @@ public class NotaryDomainImpl implements NotaryDomain {
      * @return
      */
     public ResultCode appCompareFace(CompareImageDomain compareImageDomain) {
+        FaceCompareEntity thirdPartyInterfaceEntity = new FaceCompareEntity();
         String base64 = compareImageDomain.getBase64();
         if (base64 == null) {
             return ResultCode.PARAM_IS_BLANK;
@@ -124,6 +124,8 @@ public class NotaryDomainImpl implements NotaryDomain {
         }
 
 
+        thirdPartyInterfaceEntity.setBusinessNo(compareImageDomain.getBusinessNo());
+        thirdPartyInterfaceEntity.setConsultNo(contractOrderEntity.getConsultNo());
         FileInfoEntity fileInfo = uploadFaceFile(base64);
         boolean redisFlag = (count != null && count >= 2);
         if (ObjectUtils.isEmpty(id) || redisFlag) {
@@ -131,10 +133,9 @@ public class NotaryDomainImpl implements NotaryDomain {
                 return ResultCode.IMAGE_NOT_STANDARD;
             }
             //要做人脸核身
-            FaceCompareEntity thirdPartyInterfaceEntity = JuheServiceUtil.URLPost(idCard, nickName, base64);
+            thirdPartyInterfaceEntity = JuheServiceUtil.URLPost(idCard, nickName, base64,thirdPartyInterfaceEntity);
             thirdPartyInterfaceEntity.setFileId(fileInfo.getId() + "");
-            thirdPartyInterfaceEntity.setBusinessNo(compareImageDomain.getBusinessNo());
-            thirdPartyInterfaceEntity.setConsultNo(contractOrderEntity.getConsultNo());
+
             thirdPartyInterfaceRepository.insert(thirdPartyInterfaceEntity);
             if (thirdPartyInterfaceEntity.getResult() == 1) {
                 ContractOrderEntity orderEntity = new ContractOrderEntity();
@@ -158,7 +159,7 @@ public class NotaryDomainImpl implements NotaryDomain {
             return ResultCode.LIVE_VIDEO_INVALID;
         }*/
 
-        ResultCode resultCode = compareImage(base, id, redisCount, count);
+        ResultCode resultCode = compareImage(base, id, redisCount, thirdPartyInterfaceEntity);
 
         if (resultCode.code()==100){
             if (count != null) {
@@ -178,6 +179,8 @@ public class NotaryDomainImpl implements NotaryDomain {
      * @return
      */
     public ResultCode compareFace(CompareImageDomain compareImageDomain) {
+        FaceCompareEntity faceCompareEntity = new FaceCompareEntity();
+
         String base64 = compareImageDomain.getBase64();
         if (base64 == null) {
             return ResultCode.PARAM_IS_BLANK;
@@ -218,6 +221,12 @@ public class NotaryDomainImpl implements NotaryDomain {
             }
         }
 
+
+        faceCompareEntity.setBusinessNo(compareImageDomain.getBusinessNo());
+        faceCompareEntity.setConsultNo(contractOrder.getConsultNo());
+        faceCompareEntity.setIdCard(idCard);
+        faceCompareEntity.setName(nickName);
+
         //后面需要的三个参数 base64 身份证号 姓名
         String redisCount = "idCard:" + idCard + ":COUNT:";
         Integer count = null;
@@ -251,11 +260,9 @@ public class NotaryDomainImpl implements NotaryDomain {
                 }
                 //说明客户经理需要做人脸核身
                 FileInfoEntity fileInfo = uploadFaceFile(base64);
-                FaceCompareEntity thirdPartyInterfaceEntity = JuheServiceUtil.URLPost(idCard, nickName, base64);
-                thirdPartyInterfaceEntity.setFileId(fileInfo.getId() + "");
-                thirdPartyInterfaceEntity.setConsultNo(contractOrder.getConsultNo());
-                thirdPartyInterfaceEntity.setBusinessNo(compareImageDomain.getBusinessNo());
-                if (thirdPartyInterfaceEntity.getResult() == 1) {
+                faceCompareEntity = JuheServiceUtil.URLPost(idCard, nickName, base64,faceCompareEntity);
+                faceCompareEntity.setFileId(fileInfo.getId() + "");
+                if (faceCompareEntity.getResult() == 1) {
                     //上传图片到minio FileInfo
                     SysUserEntity bank = new SysUserEntity();
                     bank.setUserId(sysUserEntity.getUserId());
@@ -263,11 +270,11 @@ public class NotaryDomainImpl implements NotaryDomain {
                     userMapper.updateById(bank);
                     log.info("银行客户经理人脸核身通过");
                     redis.del(redisCount);
-                    thirdPartyInterfaceRepository.insert(thirdPartyInterfaceEntity);
+                    thirdPartyInterfaceRepository.insert(faceCompareEntity);
                     return ResultCode.SUCCESS;
                 }
                 log.info("客户经理人脸核身失败");
-                thirdPartyInterfaceRepository.insert(thirdPartyInterfaceEntity);
+                thirdPartyInterfaceRepository.insert(faceCompareEntity);
                 return ResultCode.LIVE_VIDEO_INVALID;
             }
         } else if (ObjectUtils.isEmpty(compareImageId) || redisFlag) {
@@ -277,12 +284,11 @@ public class NotaryDomainImpl implements NotaryDomain {
 
             //说明当事人需要做人脸核身
             FileInfoEntity fileInfo = uploadFaceFile(base64);
-            FaceCompareEntity thirdPartyInterfaceEntity = JuheServiceUtil.URLPost(idCard, nickName, base64);
-            thirdPartyInterfaceEntity.setFileId(fileInfo.getId() + "");
-            thirdPartyInterfaceEntity.setBusinessNo(compareImageDomain.getBusinessNo());
-            thirdPartyInterfaceEntity.setConsultNo(contractOrder.getConsultNo());
-            thirdPartyInterfaceRepository.insert(thirdPartyInterfaceEntity);
-            if (thirdPartyInterfaceEntity.getResult() == 1) {
+            faceCompareEntity = JuheServiceUtil.URLPost(idCard, nickName, base64,faceCompareEntity);
+            faceCompareEntity.setFileId(fileInfo.getId() + "");
+
+            thirdPartyInterfaceRepository.insert(faceCompareEntity);
+            if (faceCompareEntity.getResult() == 1) {
                 //上传图片到minio FileInfo;
                 ContractOrderEntity orderEntity = new ContractOrderEntity();
                 orderEntity.setId(contractOrder.getId());
@@ -315,7 +321,7 @@ public class NotaryDomainImpl implements NotaryDomain {
             return ResultCode.LIVE_VIDEO_INVALID;
         }*/
 
-        ResultCode resultCode = compareImage(base, id, redisCount, count);
+        ResultCode resultCode = compareImage(base, id, redisCount, faceCompareEntity);
 
         if (resultCode.code()==100){
             if (count != null) {
@@ -463,7 +469,7 @@ public class NotaryDomainImpl implements NotaryDomain {
      * @param count      redis当中的次数
      * @return
      */
-    public ResultCode compareImage(File base, Integer id, String redisCount, Integer count) {
+    public ResultCode compareImage(File base, Integer id, String redisCount, FaceCompareEntity faceCompareEntity) {
         //从官网获取
         FaceEngine faceEngine = new FaceEngine(libpath);
         //激活引擎
@@ -584,8 +590,30 @@ public class NotaryDomainImpl implements NotaryDomain {
         //如果通过 上传文件 修改用户绑定id 如果没通过 返回null证明失败了
         //引擎卸载
         errorCode = faceEngine.unInit();
-        if (faceSimilar.getScore() > 0.8) {
+        float score = faceSimilar.getScore();
+        faceCompareEntity.setScore(score+"");
+        FileInputStream fileInputStream = null;
+        try {
+            fileInputStream = new FileInputStream(file);
+        } catch (FileNotFoundException e) {
+            throw new RuntimeException(e);
+        }
+        byte[] bytes = new byte[(int) file.length()];
+        try {
+            fileInputStream.read(bytes);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+
+        String s = Base64.getEncoder().encodeToString(bytes);
+
+        FileInfoEntity fileInfo = uploadFaceFile(s);
+        faceCompareEntity.setFileId(fileInfo.getId()+"");
+        if (score > 0.8) {
             log.info("人脸照片比对通过");
+            faceCompareEntity.setMessage("人脸照片对比通过");
+            faceCompareEntity.setResult(1);
+            thirdPartyInterfaceRepository.insert(faceCompareEntity);
             base.delete();
             file.delete();
             redis.del(redisCount);
@@ -593,6 +621,9 @@ public class NotaryDomainImpl implements NotaryDomain {
         }
         redis.increment(redisCount);
         log.info("人脸照片比对未通过");
+        faceCompareEntity.setMessage("人脸照片对比未通过");
+        faceCompareEntity.setResult(0);
+        thirdPartyInterfaceRepository.insert(faceCompareEntity);
         base.delete();
         file.delete();
         return ResultCode.LIVE_VIDEO_INVALID;