package com.qxgmat.help; import com.alibaba.fastjson.JSONObject; import com.nuliji.tools.exception.ParameterException; import com.nuliji.tools.third.baidu.BaiduAi; import net.ipip.ipdb.City; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; import org.springframework.stereotype.Service; import java.io.IOException; import java.util.Arrays; import java.util.HashMap; import java.util.Map; /** * Created by GaoJie on 2017/11/3. */ @Service public class AiHelp { private static final Logger logger = LoggerFactory.getLogger(AiHelp.class); private BaiduAi ai; private City cityDb; @Autowired private void getDb(@Value("${ip.database.path}") String path) throws IOException { // City类可用于IPDB格式的IPv4免费库,IPv4与IPv6的每周高级版、每日标准版、每日高级版、每日专业版、每日旗舰版 if (path.startsWith(".")){ // 相对路径,从resource中获取 // logger.debug("{}, {}", path, this.getClass().getClassLoader().getResource(path.replaceFirst(".",""))); try { cityDb = new City(this.getClass().getClassLoader().getResourceAsStream(path.replaceFirst(".",""))); }catch (Exception e){ try{ cityDb = new City(this.getClass().getClassLoader().getResourceAsStream(path)); }catch (Exception ee){ logger.error(ee.getLocalizedMessage()); } } }else{ cityDb = new City(path); } } @Autowired private void getSms(@Value("${third.baiduai.appKey}") String appKey, @Value("${third.baiduai.appSecret}") String appSecret) { this.ai = new BaiduAi(appKey, appSecret); } // normal-识别正常 // reversed_side-身份证正反面颠倒 // non_idcard-上传的图片中不包含身份证 // blurred-身份证模糊 // other_type_card-其他类型证照 // over_exposure-身份证关键字段反光或过曝 // over_dark-身份证欠曝(亮度过低) // unknown-未知状态 public Map orcIdcardFront(byte[] image){ BaiduAi.IdcardResponse response = ai.idcard(BaiduAi.IdcardSide.FRONT, false, false, image); validResponse(response); Map map = new HashMap<>(); JSONObject result = response.getWords_result(); JSONObject name = result.getJSONObject(BaiduAi.IdcardResult.NAME.key); map.put("name", name.getString("words")); JSONObject address = result.getJSONObject(BaiduAi.IdcardResult.ADDRESS.key); map.put("address", address.getString("words")); JSONObject identity = result.getJSONObject(BaiduAi.IdcardResult.ID.key); map.put("identity", identity.getString("words")); return map; } public boolean orcIdcardBack(byte[] image){ BaiduAi.IdcardResponse response = ai.idcard(BaiduAi.IdcardSide.BACK, false, false, image); validResponse(response); return true; } private boolean validResponse(BaiduAi.IdcardResponse response){ switch(response.getImage_status()){ case "normal": return true; case "reversed_side": throw new ParameterException("身份证正反面颠倒"); case "non_idcard": throw new ParameterException("上传的图片中不包含身份证"); case "blurred": throw new ParameterException("身份证模糊"); case "other_type_card": throw new ParameterException("其他类型证照"); case "over_exposure": throw new ParameterException("身份证关键字段反光或过曝"); case "over_dark": throw new ParameterException("亮度过低"); default: throw new ParameterException("图片识别失败"); } } public boolean compareIp(String ip1, String ip2){ try { // db.find(address, language) 返回索引数组 String[] ip1info = cityDb.find(ip1, "CN"); String[] ip2info = cityDb.find(ip2, "CN"); // 只考虑国家和省级 for(int i = 0; i < 2; i++){ if (!ip1info[i].equals(ip2info[i])) return false; } return true; } catch (Exception e) { e.printStackTrace(); } return true; } public String[] parseIp(String ip){ try { // db.find(address, language) 返回索引数组 return cityDb.find(ip, "CN"); } catch (Exception e) { e.printStackTrace(); } return new String[0]; } }