AiHelp.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. cityDb = new City(this.getClass().getClassLoader().getResourceAsStream(path.replaceFirst(".", "")));
  30. }else{
  31. cityDb = new City(path);
  32. }
  33. }
  34. @Autowired
  35. private void getSms(@Value("${third.baiduai.appKey}") String appKey,
  36. @Value("${third.baiduai.appSecret}") String appSecret) {
  37. this.ai = new BaiduAi(appKey, appSecret);
  38. }
  39. // normal-识别正常
  40. // reversed_side-身份证正反面颠倒
  41. // non_idcard-上传的图片中不包含身份证
  42. // blurred-身份证模糊
  43. // other_type_card-其他类型证照
  44. // over_exposure-身份证关键字段反光或过曝
  45. // over_dark-身份证欠曝(亮度过低)
  46. // unknown-未知状态
  47. public Map<String, String> orcIdcardFront(byte[] image){
  48. BaiduAi.IdcardResponse response = ai.idcard(BaiduAi.IdcardSide.FRONT, false, false, image);
  49. validResponse(response);
  50. Map<String, String> map = new HashMap<>();
  51. JSONObject result = response.getWords_result();
  52. JSONObject name = result.getJSONObject(BaiduAi.IdcardResult.NAME.key);
  53. map.put("name", name.getString("words"));
  54. JSONObject address = result.getJSONObject(BaiduAi.IdcardResult.ADDRESS.key);
  55. map.put("address", address.getString("words"));
  56. JSONObject identity = result.getJSONObject(BaiduAi.IdcardResult.ID.key);
  57. map.put("identity", identity.getString("words"));
  58. return map;
  59. }
  60. public boolean orcIdcardBack(byte[] image){
  61. BaiduAi.IdcardResponse response = ai.idcard(BaiduAi.IdcardSide.BACK, false, false, image);
  62. validResponse(response);
  63. return true;
  64. }
  65. private boolean validResponse(BaiduAi.IdcardResponse response){
  66. switch(response.getImage_status()){
  67. case "normal":
  68. return true;
  69. case "reversed_side":
  70. throw new ParameterException("身份证正反面颠倒");
  71. case "non_idcard":
  72. throw new ParameterException("上传的图片中不包含身份证");
  73. case "blurred":
  74. throw new ParameterException("身份证模糊");
  75. case "other_type_card":
  76. throw new ParameterException("其他类型证照");
  77. case "over_exposure":
  78. throw new ParameterException("身份证关键字段反光或过曝");
  79. case "over_dark":
  80. throw new ParameterException("亮度过低");
  81. default:
  82. throw new ParameterException("图片识别失败");
  83. }
  84. }
  85. public boolean compareIp(String ip1, String ip2){
  86. try {
  87. // db.find(address, language) 返回索引数组
  88. String[] ip1info = cityDb.find(ip1, "CN");
  89. String[] ip2info = cityDb.find(ip2, "CN");
  90. // 只考虑国家和省级
  91. for(int i = 0; i < 2; i++){
  92. if (!ip1info[i].equals(ip2info[i])) return false;
  93. }
  94. return true;
  95. } catch (Exception e) {
  96. e.printStackTrace();
  97. }
  98. return true;
  99. }
  100. public String[] parseIp(String ip){
  101. try {
  102. // db.find(address, language) 返回索引数组
  103. return cityDb.find(ip, "CN");
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. }
  107. return new String[0];
  108. }
  109. }