AiHelp.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.qxgmat.help;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.nuliji.tools.exception.ParameterException;
  4. import com.nuliji.tools.third.baidu.BaiduAi;
  5. import net.ipip.ipdb.City;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.core.io.FileSystemResource;
  11. import org.springframework.stereotype.Service;
  12. import java.io.IOException;
  13. import java.util.Arrays;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. /**
  17. * Created by GaoJie on 2017/11/3.
  18. */
  19. @Service
  20. public class AiHelp {
  21. private static final Logger logger = LoggerFactory.getLogger(AiHelp.class);
  22. private BaiduAi ai;
  23. private City cityDb;
  24. @Autowired
  25. private void getDb(@Value("${ip.database.path}") String path) throws IOException {
  26. // City类可用于IPDB格式的IPv4免费库,IPv4与IPv6的每周高级版、每日标准版、每日高级版、每日专业版、每日旗舰版
  27. if (path.startsWith(".")){
  28. // 相对路径,从resource中获取
  29. // logger.debug("{}, {}", path, this.getClass().getClassLoader().getResource(path.replaceFirst(".","")));
  30. try {
  31. cityDb = new City(this.getClass().getClassLoader().getResourceAsStream(path.replaceFirst(".","")));
  32. }catch (Exception e){
  33. try{
  34. cityDb = new City(this.getClass().getClassLoader().getResourceAsStream(path));
  35. }catch (Exception ee){
  36. logger.error(ee.getLocalizedMessage());
  37. }
  38. }
  39. }else{
  40. cityDb = new City(path);
  41. }
  42. }
  43. @Autowired
  44. private void getSms(@Value("${third.baiduai.appKey}") String appKey,
  45. @Value("${third.baiduai.appSecret}") String appSecret) {
  46. this.ai = new BaiduAi(appKey, appSecret);
  47. }
  48. // normal-识别正常
  49. // reversed_side-身份证正反面颠倒
  50. // non_idcard-上传的图片中不包含身份证
  51. // blurred-身份证模糊
  52. // other_type_card-其他类型证照
  53. // over_exposure-身份证关键字段反光或过曝
  54. // over_dark-身份证欠曝(亮度过低)
  55. // unknown-未知状态
  56. public Map<String, String> orcIdcardFront(byte[] image){
  57. BaiduAi.IdcardResponse response = ai.idcard(BaiduAi.IdcardSide.FRONT, false, false, image);
  58. validResponse(response);
  59. Map<String, String> map = new HashMap<>();
  60. JSONObject result = response.getWords_result();
  61. JSONObject name = result.getJSONObject(BaiduAi.IdcardResult.NAME.key);
  62. map.put("name", name.getString("words"));
  63. JSONObject address = result.getJSONObject(BaiduAi.IdcardResult.ADDRESS.key);
  64. map.put("address", address.getString("words"));
  65. JSONObject identity = result.getJSONObject(BaiduAi.IdcardResult.ID.key);
  66. map.put("identity", identity.getString("words"));
  67. return map;
  68. }
  69. public boolean orcIdcardBack(byte[] image){
  70. BaiduAi.IdcardResponse response = ai.idcard(BaiduAi.IdcardSide.BACK, false, false, image);
  71. validResponse(response);
  72. return true;
  73. }
  74. private boolean validResponse(BaiduAi.IdcardResponse response){
  75. switch(response.getImage_status()){
  76. case "normal":
  77. return true;
  78. case "reversed_side":
  79. throw new ParameterException("身份证正反面颠倒");
  80. case "non_idcard":
  81. throw new ParameterException("上传的图片中不包含身份证");
  82. case "blurred":
  83. throw new ParameterException("身份证模糊");
  84. case "other_type_card":
  85. throw new ParameterException("其他类型证照");
  86. case "over_exposure":
  87. throw new ParameterException("身份证关键字段反光或过曝");
  88. case "over_dark":
  89. throw new ParameterException("亮度过低");
  90. default:
  91. throw new ParameterException("图片识别失败");
  92. }
  93. }
  94. public boolean compareIp(String ip1, String ip2){
  95. try {
  96. // db.find(address, language) 返回索引数组
  97. String[] ip1info = cityDb.find(ip1, "CN");
  98. String[] ip2info = cityDb.find(ip2, "CN");
  99. // 只考虑国家和省级
  100. for(int i = 0; i < 2; i++){
  101. if (!ip1info[i].equals(ip2info[i])) return false;
  102. }
  103. return true;
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. }
  107. return true;
  108. }
  109. public String[] parseIp(String ip){
  110. try {
  111. // db.find(address, language) 返回索引数组
  112. return cityDb.find(ip, "CN");
  113. } catch (Exception e) {
  114. e.printStackTrace();
  115. }
  116. return new String[0];
  117. }
  118. }